基于变量值的java方法选择

wfauudbj  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(379)

我正在编写一个用户的gui,我想创建一个方法,用以前定义的名称和操作创建不同数量的按钮。但是我不知道如何编写基于变量值的方法选择。谷歌没有提供任何有用的信息。有人能帮上忙吗?否则就不可能了?
下面是一些代码示例:

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**
szqfcxe2

szqfcxe21#

你可以用 actionCommand 财产 ActionEvent 传达所采取的行动。默认为按钮的标签,但也可以使用设置(不影响标签) Button.setActionCommand() .

newButton.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent paramT) {
         String actionCmd = paramT.getActionCommand();
         if (actionCmd.equals("A")) {
             doA();
         } else if (actionCmd.equals("B")) {
             doB();
         }
         System.out.println("button pressed");
     }
 });

http://docs.oracle.com/javase/6/docs/api/java/awt/button.html#setactioncommand%28java.lang.string%29
http://docs.oracle.com/javase/6/docs/api/java/awt/event/actionevent.html

ego6inou

ego6inou2#

使用哈希Map:

Map<String, Runnable> actions2methods = new HashMap<>;
actions2methods.put("Action1", new Runnable { public void run() { testAction1(); }));

如果你的行动是 Runnable 而不是从一开始的方法。

相关问题