【问题标题】:How to create array of Panes in JavaFX(or how to control multiple similar panes)如何在 JavaFX 中创建窗格数组(或如何控制多个类似的窗格)
【发布时间】:2022-11-12 11:52:36
【问题描述】:

我有一个窗格,其中包括 label1、label2、label3 和一个图标(带图像的窗格)。我复制了那个窗格,现在我在左边有 10 个窗格 5,右边有 5 个,我为窗格命名了每个不同的项目(如 left1label1、left2label1、right2label3),但我不想对每个窗格使用不同的方法。

我试图创建一组窗格,但无法弄清楚。我想要的只是管理这个窗格的简单方法。我该怎么做?

public void leftPane1Config()
{

    leftPane1Label1.setText("Object");

    leftPane1Label2.setText("HP:"+ hp);
    leftPane1Label3.setText("EXP:"+ exp);

    Image img= new Image("directory");
    leftPane1Icon.setEffect(new ImageInput(img));
}

public void leftPane2Config()
{

    leftPane2Label1.setText("Object");

    leftPane2Label2.setText("HP:"+ hp);
    leftPane2Label3.setText("EXP:"+ exp);

    Image img= new Image("directory");
    leftPane2Icon.setEffect(new ImageInput(img));
}

.
.And 8 other 
.


我试图使用这样的东西(试图创建一个层次结构,但我做不到:'))但我不能使用 setText() 所以它不起作用。

leftPane1.getChildren().add(leftPane1Label1);
leftPane1.getChildren().get(0).setText??

然后我尝试使用这样的东西

Pane[] leftPane= new Pane[5];
leftPane[0]= new Pane();

我又想不通。

【问题讨论】:

    标签: java arrays javafx pane


    【解决方案1】:

    目前还不清楚你在问什么。通常,您只需编写一个方法来创建窗格并参数化变化的部分,就像您在任何类型的编程中所做的通常方式一样。

    public Pane createPane(String text1, String text2, String text3, URL image) {
        Image img = new Image(image.toExternalForm());
        // whatever layout you need here
        VBox pane = new VBox(
            new Label(text1),
            new Label(text2),
            new Label(text3),
            new ImageView(img)
        );
        // any other stuff you need to do
        return pane ;
    }
    

    然后当然你只需用你需要的值调用createPane(),然后根据需要将结果添加到你的场景图中。

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多