希望你能帮忙。
我希望这不仅仅是一个错误,我们可以解决它。
我目前有一个带有JComboBox
. 当用户更改组合框中的选定项目时,会JOptionPane
出现一个以允许用户确认更改。
在组合框中进行新选择时,会JOptionPane
根据需要显示,但您必须单击它两次才能使用它。也就是说,单击一次以获得焦点,然后单击要使用的按钮。另一种方法是在程序的 GUI 范围内(在 optionPane 后面)单击,然后单击按钮。
没有异常发生,单击按钮后程序正常运行。
此功能仅在使用Nimbus
LookAndFeel 时出现,而不是在本机 macos laf 中出现(在 mac 上构建,尚未在 windows 上测试),但出于其他原因我需要使用 nimbus。
我一直在查看Nimbus 问题跟踪,但尚未找到故障。
我有 JButton ,它调用相同的代码(即JOptionPane.showConfirmDialog(...
),它工作得很好,只是当它从组合框的动作中调用时。
真心希望能帮到你!提前干杯!
import javax.swing.UIManager.*;
import javax.swing.*;
public class TestJavaProblem extends JFrame {
JComboBox jComboBox1;
public TestJavaProblem() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
initComponents();
}
private void initComponents() {
jComboBox1 = new JComboBox();
//give it some values
jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "1", "2"}));
//add listener
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) { fireTask(evt);}
});
this.add(jComboBox1);
pack();
}
private void fireTask(java.awt.event.ItemEvent evt) {
if (evt.getStateChange() == 1) { //so dialog fires only once
int i = JOptionPane.showConfirmDialog(jComboBox1, "Message Text", "Title", JOptionPane.OK_CANCEL_OPTION);
System.out.println("Result:" + i);
}
}
public static void main(String args[]) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {/*no nimbus*/}
new TestJavaProblem().setVisible(true);
}
}