如何解决关闭模态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 举报,一经查实,本站将立刻删除。