我想用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 中。如何在循环中使用不同按钮的变量名称?
最好的祝福
在 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] 删除。
我来说两句