在循环内初始化按钮

huangapple go评论78阅读模式
英文:

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");
            initializeButtons();
            arrangeButtons(primaryStage);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    public void initializeButtons() {
        buttons = new Button[15];
        for (int i = 0; i < 15; i++) {
            buttons[i] = new Button("Button" + (i + 1));
        }
    }

    public void arrangeButtons(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$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.arrangeButtons(Main.java:46)
	at application.Main.start(Main.java:19)
	// 更多错误信息...

这个问题可能是因为所有已创建的按钮都是相同的。你可以告诉我问题出在哪里以及如何解决吗?谢谢!

英文:

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(&quot;Title&quot;);
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 &lt; 15; i++) {
buttons[i] = new Button(&quot;Button&quot; + (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 &lt; sizeX &amp;&amp; y &lt; 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!

答案1

得分: 2

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

尝试将你的initialisiereButtons方法更改如下。

   public void initialisiereButtons() {
        buttons = new Button[15]; // 这里移除了Button[]的声明
        for(int i = 0; i < 15; i++) {
        buttons[i] = new Button("Button" + (i+1));
        }
    }
    
英文:

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 &lt; 15; i++) {
        buttons[i] = new Button(&quot;Button&quot; + (i+1));
        }
    }
    

huangapple
  • 本文由 发表于 2020年8月1日 18:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63204013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定