java - 如何在循环中使用java动态更改按钮变量名称

Napster89

我想用java在lopp中创建多个具有不同名称的按钮变量,我该怎么做才能自动获取此代码?

GridPane button_grid = new GridPane();
         Button sound_button = new Button("Lire livre N°");
         Button sound_button2 = new Button("Lire livre N°1");
         Button sound_button3 = new Button("Lire livre N°2");
         Button sound_button4 = new Button("Lire livre N°3");
         Button sound_button5 = new Button("Lire livre N°4");
         Button sound_button6 = new Button("Lire livre N°5");
         Button sound_button7 = new Button("Lire livre N°6");
         Button sound_button8 = new Button("Lire livre N°7");
         Button sound_button9 = new Button("Lire livre N°8");
         Button sound_button10 = new Button("Lire livre N°9");
button_grid.add(sound_button, 1,1);
        button_grid.add(sound_button2, 2,1);
        button_grid.add(sound_button3, 3,1);
        button_grid.add(sound_button4, 4,1);
        button_grid.add(sound_button5, 5,1);
        button_grid.add(sound_button6, 6,1);
        button_grid.add(sound_button7, 7,1);
        button_grid.add(sound_button8, 8,1);
        button_grid.add(sound_button9, 9,1);
        button_grid.add(sound_button10,10,1);

我的目标是将 gridPane 插入到 VBox 的 scrollPane 中,这是我的代码

public class controller {

     ScrollPane scrollPane = new ScrollPane();
     GridPane button_grid = new GridPane();
     @FXML private VBox vbox;

     public void initialize() {

         Button sound_button = new Button("Lire livre N°1");
         Button sound_button2 = new Button("Lire livre N°2");


        button_grid.add(sound_button, 1,1);
        button_grid.add(sound_button, 2,1);//<================the ERROR is Here

        scrollPane.setContent(button_grid);
        scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
        scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
        vbox.getChildren().add(scrollPane);
     }

不幸的是,我收到该代码的错误:子项:添加了重复的子项。正确执行此操作的唯一方法是将不同按钮的变量名称 sound_button 和 sound_button2 等...插入到 button_grid 中。如何在循环中使用不同按钮的变量名称?

最好的祝福

詹姆斯_D

在 Java 中不能以这种方式动态生成变量的名称。请注意,在您的代码中,按钮的引用变量无论如何都限定在循环主体内;由于您无法在循环外访问这些变量,因此它们的名称无关紧要,您最好让它们都具有相同的名称。

您在代码中遇到的“重复子项”错误是因为您多次向网格窗格添加相同的按钮您用来引用按钮的变量无关紧要。

真正重要的是您每次都创建一个新按钮,并将其添加到网格窗格中。

所以这段代码失败了,因为它两次添加了相同的按钮

GridPane buttonGrid = new GridPane();
Button soundButton ;

soundButton = new Button("Lire livre N°1");
buttonGrid.add(soundButton, 1, 1);
buttonGrid.add(soundButton, 2, 1); // adds same button: "duplicate children" error

出于同样的原因,此代码也失败了,即使它使用了不同的变量:

GridPane buttonGrid = new GridPane();
Button soundButton1 ;
Button soundButton2 ;

soundButton1 = new Button("Lire livre N°1");
buttonGrid.add(soundButton1, 1, 1);

soundButton2 = soundButton1 ; // second variable refers to same button
buttonGrid.add(soundButton2, 2, 1); // "duplicate children" error

当然,这段代码成功了,因为它添加了两个不同的按钮

GridPane buttonGrid = new GridPane();
Button soundButton1 ;
Button soundButton2 ;

soundButton1 = new Button("Lire livre N°1");
buttonGrid.add(soundButton1, 1, 1);

soundButton2 = new Button("Lire livre N°2"); // second variable refers to different button
buttonGrid.add(soundButton2, 2, 1); // succeeds

这也成功了,因为它还添加了两个不同的按钮,即使它两次使用相同的变量:

GridPane buttonGrid = new GridPane();
Button soundButton ;

soundButton = new Button("Lire livre N°1");
buttonGrid.add(soundButton, 1, 1);

soundButton = new Button("Lire livre N°2"); // update variable to refer to different button
buttonGrid.add(soundButton, 2, 1); // succeeds

因此,在您冗长的、重复的代码中,您可以使用单个变量来实现这一点:

GridPane buttonGrid = new GridPane();
Button soundButton ;

soundButton = new Button("Lire livre N°1");
buttonGrid.add(soundButton, 1, 1);

soundButton = new Button("Lire livre N°2");
buttonGrid.add(soundButton, 2, 1);

soundButton = new Button("Lire livre N°3");
buttonGrid.add(soundButton, 3, 1);

soundButton = new Button("Lire livre N°4");
buttonGrid.add(soundButton, 4, 1);

// etc. etc.

但当然你也可以用循环来做到这一点:

for (int r = 1; r <= 10; r++) {
    Button soundButton = new Button("Lire livre N°"+r);
    buttonGrid.add(soundButton, r, 1);
}

这是一个完整的可运行示例:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonGrid extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        ScrollPane scrollPane = new ScrollPane();
        GridPane buttonGrid = new GridPane();
        VBox vbox = new VBox();

