【问题标题】:(JAVAFX) Add and display images in a gridpane(JAVAFX) 在网格窗格中添加和显示图像
【发布时间】:2018-04-05 18:53:52
【问题描述】:

我目前可以在遍历数组时根据它们的类类型将不同的标签添加到网格窗格中。我怎么能做到这一点,但使用某些图像呢?即如果数组中的对象是类型:1类,则添加图像banana.png

这是我目前用来添加标签的代码:

for (int x = 0; x < Array.length; x++) {
        for (int y = 0; y < Array[x].length; y++) {
            Label labelName;
            if (Array[x][y] instanceof Class1) {
                labelName = new Label("Class1");
            else {
                labelName = new Label(Class2");
            }

            theGrid.add(labelName, x, y);

【问题讨论】:

  • 不要使用ArrayObject
  • 不应该太难Image img = (Array[x][y] instanceof Class1) ? image1 : image2; theGrid.add(new ImageView(img), x, y);。在这种情况下,最好避免使用instanceof,并更改数组中包含的类的实现,以允许您在不考虑数组中对象类型的情况下处理这些事情......跨度>

标签: java arrays javafx fxml gridpane


【解决方案1】:

你可以用 ImageView 替换标签

Image img = new Image("\banana.png");

for (int x = 0; x < Array.length; x++) {
        for (int y = 0; y < Array[x].length; y++) {
            ImageView imageView;
            if (Array[x][y] instanceof Class1) {
                imageView = new ImageView(img);
            else {
                //do something
            }

            theGrid.add(imageView, x, y);

【讨论】:

    猜你喜欢
    • 2018-03-03
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    相关资源
    最近更新 更多