我正在編寫用戶的GUI,我想創建一個方法,該方法將創建具有以前定義的名稱和操作的各種數量的按鈕。但我不知道如何編寫基於變量值的方法選擇。 Google沒有提供有關它的有用信息。任何人都可以幫助這個,或者這是不可能的?基於變量值的方法
下面是一些代碼示例:
String[] actions={"testAction1","testAction2","testAction3"};
defaultDialogWindow(actions,"test1", "test2", "test3");
void defaultDialogWindow(String[] actions, String... bNames){
double layoutX = 25;
double spacing = 15;
final Stage dialogStage = new Stage();
dialogStage.initOwner(stage);
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setFullScreen(false);
dialogStage.setResizable(false);
dialogStage.setHeight(100);
dialogStage.setWidth(bNames.length*100+(bNames.length-1)*spacing+2*layoutX+5);
dialogStage.setScene(new Scene(buttonBuilder(actions,spacing,layoutX,bNames)));
dialogStage.show();
}
HBox buttonBuilder(String[] actions, double spacing,double layoutX,String... bNames){
HBox lBar = new HBox(10);
final ReadOnlyDoubleProperty menuWidthProperty = lBar.widthProperty();
lBar.setAlignment(Pos.CENTER_LEFT);
lBar.setLayoutX(layoutX);
lBar.setSpacing(spacing);
for(String text : bNames){
Button newButton = new Button();
newButton.setText(text);
newButton.setFont(Font.font("Times New Roman", 22));
newButton.prefWidthProperty().set(100);
newButton.prefHeightProperty().set(50);
newButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent paramT) {
**HERE MUST BE ACTION CALL BASED ON bNames VALUE**
System.out.println("button pressed");
}
});
lBar.getChildren().add(newButton);
}
System.out.println(lBar.prefWidth(-1));
return lBar;
}
void testAction1(){
System.out.println("this is test action one");
}
void testAction2(){
System.out.println("this is test action two");
}
void testAction3(){
System.out.println("this is test action three");
}**strong text**