如何在java循环中动态改变按钮变量名?

问题描述 投票:0回答:1

我想在一个lopp中用java创建多个不同名称的按钮,我该怎么做才能自动得到这个代码?

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

我的目标是在VBox中插入一个gridPane到一个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);
     }

不幸的是,我得到一个错误的代码。Children: duplicate children added.The only way to do this correctly is to have a different button's variables namessound_button & sound_button2 etc... inserted into button_grid.How can I have a different button's variable name inside a loop?

问好

loops variables button javafx dynamic
1个回答
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);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.