gpt4 book ai didi

显示构造对话框时未选择 Java swing 复选框

转载 作者:行者123 更新时间:2023-12-01 14:01:53 27 4
gpt4 key购买 nike

我正在开发一个应用程序,在该应用程序中我开发了一个带有三个复选框的窗口。当选择第一个和第二个复选框时,将根据需要打开新窗口。第三个复选框用于关闭应用程序。当选择“退出”复选框时,将显示所需的确认对话框,但未勾选“退出”复选框。

我无法在这里找出问题所在。请帮我解决这个问题! enter image description here

封装jcheckbox;

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class InitiaaWindow extends JPanel {
static JFrame frame = new JFrame("Credit Contract Validation");

private static final long serialVersionUID = 1L;
JCheckBox jValidateECOUT;
JCheckBox jValidateSuperDeals;
JCheckBox jEXIT;

JLabel jlbPicture,jlbPicture1;
CheckBoxListener myListener = null;

public InitiaaWindow() {

myListener = new CheckBoxListener();

jValidateECOUT = new JCheckBox("ValidateECOUT");
jValidateECOUT.setMnemonic(KeyEvent.VK_C);
jValidateECOUT.setSelected(false);
jValidateECOUT.addItemListener(myListener);

jValidateSuperDeals = new JCheckBox("ValidateSuperDeals");
jValidateSuperDeals.setMnemonic(KeyEvent.VK_G);
jValidateSuperDeals.setSelected(false);
jValidateSuperDeals.addItemListener(myListener);

jEXIT = new JCheckBox("EXIT");
jEXIT.setMnemonic(KeyEvent.VK_G);
jEXIT.setSelected(false);
jEXIT.addItemListener(myListener);

jlbPicture = new JLabel(new ImageIcon("src/jcheckbox/image.jpg"));
jlbPicture1 = new JLabel(new ImageIcon("src/jcheckbox/image1.jpg"));

JPanel jplCheckBox = new JPanel();
jplCheckBox.setLayout(new GridLayout(0, 1));
jplCheckBox.add(jValidateECOUT);
jplCheckBox.add(jValidateSuperDeals);
jplCheckBox.add(jEXIT);

setLayout(new BorderLayout());
add(jplCheckBox, BorderLayout.WEST);
add(jlbPicture1, BorderLayout.CENTER);
add(jlbPicture, BorderLayout.EAST);
setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
}
class CheckBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {

if (jValidateECOUT.isSelected())
{
try {
UIPack.UI.myMethod(null);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if (jValidateSuperDeals.isSelected())
{
try {
ValidateSuperDealsUIPack.UI.ValidateSuperDealsUI(null);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if (jEXIT.isSelected())
{
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null, "Would you like to close the application", "Conformation message",dialogButton);
if(dialogResult==0)
System.exit(1);
else
JOptionPane.getRootFrame().dispose();
}
}
}

public static void main(String s[]) {
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new InitiaaWindow());
frame.pack();
frame.setVisible(true);
}
}

最佳答案

由于 showConfirmDialog 创建模式对话框,直到对话框关闭为止,项目选择事件的执行可能不会传播到可能负责更新复选框显示的其他监听器。

如果您在对话框中单击“否”,复选框是否会正确更新?如果是,那么您可以使用 SwingUtilities.invokeLater 在单独的可运行任务中创建对话框,这将确保选择事件的当前处理在打开模式对话框之前完成。

关于显示构造对话框时未选择 Java swing 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19292738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com