【问题标题】:JavaFX: How to set an action to a ComboBox?JavaFX:如何为 ComboBox 设置操作?
【发布时间】:2017-10-26 16:04:12
【问题描述】:
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));
}

我正在尝试为组合框中的每个单独元素调用一个方法。举个例子:如果有人选择“英镑”,它将调用我编写的 calculatePound 方法并计算英镑转换。我没有做太多的 GUI,所以我试图用它进行更多的练习。

【问题讨论】:

  • 在 dropDown.getValue() 上切换或 elseif。您的听众看起来不正确。

标签: java user-interface javafx combobox


【解决方案1】:

这样做的惯用方法是创建一个类来封装您需要的数据和功能,并用它的实例填充您的组合框。

例如创建一个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();
    }
}

那就做吧

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

现在你需要做的就是

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

您可以在Currency 类中添加其他特定于货币的信息,例如货币符号和格式规则等。

请注意,与使用字符串填充组合框的方法(您需要另一个方法和您的 switch 或 if 语句中的另一种情况)。这种方法还有许多其他好处。

【讨论】:

  • 不依赖于这个对象的.toString(),我们也可以使用一个CellFactory,它将它的文本属性绑定到应用该项目的函数的结果,例如this.textProperty.bind(Bindings.createStringBinding(() -&gt; getItem().getName(), itemProperty())
  • @glglgl 是的,绝对的。我想保持简单(在这种特定情况下,我使用的toString() 方法可能还是有意义的,没有任何 UI 考虑)。
【解决方案2】:

在选择列表中的项目后,您首先需要一个供用户单击的按钮。这个按钮应该调用一个方法:

  1. 确定当前选择了组合框列表中的哪个项目
  2. 调用另一个方法对当前选定的项目执行适当的操作

例如:

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

【讨论】:

  • 使用这个; dropDown.setOnAction(event -&gt; {...} 而不是 button..setOnAction(event -&gt; {...}
  • 我想这也可以。这仅取决于他们是否希望在选择列表中的项目后立即执行操作。
猜你喜欢
  • 1970-01-01
  • 2017-06-15
  • 2014-04-07
  • 2016-11-15
  • 2018-03-30
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 2017-03-22
相关资源
最近更新 更多