关闭模态JDialog后,为什么我的JFrame移到后台

问题描述

遇到这个问题,找不到自己解决的办法之后,我很失落。

说明:

我有一个带有Java Swing GUI的应用程序。与GUI的所有通信都是通过自定义事件完成的,这些事件大多由主程序创建,然后由控制器处理以控制GUI。

我遇到的一个事件将触发Controller打开一个自定义的Modal JDialog,它的作用就像“请稍候,正在处理后台的东西”。因此,在后台任务完成后,一个事件将触发该对话框进行处理,以使主GUI框架再次可访问。

问题:

放置对话框后,主框架将神奇地设置为背景。没有最小化,不是完全在所有打开的窗口的背景下,而是实际上在最后一个打开的窗口的背景下。我完全不知道这种情况的发生方式和原因。

Controller类的重要部分看起来像这样:

class Controller {
    private ModalWaitDialog dialog;
    private JFrame frame;

    private void createMainFrame() {

        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                // create frame
                frame = new JFrame();
                // make the frame visible
                frame.setVisible(true);
            }
        });
    }

    private void showWaitPopup(String msg) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                modalWaitDialog = new ModalWaitDialog(frame,message);
                modalWaitDialog.showDialog();
            }
        });
    }

    // after this was executed,my frame will be set in the background
    private void endWaitPopup() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                if (modalWaitDialog != null) {
                    modalWaitDialog.dispose();
                    modalWaitDialog = null;
                }
            }
        });
    }
}

我知道这不是MCVE,但也许有人知道这种情况的发生方式和原因。

编辑:

我在Frame中添加了WindowFocusListener,并在endWaitPopup中添加了一些打印语句,并调用了invokeAndWait以进一步了解正在发生的情况。结果如下:

private void endWaitPopup() {
    System.out.println("In EndWaitPopup");
    try {
        SwingUtilities.invokeAndWait(new Runnable() {

            public void run() {
                System.out.println("EndWaitPopup... run started");
                // implement as if it could be called if no wait popup is available
                if (modalWaitDialog != null) {
                    System.out.println("Disposing the modal dialog");
                    modalWaitDialog.dispose();
                    modalWaitDialog = null;
                }
                System.out.println("End of EndWaitPopup");
            }
        });
    } catch (InvocationTargetException | InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

输出:

In EndWaitPopup
+++Focus of Frame lost+++
EndWaitPopup... run started
Disposing the modal dialog
End of EndWaitPopup

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...