在循环内初始化按钮

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

initialising Buttons inside a for-loop

问题

我刚刚开始使用JavaFX进行编程,遇到了以下问题:

我创建了一个按钮数组,并希望用15个按钮填充它。当我使用for循环遍历按钮数组以将它们放在指定位置时,出现了IllegalArgumentException异常。

以下是代码:

  1. package application;
  2. import javafx.application.Application;
  3. import javafx.stage.Stage;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.layout.BorderPane;
  7. import javafx.scene.layout.GridPane;
  8. public class Main extends Application {
  9. private Button[] buttons;
  10. @Override
  11. public void start(Stage primaryStage) {
  12. try {
  13. primaryStage.setTitle("Title");
  14. initializeButtons();
  15. arrangeButtons(primaryStage);
  16. primaryStage.show();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. public static void main(String[] args) {
  22. launch(args);
  23. }
  24. public void initializeButtons() {
  25. buttons = new Button[15];
  26. for (int i = 0; i < 15; i++) {
  27. buttons[i] = new Button("Button" + (i + 1));
  28. }
  29. }
  30. public void arrangeButtons(Stage primaryStage) {
  31. GridPane grid = new GridPane();
  32. int sizeX = 4;
  33. int sizeY = 2;
  34. int x = 0;
  35. int y = 0;
  36. while (x < sizeX && y < sizeY) {
  37. for (Button b : buttons) {
  38. grid.add(b, x, y);
  39. }
  40. x++;
  41. if (x % 5 == 0) {
  42. y++;
  43. }
  44. }
  45. Scene scene = new Scene(grid, 390, 360);
  46. primaryStage.setScene(scene);
  47. }
  48. }

这里是错误信息:

  1. java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
  2. at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:561)
  3. at javafx.base/com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
  4. at javafx.graphics/javafx.scene.layout.GridPane.add(GridPane.java:974)
  5. at application.Main.arrangeButtons(Main.java:46)
  6. at application.Main.start(Main.java:19)
  7. // 更多错误信息...

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

英文:

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:

  1. package application;
  2. import javafx.application.Application;
  3. import javafx.stage.Stage;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.layout.BorderPane;
  7. import javafx.scene.layout.GridPane;
  8. public class Main extends Application {
  9. private Button[] buttons;
  10. @Override
  11. public void start(Stage primaryStage) {
  12. try {
  13. primaryStage.setTitle(&quot;Title&quot;);
  14. initialisiereButtons();
  15. ordneButtons(primaryStage);
  16. primaryStage.show();
  17. } catch(Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. public static void main(String[] args) {
  22. launch(args);
  23. }
  24. public void initialisiereButtons() {
  25. buttons = new Button[15];
  26. for(int i = 0; i &lt; 15; i++) {
  27. buttons[i] = new Button(&quot;Button&quot; + (i+1));
  28. }
  29. }
  30. public void ordneButtons(Stage primaryStage) {
  31. GridPane grid = new GridPane();
  32. int sizeX = 4;
  33. int sizeY = 2;
  34. int x = 0;
  35. int y = 0;
  36. while(x &lt; sizeX &amp;&amp; y &lt; sizeY) {
  37. for (Button b : buttons) {
  38. grid.add(b, x, y);
  39. }
  40. x++;
  41. if(x % 5 == 0) {
  42. y++;
  43. }
  44. }
  45. Scene scene = new Scene(grid, 390, 360);
  46. primaryStage.setScene(scene);
  47. }
  48. }

And here's the error:

  1. java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
  2. at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:561)
  3. at javafx.base/com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
  4. at javafx.graphics/javafx.scene.layout.GridPane.add(GridPane.java:974)
  5. at application.Main.ordneButtons(Main.java:46)
  6. at application.Main.start(Main.java:19)
  7. at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
  8. at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
  9. at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
  10. at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
  11. at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
  12. at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
  13. at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  14. at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
  15. 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方法更改如下。

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

You don't need to re-define the buttons variable.

Try changing your initialisiereButtons method like this.

  1. public void initialisiereButtons() {
  2. buttons = new Button[15]; // Here the Button[] was removed
  3. for(int i = 0; i &lt; 15; i++) {
  4. buttons[i] = new Button(&quot;Button&quot; + (i+1));
  5. }
  6. }

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:

确定