繁体   English   中英

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

[英]How to perform multiple actions for a Button depending on ComboBox selection in JavaFX

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

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。 任何意见,将不胜感激。

谢谢!

way 1way 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:

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

[这是你不能用way 1way 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那么你必须在External类的构造函数上传递它的引用
  • 有一百万种情况

    //情况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 } } } 

为什么不使用开关声明?

@Override
public void start(Stage primaryStage) {

    VBox vbox = new VBox();
    ComboBox<String> comboBox = new ComboBox();
    ObservableList<String> options = FXCollections.observableArrayList("one", "two", "three");
    comboBox.setItems(options);
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction((ActionEvent event) -> {
        if(comboBox.getValue() != null)
        {
            String tempString = comboBox.getSelectionModel().getSelectedItem();
            System.out.print(tempString);
            switch(tempString)
            {
                case "one":
                    btn.setText("one is selected in combobox");
                    break;
                case "two":
                    btn.setText("two is selected in combobox");
                    break;
                case "three":
                    btn.setText("three is selected in combobox");
                    break;
                default:
                    btn.setText("Nothing is selected in combobox");
            }
        }
    });

    StackPane root = new StackPane();
    vbox.getChildren().addAll(btn, comboBox);
    root.getChildren().addAll(vbox);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM