我怎样才能停止我的申请?

发布于 2024-08-28 03:16:03 字数 334 浏览 11 评论 0原文

我有一个主线程,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

番薯 2024-09-04 03:16:03
System.exit(0);
System.exit(0);
像你 2024-09-04 03:16:03

如果您显示的是 JFrame,您可以告诉它在框架关闭时退出应用程序 - 默认情况下只是隐藏框架:

 myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

如果用户关闭窗口(右上角的 [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:

 myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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();

冷月断魂刀 2024-09-04 03:16:03

您必须完成所有线程才能停止您的应用程序。仅隐藏 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文