如何根据JComboBox选择进行选择

时间:2017-12-13 01:20:34

标签: java

如何根据JComboBox选择激活我的代码。我正在将指标转换为英制,我需要知道何时根据何时选择某个选项来调用方法。

    public JComboBox<String> metricUnits = new JComboBox<String>();

    public JComboBox<String> imperialUnits = new JComboBox<String>();

    imperialUnits.setBounds(140, 65, 100, 20);
    imperialUnits.addItem("Inches");
    imperialUnits.addItem("Feet");
    imperialUnits.addItem("Yards");
    imperialUnits.addItem("Miles");

    metricUnits.setBounds(140, 20, 100, 20);
    metricUnits.addItem("Millimeters");
    metricUnits.addItem("Centimeters");
    metricUnits.addItem("Meter");
    metricUnits.addItem("Kilometer");

1 个答案:

答案 0 :(得分:0)

如果您使用的是Java 8,则可以使用lambda表达式

JComboBox comboBox  = new JComboBox() ;
comboBox.addActionListener((java.awt.event.ActionEvent e) -> {
 // here what do you want to do when change
    });