选择JComboBox选项不会产生变化

时间:2015-06-04 12:58:26

标签: java string swing jcombobox

在我的代码中,我有一个简单的JComboBox,用于打开或关闭某个功能。 JComboBox代码是:

public static JComboBox getErrorLoggingOnOrOff(){
    String[] options = { "On", "Off" };
    JComboBox combo = new JComboBox(options);
    return combo;
}

问题在于,无论何时单击“关闭”,它始终会将值返回为开启状态。这里调用JComboBox:请注意我已经包含System.out.println()(以及检查null)只是为了检查输出

String option = combo.getSelectedItem().toString();
....
if(option.equals("On")){
    System.out.println("On selected");
    return;
}
else if(option.equals("Off")){
    System.out.println("Off selected");
    return;
}
else {
    System.out.println("Null value for option.... :-/");
}

任何指针都会非常感激,真正感到困惑的是为什么会发生这种情况,无疑是一种简单的过度。

此致,Reg。

修改 这是完整的代码

 final JMenuItem enableErrorLogging = new JMenuItem("Enable Error Logging");
    enableErrorLogging.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK));
    enableErrorLogging.setMnemonic(KeyEvent.VK_M);
    menu.add(enableErrorLogging);
    enableErrorLogging.addActionListener( new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
            if (ui.getWorkspace().getCurrentProject() == null) {
                openProjectMessagePane();
                return;
            }

            JComboBox combo = getErrorLoggingOnOrOff();
            JPanel panel = new JPanel(new GridLayout(0, 1));
            panel.add(new JLabel("Turn Error Logging On Or Off"));
            panel.add(combo);
            String option = combo.getSelectedItem().toString();

            try{

            int r = JOptionPane.showConfirmDialog(frame, panel, "Enable Error Logging?",
                    JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (r == JOptionPane.OK_OPTION) {
            if(option.equals("On")){                 
                System.out.println("On selected");
                return;
            }
            else if(option.equals("Off")){
                System.out.println("Off selected");
                return;
            }
            else{
                System.out.println("Null value for option.... :-/");
            }
                }
            } catch (Exception ex) {
                LOGGER.debug(ex);
            }
        }
    });

1 个答案:

答案 0 :(得分:2)

使用.equals比较字符串,不= =

if(option.equals("On")) {
    // ...
}
else if(option.equals("Off")) {
    // ...
}

这可能是另一个问题:

JComboBox combo = getErrorLoggingOnOrOff();
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("Turn Error Logging On Or Off"));
panel.add(combo);

// Here, you seem to be getting the 'selected item' before even
// showing it using a JOptionPane, which I believe will be null by default
String option = combo.getSelectedItem().toString();

try {
    int r = JOptionPane.showConfirmDialog(frame, panel, "Enable Error Logging?",
        JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (r == JOptionPane.OK_OPTION) {
            if(option.equals("On")) {
                // ...
            }
    }
}

致电String option = combo.getSelectedItem().toString();后放置JOptionPane.showConfirmDialog(...)