JavaFX。将多个带有图像的标签添加到窗格

JavaFX。将多个带有图像的标签添加到窗格,java,javafx-2,Java,Javafx 2,我想在窗格中添加两个不同的带有图像的标签。我使用的代码如下: import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; public class Controller extends Application { public static void main(String[] args) { launch(args); }

我想在窗格中添加两个不同的带有图像的标签。我使用的代码如下:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Controller extends Application {


    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("Starting FX");
        primaryStage.setScene(new Scene(new Panel(), 590, 390));
        primaryStage.setResizable(false);
        primaryStage.centerOnScreen();
        primaryStage.show();

    }

}



import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;


public class Panel extends Pane {

    private ImageView image = new ImageView(new Image(getClass().getResourceAsStream("red.jpg")));

    private Label label1 = new Label();
    private Label label2 = new Label();

    public Panel() {

        label1.relocate(524, 280);
        label1.setGraphic(image);
        this.getChildren().add(label1);

        label2.relocate(250, 200);
        label2.setGraphic(image);
        this.getChildren().add(label2);


    }



}
我的问题是它不能在屏幕上同时添加两个标签

如果我有:

this.getChildren().add(label1);
this.getChildren().add(label2);
它在屏幕上只显示标签2,即使我打印(this.getchildren())时它有两个标签

如果我有其中一个,它会正常添加

即使我两个都没有,也要执行

this.getChildren().addAll(label1, label2);
但它只添加了label2

为什么呢


谢谢

这是因为ImageView是一个节点,不能在场景图中出现两次。添加第二个ImageView并将其添加到第二个标签


private ImageView image1=新的ImageView(新图像(getClass().getResourceAsStream(“red.jpg”));

这是因为ImageView是一个节点,不能在场景图中出现两次。添加第二个ImageView并将其添加到第二个标签


private ImageView image1=新的ImageView(新图像(getClass().getResourceAsStream(“red.jpg”));

两个标签实际上都在那里,但它们不能在场景图中使用相同的ImageView。如果向标签中添加文本,则应该可以看到它们。您必须创建两个单独的ImageView实例,每个标签一个。试试这个

public class Panel extends Pane
{
    Image labelImage = new Image(getClass().getResourceAsStream("red.jpg"));
    private Label label1 = new Label();
    private Label label2 = new Label();

    public Panel()
    {
        label1.relocate(524, 280);
        label1.setGraphic(new ImageView(labelImage));
        this.getChildren().add(label1);

        label2.relocate(250, 200);
        label2.setGraphic(new ImageView(labelImage));
        this.getChildren().add(label2);
    }
}
查看此帖子以了解更多信息

两个标签实际上都在那里,但它们不能在场景图中同时使用同一个ImageView。如果向标签中添加文本,则应该可以看到它们。您必须创建两个单独的ImageView实例,每个标签一个。试试这个

public class Panel extends Pane
{
    Image labelImage = new Image(getClass().getResourceAsStream("red.jpg"));
    private Label label1 = new Label();
    private Label label2 = new Label();

    public Panel()
    {
        label1.relocate(524, 280);
        label1.setGraphic(new ImageView(labelImage));
        this.getChildren().add(label1);

        label2.relocate(250, 200);
        label2.setGraphic(new ImageView(labelImage));
        this.getChildren().add(label2);
    }
}
查看此帖子以了解更多信息