关闭 JDialog 会留下一个空框

Closing JDialog leaves an empty frame

我有一个关于 JDialog 的小问题,尽管我做了一切,它在关闭后留下了一个空框。我一直在努力寻找解决方案,不幸的是,这个

daughterWindow.dispatchEvent(new WindowEvent(validation, WindowEvent.WINDOW_CLOSING));

也不

daughterWindow.setVisible(false);
daughterWindow.dispose();

这个也没有帮助我

WindowAdapter adapter = (WindowAdapter)jdialog.getWindowListeners()[0];
adapter.windowClosing(new WindowEvent((Window)jdialog, WindowEvent.WINDOW_CLOSING));

最有可能,因为最后一个抛出 ClassCastException。

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.SwingUtilities$SharedOwnerFrame cannot be cast to java.awt.event.WindowAdapter

这是我的代码,也许有人可以给我提示。

    JDialog daughterWindow = new JDialog();
    daughterWindow.setModal(true);
    daughterWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    daughterWindow.getContentPane().setLayout(new BoxLayout(daughterWindow.getContentPane(), BoxLayout.Y_AXIS));

    UIManager.put("FileChooser.readOnly", Boolean.TRUE);
    JFileChooser open = new JFileChooser();
    File rsc = new File(System.getProperty("user.dir") + "\rsc\");
    if(!rsc.exists()) rsc.mkdir();
    open.setCurrentDirectory(new File(System.getProperty("user.dir") + "\rsc\"));
    open.setDialogTitle("Ordner mit der Datenbank auswählen");
    open.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    if(open.showOpenDialog(daughterWindow) == JFileChooser.APPROVE_OPTION){
        UIManager.put("FileChooser.readOnly", Boolean.FALSE);
        setValidateAccessWindowLayout(open.getSelectedFile());
        daughterWindow.dispatchEvent(new WindowEvent(daughterWindow, WindowEvent.WINDOW_CLOSING));
    } else{
        UIManager.put("FileChooser.readOnly", Boolean.FALSE);
        daughterWindow.dispatchEvent(new WindowEvent(daughterWindow, WindowEvent.WINDOW_CLOSING));
    }

    daughterWindow.setResizable(false);
    daughterWindow.pack();
    daughterWindow.setVisible(true);

And a screen of the problem
提前致谢!

更新: 检查了第三个选项,没有明显不必要的转换,但这也没有帮助。

WindowListener adapter = daughterWindow.getWindowListeners()[0];
adapter.windowClosing(new WindowEvent(daughterWindow, WindowEvent.WINDOW_CLOSING));

您在提供的代码片段中创建了两个视觉元素:

JDialog daughterWindow = new JDialog();

JFileChooser open = new JFileChooser();

两者都定义为模态。这基本上意味着——当一个处于活动状态时,另一个不处于活动状态,甚至没有获得控制权的可能性。

我建议您设置 daughterWindow.setModal(false);

并移动

open.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

daughterWindow.setResizable(false);
daughterWindow.pack();
daughterWindow.setVisible(true);         

if(open.showOpenDialog(daughterWindow) == JFileChooser.APPROVE_OPTION){

更新。从代码中看不出文件对话框是模态的,但根据 Java 文档,它是:

File choosers provide a GUI for navigating the file system, and then either choosing a file or directory from a list, or entering the name of a file or directory. To display a file chooser, you usually use the JFileChooser API to show a modal dialog containing the file chooser. https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html