Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 所选JComboBox选项不强制更改_Java_String_Swing_Jcombobox - Fatal编程技术网

Java 所选JComboBox选项不强制更改

Java 所选JComboBox选项不强制更改,java,string,swing,jcombobox,Java,String,Swing,Jcombobox,在我的代码中,我有一个简单的JComboBox,用于打开或关闭某个特性。JComboBox代码为: public static JComboBox getErrorLoggingOnOrOff(){ String[] options = { "On", "Off" }; JComboBox combo = new JComboBox(options); return combo; } 问题是,无论何时单击“关闭”,它都会将值返回为打开状态。这里调用JComboBox:注

在我的代码中,我有一个简单的JComboBox,用于打开或关闭某个特性。JComboBox代码为:

public static JComboBox getErrorLoggingOnOrOff(){
    String[] options = { "On", "Off" };
    JComboBox combo = new JComboBox(options);
    return combo;
}
问题是,无论何时单击“关闭”,它都会将值返回为打开状态。这里调用JComboBox:注意,我包含了System.out.println()(以及null检查)只是为了检查输出

任何指点都将非常感激,对于为什么会发生这种情况,我们真的感到困惑,毫无疑问,这是一种简单的忽视

你好,雷格

编辑 下面是完整的代码练习

 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);
            }
        }
    });
用于比较字符串,非==

i、 e

这可能是另一个问题:

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")) {
                // ...
            }
    }
}

Place
String option=combo.getSelectedItem().toString()JOptionPane.showConfirmDialog(…)

@Reginald是的,非常确定。没有解决问题。。。但是,我将编辑问题以显示此更改。@Reginald如果您将
errorLoggingStatus
与任何其他字符串进行比较,您将需要执行相同的操作。@Reginald如果这不能解决您的问题,那么问题可能出在代码的其他部分?添加一些调试文本以检查流是否正确?@Reginald Updated answer您可以提供可运行版本的代码吗,还是太长?可能太长了。。。我将寻找其他可能导致问题的练习@巴卡洛把这个操作的所有代码都放在上面,无视我之前的评论。顺便说一下,感谢您的关注。Place
String option=combo.getSelectedItem().toString()
if(r==JOptionPane.OK\u选项)
之后。我认为您在选择任何内容之前就获得了“selectedItem”。
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")) {
                // ...
            }
    }
}