【问题标题】:How can i change dynamically button variable name in a loop with java如何使用java在循环中动态更改按钮变量名称
【发布时间】:2020-09-20 22:28:03
【问题描述】:

我想使用 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 插入一个 scrollPane 到一个 VBox 中,这是我的代码

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。 如何在循环中使用不同的按钮变量名?

最好的问候

【问题讨论】:

  • 只需编写一个for 循环,它会创建一个新按钮并在每次迭代时将其添加到网格窗格中。至少表明你这样做的尝试。
  • int r; for (r = 1; r
  • 将代码放在问题中,而不是在评论中。您不能动态更改变量的名称。这样做没有任何意义,因为一旦退出循环,变量就会超出范围,所以它们的名称完全不重要。为什么不在循环的每次迭代中使用相同的变量名?如果您以后需要访问它们(绝对没有理由这样做),请将它们放在列表或数组中。

标签: loops variables button javafx dynamic


【解决方案1】:

您不能在 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);
    }

}

【讨论】:

  • 感谢您的回答,不幸的是,我想将 GridePane 添加到 ScrollPane 中,使用该代码我得到 Children: duplicate children 添加,我解释了情况,问候
  • @Napster89 不,此代码不会给您“重复的孩子”错误。您收到该错误是因为您将 same 按钮 添加到网格窗格,而不是因为您使用的是 same 变量。你真的尝试过我建议的代码吗?
猜你喜欢
  • 2011-07-14
  • 2021-11-18
  • 2014-04-15
  • 2012-01-05
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多