简体   繁体   English

用一个按钮javafx以干净的方式进行多项操作

[英]Multiple actions with one button javafx in clean way

I am wondering if there is a way to use single JavaFX Button and perform different actions on this single Button depending on some another condition (example: perform action one when clicked if option one was chosen, action two when option two etc.) in clean way. 我想知道是否有一种方法可以使用干净的JavaFX Button并根据其他条件对单个Button执行不同的操作(例如:如果选择了选项1,则单击时执行一个动作,选择选项二时执行第二个动作,依此类推)方式。

I could make it with if statements, that is obvious, but I would prefer some decent and clean way to do that. 我可以使用if语句来做到这一点,但这很明显,但是我更喜欢一些体面和简洁的方法来做到这一点。

Could you suggest me some ideas in orded to achieve that? 您能建议我一些想法实现这一目标吗?

Associate the handler to the option. 将处理程序与选项相关联。 You could do eg this using a Map<Option, EventHandler<ActionEvent>> or by simply by implementing functionality in the options themself. 您可以使用Map<Option, EventHandler<ActionEvent>>或仅通过在选项本身中实现功能来进行此操作。

The following example lets the user choose between closing printing Hello World to the console or closing the application in a ComboBox : 以下示例使用户可以选择在控制台上关闭打印Hello World或在ComboBox关闭应用程序:

// handler with a given return value for toString()
public abstract class NamedEventHandler implements EventHandler<ActionEvent> {

    private final String name;

    public NamedEventHandler(String name) {
        this.name = name;
    }

    @Override
    public final String toString() {
        return name;
    }

}
ComboBox<EventHandler<ActionEvent>> comboBox = new ComboBox<>(FXCollections.observableArrayList(
        new NamedEventHandler("Print \"Hello World\"") {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World");
            }
        }, new NamedEventHandler("Close") {
            @Override
            public void handle(ActionEvent event) {
                Platform.exit();
            }
        }
));

Button button = new Button("OK");
button.setOnAction((ActionEvent event) -> {
    EventHandler<ActionEvent> handler = comboBox.getValue();
    if(handler != null) {
        handler.handle(event);
    }
});

Using a Map map to store the event handlers, you'd use 使用Map map来存储事件处理程序,您将使用

EventHandler<ActionEvent> handler = map.get(comboBox.getValue());

instead of 代替

EventHandler<ActionEvent> handler = comboBox.getValue();

I think that you could use a map of methods, something like that: 我认为您可以使用方法图,例如:

Map<String, Method> actions = new HashMap();

Then, get the methods ready to be called: 然后,准备调用这些方法:

Method method1 = classInstance.getClass().getMethod("myMethodOne")
Method method2 = classInstance.getClass().getMethod("myMethodTwo")
...

and populate the map with the methods and their names: 并使用方法及其名称填充地图:

actions.put("methodOne", method1);
actions.put("methodTwo", method2);

So based on the text entered in a TextField (or anything) you could invoke the desired method, something like: 因此,基于在TextField(或其他任何内容)中输入的文本,您可以调用所需的方法,例如:

String nameMethodToInvoke = myTextField.getText();
Method methodToInvoke = actions.get(nameMethodToInvoke);
methodToInvoke.invoke()

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

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