gpt4 book ai didi

java - 如何根据 JavaFX 中的组合框选择对按钮执行多个操作

转载 作者:搜寻专家 更新时间:2023-11-01 02:03:14 25 4
gpt4 key购买 nike

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

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
}
};

方式一:

        //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
});

方式三:

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

[这是方式 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
});

方式四:

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

这里可以有不同的情况:

  • 该类将嵌套到您当前的类?
  • 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 中的组合框选择对按钮执行多个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40499042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com