        for (int r = 1; r <= 10; r++) {
            Button soundButton = new Button("Lire livre N°" + r);
            buttonGrid.add(soundButton, 1, r);
        }

        scrollPane.setContent(buttonGrid);
        scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
        scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
        vbox.getChildren().add(scrollPane);

        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在java循环中动态更改方法名称

来自分类Dev

每次在 Java Android Studio 中都需要在 for 循环中更改变量名称

来自分类Java

如何在Java中的for循环中使用公式

来自分类Dev

如何在Java脚本循环中使用push?

来自分类Dev

如何在Java的for循环中使用值?

来自分类Java

在Java中使用动态名称分配变量

来自分类Java

在Kotlin / Java变量名称中使用特殊字符“ $”

来自分类Dev

如何在Java变量本身中使变量名

来自分类Dev

在循环中使用数组名称作为变量(Java)

来自分类Dev

如何在Java中使用非标准变量名来生成XML标签?

来自分类Dev

Java:如何在循环中分配变量值?

来自分类Dev

如何在while循环中使用java Scanner类使用OR条件?

来自分类Java

是否有可用Java动态生成变量名称的方法?

来自分类Dev

很难弄清楚如何在 Java 的 for 循环中使用用户输入

来自分类Java

如何在自定义类的每个循环中使用java?

来自分类Java

如何在Java的嵌套for循环中使用字母递增值?

来自分类Dev

如何在Java中将接口名称和变量名称作为字符串传递

来自分类Dev

如何在Java中使用单选按钮?

来自分类Dev

在java循环中动态使用多个setMethods

来自分类Java

Java Unicode变量名称

来自分类Dev

如何在Java中使用递增循环来获取数量递增的变量(以获取所述变量的值)?

来自分类Dev

Android Java:在for循环中使用setOnClickListener

来自分类Dev

在 Java 的“While”循环中使用“Or”条件

来自分类Dev

如何在字符串变量名称中包含反斜杠(在Java中)

来自分类Dev

如何在Java中使用Scanner,while和for循环递增字符串名称

来自分类Dev

如何在Java中使用用户定义的名称创建变量?

来自分类Dev

如何在 JAVA 中使用字符串变量作为对象名称?

来自分类Dev

如何在循环中更改Bash变量名称,然后展开更改的名称?

来自分类Dev

如何在动态SQL中使用表变量名称?

Related 相关文章

  1. 1

    如何在java循环中动态更改方法名称

  2. 2

    每次在 Java Android Studio 中都需要在 for 循环中更改变量名称

  3. 3

    如何在Java中的for循环中使用公式

  4. 4

    如何在Java脚本循环中使用push?

  5. 5

    如何在Java的for循环中使用值?

  6. 6

    在Java中使用动态名称分配变量

  7. 7

    在Kotlin / Java变量名称中使用特殊字符“ $”

  8. 8

    如何在Java变量本身中使变量名

  9. 9

    在循环中使用数组名称作为变量(Java)

  10. 10

    如何在Java中使用非标准变量名来生成XML标签?

  11. 11

    Java:如何在循环中分配变量值?

  12. 12

    如何在while循环中使用java Scanner类使用OR条件?

  13. 13

    是否有可用Java动态生成变量名称的方法?

  14. 14

    很难弄清楚如何在 Java 的 for 循环中使用用户输入

  15. 15

    如何在自定义类的每个循环中使用java?

  16. 16

    如何在Java的嵌套for循环中使用字母递增值?

  17. 17

    如何在Java中将接口名称和变量名称作为字符串传递

  18. 18

    如何在Java中使用单选按钮?

  19. 19

    在java循环中动态使用多个setMethods

  20. 20

    Java Unicode变量名称

  21. 21

    如何在Java中使用递增循环来获取数量递增的变量(以获取所述变量的值)?

  22. 22

    Android Java:在for循环中使用setOnClickListener

  23. 23

    在 Java 的“While”循环中使用“Or”条件

  24. 24

    如何在字符串变量名称中包含反斜杠(在Java中)

  25. 25

    如何在Java中使用Scanner,while和for循环递增字符串名称

  26. 26

    如何在Java中使用用户定义的名称创建变量?

  27. 27

    如何在 JAVA 中使用字符串变量作为对象名称?

  28. 28

    如何在循环中更改Bash变量名称,然后展开更改的名称?

  29. 29

    如何在动态SQL中使用表变量名称?

热门标签

归档