简体   繁体   English

摆动JDialog错误?

[英]Swing JDialog bug?

I'm trying to create my own dialog by extending the JDialog class this is the code i used to start: 我试图通过扩展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);
    }
}

when i try to run the code it works fine but i'm getting the following exception: 当我尝试运行代码时,它工作正常,但出现以下异常:

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

i read that there were problems with WINDOWS_EXIT or something like that but the parameter i pass should do the job. 我阅读到WINDOWS_EXIT或类似问题,但我通过的参数应该可以完成工作。 the thing that makes it even weirder is that when i change my class so it will contain a JDialog field instead of extending it, it seems to work just fine. 使它变得更奇怪的是,当我更改类时,它将包含一个JDialog字段而不是对其进行扩展,它似乎正常工作。 I asked a friend to test this on his computer and the code did not throw the exception, he is using jre version 1.6.022 and I'm using 1.6.022 both of us are using 64 bit. 我请一个朋友在他的计算机上对其进行测试,并且代码未引发异常,他正在使用jre版本1.6.022,而我正在使用1.6.022,我们俩都在使用64位。

so what did i do wrong? 所以我做错了什么? or is that a bug in the JRE? 还是JRE中的错误?

Edit: forgot to mention, I'm using eclipse 编辑:忘了提,我正在使用eclipse
Edit2: i tried the same code in Netbeans and it works fine, what could be my problem?? Edit2:我在Netbeans中尝试了相同的代码,并且工作正常,这可能是我的问题吗?

All the methods you call in constructor should be called on the EDT thread . 您在构造函数中调用的所有方法都应在EDT线程上调用 It is not advised to do it inside the constructor, but if you insist make sure it runs on Swing (EDT) thread such as: 不建议在构造函数中执行此操作,但是如果您坚持要确保它在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);
             }
         });
    }
}

IMO the best way do accomplish it would be to move this into separate method and then call it after creating your ColorManager instance. IMO做到这一点的最佳方法是将其移到单独的方法中,然后在创建ColorManager实例后调用它。

When using Swing you should always adhere to Swing threading rules. 使用Swing时,您应始终遵守Swing线程规则。 More information can be found at 可以在以下位置找到更多信息

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

All the above solution are great, I also had very disturbing moment with show the JDialog . 上面的所有解决方案都很棒,展示JDialog也让我感到非常不安。

On NETBEAN 8.2 just left click the JFrame and choose properties, then set the defaultCloseOperation Property ... Usually the first on the list, 在NETBEAN 8.2上,左键单击JFrame并选择属性,然后设置defaultCloseOperation属性...通常是列表中的第一个,

do the same for the JDialog JDialog做同样的事情

... Anyway that,s my own experience ...无论如何,这是我自己的经历

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM