gpt4 book ai didi

java - 一次只打开一扇 Swing 窗

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:21 28 4
gpt4 key购买 nike

我开发了一个 swing 应用程序,但每次运行应用程序时都会打开一个新窗口。我希望如果一个窗口已经打开,则其他窗口不允许打开。

最佳答案

Here是一个 Java 单一应用实例的例子:

A single instance application is one that only allows for 1 of the application to run no matter how many times the user tries to launch.

另请参阅:A shorter example that does not notify the running instance .

应用程序尝试在特定端口上打开套接字。如果您的应用程序的另一个实例已经在运行,则打开套接字会失败。

这对您来说应该已经足够了,因此您不必使用用于向第一个启动的应用程序注册新应用程序的代码部分。

与向文件系统/注册表/任何东西写入某种标志相比,使用套接字有一个很大的优势:
即使您的应用程序崩溃,它也会被删除。

关于java - 一次只打开一扇 Swing 窗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2528844/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com