我正在尝试通过扩展JDialog类来创建我自己的对话框,这是我用来启动的代码:
import javax.swing.JDialog;
public class ColorManager extends JDialog {
private static final long serialVersionUID = 1L;
public ColorManager(){
super();
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(true);
}
}当我尝试运行代码时,它工作得很好,但我得到了以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE我读到WINDOWS_EXIT或类似的东西有问题,但我传递的参数应该可以完成这项工作。更奇怪的是,当我更改我的类,使其包含一个JDialog字段而不是扩展它时,它似乎工作得很好。我请一个朋友在他的电脑上测试这个,代码没有抛出异常,他使用的是jre版本1.6.022,而我使用的是1.6.022,我们都使用64位。
那么我做错了什么呢?或者这是JRE中的一个bug?
编辑:忘了提一下,我正在使用eclipse
Edit2:我在Netbeans中尝试了相同的代码,它工作得很好,我的问题是什么??
发布于 2011-03-27 01:06:26
您在构造函数中调用的所有方法都应该在EDT线程上调用。不建议在构造函数中执行此操作,但如果您坚持,请确保它在Swing (EDT)线程上运行,例如:
import javax.swing.JDialog;
public class ColorManager extends JDialog {
private static final long serialVersionUID = 1L;
public ColorManager(){
super();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(true);
}
});
}
}要做到这一点,最好的方法是将它移到单独的方法中,然后在创建ColorManager实例后调用它。
在使用Swing时,您应该始终遵循Swing线程规则。有关更多信息,请访问
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
发布于 2018-03-04 23:33:23
以上所有的解决方案都很棒,我在展示JDialog的时候也遇到了很大的困扰。
在NetBEAN8.2上,只需左键单击JFrame并选择属性,然后设置defaultCloseOperation属性...通常排在名单的第一位,
对JDialog执行相同的操作
..。不管怎么说,那是我自己的经验。
https://stackoverflow.com/questions/5442973
复制相似问题