2016-11-30 64 views
0

我想迭代地使用for循環向JavaFX中的GridPane添加標籤,但是當我嘗試啓動應用程序時,我一直在收到錯誤。控制檯輸出爲:如何在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); 
    } 
} 

回答

0

每當您想要將文本添加到網格時,您都需要使用新的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); 
} 
0

您需要爲每個標籤調用構造函數。對於您的第一個片段,只需在循環中移動該「新標籤」調用即可。

相關問題