选择的 JComboBox 选项未强加更改
JComboBox Option Selected Not Imposing Change
在我的代码中,我有一个简单的 JComboBox,用于打开或关闭某个功能。 JComboBox 代码是:
public static JComboBox getErrorLoggingOnOrOff(){
String[] options = { "On", "Off" };
JComboBox combo = new JComboBox(options);
return combo;
}
问题是,无论我何时单击 "Off.",此值始终 returns 处于开启状态 JComboBox 在此处被调用:请注意,我已经包含了 System.out.println()(以及空值检查)只是为了检查输出.
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.... :-/");
}
任何指点将不胜感激,对于为什么会发生这种情况真的感到困惑,毫无疑问是一个简单的疏忽。
此致,注册
编辑
这是代码的完整摘录
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);
}
}
});
使用 .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")) {
// ...
}
}
}
在调用 JOptionPane.showConfirmDialog(...)
之后放置 String option = combo.getSelectedItem().toString();
。
在我的代码中,我有一个简单的 JComboBox,用于打开或关闭某个功能。 JComboBox 代码是:
public static JComboBox getErrorLoggingOnOrOff(){
String[] options = { "On", "Off" };
JComboBox combo = new JComboBox(options);
return combo;
}
问题是,无论我何时单击 "Off.",此值始终 returns 处于开启状态 JComboBox 在此处被调用:请注意,我已经包含了 System.out.println()(以及空值检查)只是为了检查输出.
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.... :-/");
}
任何指点将不胜感激,对于为什么会发生这种情况真的感到困惑,毫无疑问是一个简单的疏忽。
此致,注册
编辑 这是代码的完整摘录
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);
}
}
});
使用 .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")) {
// ...
}
}
}
在调用 JOptionPane.showConfirmDialog(...)
之后放置 String option = combo.getSelectedItem().toString();
。