[英]initialising Buttons inside a for-loop
I have just started programming with javaFX and have the following problem:我刚开始用 javaFX 编程,遇到以下问题:
i created an button-array and want to fill it with 15 buttons.我创建了一个按钮数组并想用 15 个按钮填充它。 when i go threw the button-array with the for-loop to put them in place, it gives me a IllegalArgumentException.当我 go 将按钮阵列与 for 循环一起放置到位时,它给了我一个 IllegalArgumentException。
Here's the code:这是代码:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
public class Main extends Application {
private Button[] buttons;
@Override
public void start(Stage primaryStage) {
try {
primaryStage.setTitle("Title");
initialisiereButtons();
ordneButtons(primaryStage);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public void initialisiereButtons() {
buttons = new Button[15];
for(int i = 0; i < 15; i++) {
buttons[i] = new Button("Button" + (i+1));
}
}
public void ordneButtons(Stage primaryStage) {
GridPane grid = new GridPane();
int sizeX = 4;
int sizeY = 2;
int x = 0;
int y = 0;
while(x < sizeX && y < sizeY) {
for (Button b : buttons) {
grid.add(b, x, y);
}
x++;
if(x % 5 == 0) {
y++;
}
}
Scene scene = new Scene(grid, 390, 360);
primaryStage.setScene(scene);
}
}
And here's the error:这是错误:
java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:561)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at javafx.graphics/javafx.scene.layout.GridPane.add(GridPane.java:974)
at application.Main.ordneButtons(Main.java:46)
at application.Main.start(Main.java:19)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
It's probably because all the buttons that have been created, are the same.这可能是因为所有已创建的按钮都是相同的。 can you tell me, what's wrong and how to fix it, Thanks!你能告诉我,什么是错的以及如何解决它,谢谢!
You don't need to re-define the buttons
variable.您不需要重新定义buttons
变量。
Try changing your initialisiereButtons
method like this.尝试像这样更改您的initialisiereButtons
方法。
public void initialisiereButtons() {
buttons = new Button[15]; // Here the Button[] was removed
for(int i = 0; i < 15; i++) {
buttons[i] = new Button("Button" + (i+1));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.