我怎样才能停止我的申请?
我有一个主线程,我使用 invokeLater
从中启动一个窗口。我从命令行运行我的应用程序。因此,当应用程序运行时,我会看到该窗口,并且我的命令行被应用程序“阻止”。
我可以通过关闭窗口(因此命令行不受阻止)或在命令行中键入 Ctrl-C(因此窗口消失)来停止应用程序。
我希望能够通过单击应用程序窗口中的按钮来停止应用程序。我为此使用了 setVisible(false) 。但这样我只能部分地实现目标。我的窗口确实消失了,但命令行仍然被阻止。所以,该软件仍在运行。
好吧,我认为这是因为其他一些线程仍在运行。但是我如何轻松关闭所有这些线程(就像我手动关闭应用程序窗口一样)。
I have the main thread from which I start a window using invokeLater
. I run my application from command line. So, when application is running I see the window and my command line is "blocked" by the application.
I can stop the application either by closing the window (as a result the command line is unblocked) or by typing Ctrl-C
in the command line (as a result the window disappear).
I wanted to be able to stop the application by clicking on a button in the window of the application. I used setVisible(false)
for that. But in this way I can achieve the goal only partially. My window really disappear but the command line is still blocked. So, the software is still running.
Well, I assume it's because some other threads are still running. But how can I easily close all these threads (like I do by closing the window of the application manually).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您显示的是 JFrame,您可以告诉它在框架关闭时退出应用程序 - 默认情况下只是隐藏框架:
如果用户关闭窗口(右上角的 [x] 按钮),这将退出应用程序经常),此外,您还可以有一个退出按钮,其事件处理程序使用 myFrame.dispose(); 关闭窗口
If it's a JFrame you're showing, you can tell it to exit the app when the frame is closed - the default is to just hide the frame:
This will exit the app if the user closes the window (top right [x] button often) , in addition you could have a Quit button whose event handler closes the window using
myFrame.dispose();
您必须完成所有线程才能停止您的应用程序。仅隐藏 GUI 不会完成 AWT 线程。查看您使用的 GUI 类的 API,例如 dispose-methods。
You must finish all threads in order to stop your application. Just hiding the GUI will not finish the AWT-Thread. Have a look at the API of the GUI classes you use like the dispose-methods.
尝试 Window.dispose()
Try Window.dispose()