在for循环中初始化按钮

initialising Buttons inside a for-loop

我刚开始使用 javaFX 编程,遇到以下问题:

我创建了一个按钮数组并想用 15 个按钮填充它。当我用 for 循环抛出按钮数组以将它们放置到位时,它给了我一个 IllegalArgumentException。

代码如下:

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);
    }
}

这是错误:

java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
    at javafx.graphics/javafx.scene.Parent.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(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(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(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)

可能是因为所有创建的按钮都是一样的。 你能告诉我,哪里出了问题以及如何解决它,谢谢!

您不需要重新定义 buttons 变量。

尝试像这样更改您的 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));
        }
    }