简体   繁体   English

如何检查是否存在JFrame?

[英]How to check if a JFrame is in existence?

I am making an application where I need to have a Preferences JFrame which can open another JFrame for further preferences of a certain type. 我正在制作一个需要使用Preferences JFrame的应用程序,它可以打开另一个JFrame以获得特定类型的其他首选项。 Though I do not want the further preferences JFrame to be able to have more than one instance open at the same time. 尽管我不希望进一步的偏好,JFrame能够同时打开多个实例。

I have tried using the .isActive() method but that always no matter what happens reports that the variable used to make the new instance of the JFrame is equal to null , which it has been initially set to when it was initialised. 我尝试使用.isActive()方法,但是无论发生什么情况,始终都会报告用于创建JFrame新实例的变量等于null ,该变量最初在初始化时设置为null

So what is the best way of actually checking if a certain JFrame already has an instance in existence? 那么,实际检查某个JFrame是否已经存在实例的最佳方法是什么? Is it to get it to write to a variable when it launches and when it quits? 是要它在启动时和退出时写入变量吗? If so, how would you get it to write to a variable when someone pressed the x button, and what if it crashed and closed not through the standard means? 如果是这样,当有人按下x按钮时,如何将其写入变量?如果不按标准方法崩溃并关闭它,该怎么办?

Or is there another way of doing it like getting the isActive() method to work or another method that would do better? 还是有另一种方法可以使isActive()方法正常工作,还是可以做得更好?

What you should do is to make the JFrame a modal JDialog. 应该做的是使JFrame成为模式JDialog。 Instead of opening a JFrame, you open a JDialog . 无需打开JFrame,而是打开JDialog You pass the other settings window as the parent and pass true for modal . 您将其他设置窗口作为parent传递,并将modal传递给true This will prevent any action in the other settings window as long as this modal dialog is open. 只要此模式对话框处于打开状态,这将阻止其他设置窗口中的任何操作。

Thats what a lot/almost every program uses. 那就是很多/几乎每个程序都使用的东西。 Have you ever noticed that, when you open a settings window and try to click on the main window, the settings window pops back on top and blinks for a second? 您是否曾经注意到,当您打开设置窗口并尝试单击主窗口时,设置窗口会弹出顶部并闪烁一秒钟? Thats what the modal dialog does. 那就是模态对话框的作用。

Why don't you just avoid creating a new JFrame at all? 您为什么不完全避免创建新的JFrame? Just make one earlier in your execution and make it visible when you need it and hide it when you temporarily don't need it anymore. 只需在执行过程中使它更早一些,并在需要时使其可见,而在暂时不再需要时将其隐藏。 Trying to make an already visible JFrame visible or hide a hidden JFrame shouldn't do anything. 试图使已经可见的JFrame可见或隐藏隐藏的JFrame不应执行任何操作。

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

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