【问题标题】:How can labels be iteratively added to a GridPane in JavaFX?如何将标签迭代地添加到 JavaFX 中的 GridPane?
【发布时间】:2016-11-30 18:25:44
【问题描述】:

我正在尝试使用 for 循环迭代地将标签添加到 JavaFX 中的 GridPane,但是当我尝试启动应用程序时,我不断收到错误消息。控制台输出为:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=10.0, vgap=10.0, alignment=TOP_CENTER
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.scene.layout.GridPane.add(GridPane.java:965)
    at com.comsci.drt.ProjectGUI.start(ProjectGUI.java:181)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)

我是这方面的初学者,但我知道它告诉我我正在尝试放置一个已经放置的对象。

代码:

Label l = new Label();
for(int i = 2; i <= 3; i++) {
    l.setText(finalResultsArray[i].getTeamName());   <-- line 181
    pane1.add(l, 0, i);
}

然后我继续使用一组标签,我认为这些标签不会尝试替换已放置的对象,但我收到另一个错误:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at com.comsci.drt.ProjectGUI.start(ProjectGUI.java:175)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)

代码:

Label[] label = new Label[12];
for(int i = 0; i < 12; i++) {
    for(int j = 0; j < 1; j++) {
        label[i].setText(finalResultsArray[i].getTeamName());
        pane1.add(label[i], 0, i);
    }
}

【问题讨论】:

    标签: javafx gridpane


    【解决方案1】:

    每次您想向网格中添加文本时,都需要使用一个新的Label 实例。此外,新创建的数组填充有null 值。

    因此,完成这项工作的可能方法是

    for(int i = 2; i <= 3; i++) {
        Label l = new Label();
        l.setText(finalResultsArray[i].getTeamName());
        pane1.add(l, 0, i);
    }
    

    Label[] label = new Label[12];
    for(int i = 0; i < 12; i++) {
        label[i] = new Label();
        label[i].setText(finalResultsArray[i].getTeamName());
        pane1.add(label[i], 0, i);
    }
    

    【讨论】:

      【解决方案2】:

      您需要为每个标签调用一个构造函数。对于您的第一个 sn-p,只需将“新标签”调用移动到循环内即可。

      【讨论】:

        猜你喜欢
        • 2022-12-31
        • 1970-01-01
        • 2014-08-22
        • 2016-12-15
        • 2014-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-13
        相关资源
        最近更新 更多