简体   繁体   English

JavaFX:如何将动作设置为ComboBox?

[英]JavaFX: How to set an action to a ComboBox?

ComboBox dropDown = new ComboBox();


dropDown.getItems().addAll("Mexican Peso", "Canadian Dollar", "Euro", "British Pound");

dropDown.setOnAction(e -> dropDownMenu());
private void dropDownMenu(ComboBox dropDown){


    }
private void calculatePound() {

    double n1 = Double.parseDouble(input.getText());
    output.setText(String.format("%.2f", n1*.80));
}

I'm trying to call a method for each individual element in the combobox. 我试图为组合框中的每个单独元素调用一个方法。 So for an example: if someone chose "British Pound" it would call the calculatePound method I have written and calculate the British Pound conversion. 举个例子:如果有人选择了“英镑”,它将调用我编写的calculatePound方法并计算英镑换算。 I haven't done much GUI so I'm attempting to get more practice with it. 我没有做太多的GUI,所以我正尝试进行更多的练习。

The idiomatic way to do this is to create a class encapsulating the data and functionality you need, and to populate your combo box with instances of it. 惯用的方法是创建一个封装所需数据和功能的类,并用其实例填充组合框。

Eg Create a Currency class: 例如,创建一个Currency类:

public class Currency {

    private final double exchangeRate ;
    private final String name ;

    public Currency(String name, double exchangeRate) {
        this.name = name ;
        this.exchangeRate = exchangeRate ;
    }

    public double convert(double value) {
        return value * exchangeRate ;
    }

    public String getName() {
        return name ;
    }

    @Override
    public String toString() {
        return getName();
    }
}

Then do 然后做

ComboBox<Currency> dropDown = new ComboBox<>();
dropDown.getItems().addAll(
    new Currency("Mexican Peso", 18.49),
    new Currency("Canadian Dollar", 1.34),
    new Currency("Euro", 0.89),
    new Currency("British Pound", 0.77)
);

And now all you need to do is 现在您需要做的就是

dropDown.setOnAction(e -> {
    double value = Double.parseDouble(input.getText());
    double convertedValue = dropDown.getValue().convert(value);
    output.setText(String.format("%.2f", convertedValue));
});

You could add other currency-specific information to the Currency class, such as a currency symbol and formatting rule, etc. 您可以将其他特定于货币的信息添加到Currency类,例如货币符号和格式规则等。

Note how much easier it is to add more currencies with this approach (all you need is another new Currency(name, rate) in the dropDown.getItems().addAll(...) call), compared to the approach of populating the combo box with strings (where you would need another method and another case in your switch or if statement). 请注意,与dropDown.getItems().addAll(...)货币的方法相比,用这种方法添加更多的货币要容易得多(您所需要的只是dropDown.getItems().addAll(...)调用中的另一个new Currency(name, rate) dropDown.getItems().addAll(...)带有字符串的组合框(在开关或if语句中,您将需要其他方法和其他情况)。 There are many other benefits to this approach. 这种方法还有许多其他好处。

You'll first need a button of some sort for the user to click on after selecting an item in the list. 首先,您需要某种按钮供用户在列表中选择一个项目后单击。 This button should call a method that: 此按钮应调用以下方法:

  1. Determines which item in the combobox list is currently selected 确定组合框列表中当前选中的项目
  2. Calls another method to perform the appropriate action on the current selected item 调用另一个方法以对当前所选项目执行适当的操作

For example: 例如:

        ComboBox dropDown = new ComboBox();
        dropDown.getItems().addAll("Mexican Peso", "Canadian Dollar", "Euro", "British Pound");

        Button button = new Button();
        button.setOnAction(event -> {
              //Call a method to determine which item in the list the user has selected
              doAction(dropDown.getValue().toString()); //Send the selected item to the method
        });


  private void doAction(String listItem) {
        switch (listItem) {
              case "Mexican Peso": //Action for this item
                    break;
              case "Canadian Dollar": //Action for this item
                    break;
              case "Euro": //Action for this item
                    break;
              case "British Pound": calculatePound();
                    break;
              default: //Default action
                    break;
        }
  }

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

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