在任务栏中显示JDialog无法正常工作

Nik*_*hil 6 java swing jdialog

我正在使用下面的代码在任务栏上显示JDialog,并且完全在JDK 1.6中工作.

public class test8 {   
    public static void main(String[] args) {   
        Runnable r = new Runnable() {   
            public void run() {
                JDialog d = new JDialog((Frame)null,Dialog.ModalityType.TOOLKIT_MODAL);   
                d.setTitle("title");  
                d.setSize(300,200);  
                d.setVisible(true);  
                System.exit(0);   
            }
        };
        EventQueue.invokeLater(r);   
    }  
}   
Run Code Online (Sandbox Code Playgroud)

但是当我使用该方法设置模态类型时,它无法正常工作

public class test8 {   
    public static void main(String[] args) {   
        Runnable r = new Runnable() {   
            public void run() {
                JDialog d = new JDialog();   
                d.setTitle("title");  
                d.setSize(300,200); 
                d.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL); 
                d.setVisible(true);  
                System.exit(0);   
            }  
        };   
        EventQueue.invokeLater(r);   
    }  
}   
Run Code Online (Sandbox Code Playgroud)

这两个代码有什么区别?有没有办法用这个方法来解决这个问题?

Hol*_*ger 6

问题是JDialog如果所有者是null出于历史原因,某些构造者会创建一个虚拟框架所有者.但是,Dialog 必须没有所有者像顶级窗口一样可见.即

JDialog d=new JDialog((Window)null);
d.setModalityType(ModalityType.TOOLKIT_MODAL);
d.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

将工作.