Loops 如何使用java动态更改循环中的按钮变量名
我想用java在lopp中创建多个具有不同名称的按钮和变量包,我该怎么做才能自动获得这些代码Loops 如何使用java动态更改循环中的按钮变量名,loops,variables,button,javafx,dynamic,Loops,Variables,Button,Javafx,Dynamic,我想用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
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的滚动窗格中,这是我的代码
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);
}
公共类控制器{
ScrollPane ScrollPane=新的ScrollPane();
GridPane按钮_grid=新建GridPane();
@FXML私有VBox-VBox;
公共无效初始化(){
按钮声音\按钮=新按钮(“Lire livre N°1”);
按钮声音\按钮2=新按钮(“Lire livre N°2”);
按钮网格。添加(声音按钮,1,1);
按钮网格。添加(声音按钮,2,1)在Java中,无法以这种方式动态生成变量的名称。请注意,在代码中,按钮的引用变量的作用域始终是循环体;由于无法访问循环外部的变量,因此调用它们并不重要,最好让它们都具有相同的名称
您在代码中遇到的“复制子项”错误是因为您多次将同一按钮添加到网格窗格中。用于引用按钮的变量不相关
真正重要的是每次创建一个新按钮,并将其添加到网格窗格中
这段代码失败了,因为它添加了两次相同的按钮:
出于同样的原因,该代码也会失败,即使它使用了不同的变量:
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 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);
}
for(int r=1;r)只需编写一个for
循环,在每次迭代时创建一个新按钮并将其添加到网格窗格中。至少显示您这样做的尝试。int r;for(r=1;r将代码放在问题中,而不是放在注释中。您不能动态更改变量的名称。这样做没有任何意义,因为一旦退出循环,变量就超出范围,因此它们的名称完全不重要。为什么不在循环的每次迭代中使用相同的变量名称?如果需要的话稍后访问它们(你完全没有理由这样做),将它们放在列表或数组中。谢谢你的回答,不幸的是,我想将GridePane添加到滚动窗格中,使用该代码我得到了子项:添加了重复的子项,我解释了这一情况,regards@Napster89不,此代码不会为您提供“重复的子项”错误。出现该错误是因为您正在向网格窗格添加相同的按钮,而不是因为您使用的是相同的变量。您是否确实尝试了我建议的代码?
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);
}
}