编程语言
首页 > 编程语言> > 如何根据JavaFX中的ComboBox选择为Button执行多个操作

如何根据JavaFX中的ComboBox选择为Button执行多个操作

作者:互联网

这是创建用户界面的主要类:

public class Test extends Application {

@Override
public void start(Stage primaryStage) {

    FlowPane mainPane = new FlowPane();         


    FlowPane query = new FlowPane();            
    query.setPadding(new Insets(30,30,30,30));
    query.setHgap(10);
    query.setVgap(20);

    ComboBox<String> queryDropDown = new ComboBox<>();      
    queryDropDown.getItems().addAll("Gene", "Disease");
    queryDropDown.setValue("Select One");
    System.out.println(queryDropDown.getValue());


    query.getChildren().addAll(new Label("Select Category: "), queryDropDown);


    FlowPane userInput = new FlowPane();            
    userInput.setPadding(new Insets(30,30,30,30));
    userInput.setHgap(10);
    userInput.setVgap(20);

    TextField searchField = new TextField();    
    searchField.setPrefColumnCount(3);
    userInput.getChildren().addAll(new Label("Enter Query: "), new TextField());




    FlowPane searchButtonPane = new FlowPane();         

    searchButtonPane.setPadding(new Insets(30,30,30,200));
    searchButtonPane.setHgap(50);
    searchButtonPane.setVgap(50);
    Button searchButton = new Button("Search");

    searchButtonPane.getChildren().addAll(searchButton);

    ButtonHandlerClass handler1 = new ButtonHandlerClass();     
    searchButton.setOnAction(handler1);


    mainPane.getChildren().addAll(query, userInput, searchButtonPane);      

    Scene scene = new Scene(mainPane, 300, 250);
    primaryStage.setTitle("Genetic Database");
    primaryStage.setScene(scene);
    primaryStage.show();





}


public static void main(String[] args) {
    // Prints "Hello, World" to the terminal window.
    System.out.println("Hello, World");
    Application.launch(args);



}

}

这是按钮处理程序类

public class ButtonHandlerClass implements EventHandler<ActionEvent> {

@Override
public void handle(ActionEvent e) {
    System.out.println("Button Clicked");



}

}

我希望能够使用相同的“搜索”按钮执行不同的操作,具体取决于用户在组合框中选择的选项.我尝试过类似于组合框的ButtonHandlerClass.任何意见,将不胜感激.

谢谢!

解决方法:

为方法1和方式3保留此事件处理程序:

           // Using an event handler
            EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
                    System.out.println(selectedItem);
                    // ...code
                }
            };

方式1:

        //note that every time one action event handler is called
        //instead for multiple handlers you can use the way 3
        searchButton.setOnAction(handler);

方式2(避免创建匿名类):

    //using lambda expression
    searchButton.setOnAction(a->{
        String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
        System.out.println(selectedItem);
        // ...code      
    });

方式3:

(您可以添加多个事件处理程序,并且将调用所有事件处理程序)(例如,您可以添加多个操作事件处理程序…)

[这是你不能用方式1和方式2]:

    //adding an event handler
    searchButton.addEventHandler(ActionEvent.ACTION,handler);

    //or

    searchButton.addEventHandler(ActionEvent.ACTION,a->{
        String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
        System.out.println(selectedItem);
        // ...code      
    });

方式4:

使用外部类(不推荐,除非你有太多的代码,你不希望你当前的类太多行);()

这可以是不同的情况:

>该类将嵌套到您当前的类中?
> ComboBox是全局变量,可以从嵌套中访问
类?
>如果!2那么你必须在构造函数上传递它的引用
外部类
>有一百万种情况

//情况3代码:

 public class ButtonHandlerClass implements EventHandler<ActionEvent> {

   ComboBox<String> comboBox;

   /**
   *Constructor
   */
   public ButtonHandlerClass(ComboBox comboBox){

     this.comboBox = comboBox;

  }

 @Override
  public void handle(ActionEvent e) {
     String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
            System.out.println(selectedItem);
            // ...code  

  }

 }

}

标签:java,javafx,actionevent
来源: https://codeday.me/bug/20190608/1198636.html