简体   繁体   中英

initialising Buttons inside a for-loop

I have just started programming with javaFX and have the following problem:

i created an button-array and want to fill it with 15 buttons. when i go threw the button-array with the for-loop to put them in place, it gives me a 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.

Try changing your initialisiereButtons method like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM