简体   繁体   English

Swing 应用程序中的 Swing 弹出窗口

[英]Swing popup in a Swing Application

I have a Swing UI with certain number of buttons.我有一个带有一定数量按钮的 Swing UI。 One button invokes a separate swing class and a new window is opened with new buttons and other features.一个按钮调用一个单独的 swing class 和一个新的 window 打开新按钮和其他功能。 Now when I am closing this window the original window is also getting closed.现在,当我关闭这个 window 时,原来的 window 也正在关闭。 I think this is because both the classes have impemented我认为这是因为这两个课程都实施了

javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
//some code
}
}

Now how to overcome this?现在如何克服这个? A code example or link will be very helpful.代码示例或链接将非常有帮助。 Thanks谢谢

It is hard to guess without seeing the code.不看代码很难猜到。 Perhaps your JFrame has setDefaultCloseOperation set to close the application when the window is closed (maybe EXIT_ON_CLOSE ).也许您的JFrame已将setDefaultCloseOperation设置为在 window 关闭时关闭应用程序(可能是EXIT_ON_CLOSE )。 Have a look at the java doc for more info on this.查看java 文档了解更多信息。

From the java doc :来自java 文档

EXIT_ON_CLOSE (defined in JFrame ): Exit the application using the System exit method. EXIT_ON_CLOSE (在JFrame中定义):使用System exit方法退出应用程序。 Use this only in applications.仅在应用程序中使用它。

This means that the whole application will close.这意味着整个应用程序将关闭。 You do not want that for the second window.对于第二个 window,您不希望这样。 You just want the window (not the application) to close.您只想关闭 window(不是应用程序)。

SwingUtilities.invokeLater has no relation to an application closing. SwingUtilities.invokeLater与应用程序关闭无关。 Its purpose is to defer the run method invocation to when the GUI thread has finished painting and processing current events.其目的是将run方法调用推迟到 GUI 线程完成绘制和处理当前事件时。

I suggest you to check your code for EXIT_ON_CLOSE as was already suggested and also for System.exit() calls.我建议您按照已经建议的方式检查您的EXIT_ON_CLOSE代码以及System.exit()调用。

To answer the comment, since you set EXIT_ON_CLOSE on both frames, it is the expected behaviour to exit the whole application when you close any of the frames.要回答评论,由于您在两个框架上都设置了EXIT_ON_CLOSE ,因此当您关闭任何框架时,退出整个应用程序是预期的行为。 See Javadoc .请参阅Javadoc

To solve your problem, you need to remove this setDefaultCloseOperation on the second frame.要解决您的问题,您需要在第二帧上删除此setDefaultCloseOperation

I agree with the others, calling your code on the main Swing thread, the EDT or Event Dispatch Thread, by using SwingUtilities.invokeLater has nothing to do with your problem, and in fact is a good habit of getting in to if there's a risk that by not doing this your code could be called off of the EDT.我同意其他人的观点,通过使用SwingUtilities.invokeLater在主 Swing 线程、EDT 或事件调度线程上调用您的代码与您的问题无关,实际上是进入是否存在风险的好习惯如果不这样做,您的代码可能会被 EDT 取消。 In other words, if you're showing your other window from code called in a JButton's ActionListener, then there's no need to use SwingUtilities.invokeLater since the actionPerformed method will be called on the EDT.换句话说,如果您从 JButton 的 ActionListener 中调用的代码中显示您的其他 window,则无需使用SwingUtilities.invokeLater ,因为将在 EDT 上调用 actionPerformed 方法。

Getting back to your problem at hand, I suggest that you make the other window a JDialog -- either modal to the application or not, depending on your need -- rather than a JFrame.回到您手头的问题,我建议您将另一个 window 设置为 JDialog(取决于您的需要是否为应用程序的模式),而不是 JFrame。 Doing this allows you to keep the dialog above the main JFrame and allows you to force the user to deal with your dialog before going back to the main GUI, and prevents the application from closing if the dialog closes.这样做可以让您将对话框保留在主 JFrame 上方,并允许您强制用户在返回主 GUI 之前处理您的对话框,并防止应用程序在对话框关闭时关闭。

And to answer your likely next question, yes, JDialogs can hold complex GUI's, as complex as any JFrame.为了回答您可能的下一个问题,是的,JDialogs 可以容纳复杂的 GUI,与任何 JFrame 一样复杂。

Try using setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) on the parent JFrame.尝试在父 JFrame 上使用setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) will terminate the application when the last JFrame is closed whereas EXIT_ON_CLOSE will terminate the application as soon as that JFrame is closed. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)将在最后一个 JFrame 关闭时终止应用程序,而EXIT_ON_CLOSE将在 JFrame 关闭后立即终止应用程序。

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

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