简体   繁体   English

如何关闭活动的JFrame

[英]How to close the active JFrame

I am trying to find a method that can close the active JFrame. 我正在尝试找到一种可以关闭活动JFrame的方法。

I am unable to use frame.dispose(); 我无法使用frame.dispose(); , as I am declaring the action listener in a toolbar class and the frames I want to close are not static and are declared at runtime. ,因为我在工具栏类中声明了动作侦听器,并且我要关闭的框架不是静态的,而是在运行时声明的。

I have tried using: 我试过使用:

java.awt.Window win[] = java.awt.Window.getWindows(); 
for(int i=0;i<win.length;i++){ 
win[i].dispose(); 
}

and whilst this does work, in certain circumstances it will close more than one window even though only 1 window appears to be open, so frames will flash open and closed many times depending on what actions the user has made. 尽管这样做确实可行,但在某些情况下,即使看起来只有一个窗口打开,它也会关闭多个窗口,因此框架会根据用户执行的操作多次闪烁打开和关闭。

For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles. 对于我来说,完全重新创建我的问题将涉及发布大量与MCVE原则不符的代码。

I am hoping someone will know of a more simple and reliable way of closing the active frame in the mould of acitveframe.dispose(); 我希望有人会知道在acitveframe.dispose();模具中关闭活动框架的更简单可靠的方法。 - which I now is not a real solution!! -我现在不是真正的解决方案!!

What happens if you try to get the Window ancestor of the source of the action event? 如果您尝试获取动作事件的来源的Window祖先,会发生什么? ie,

@Override
public void actionPerformed(ActionEvent actionEvent) {
    Component comp = (Component) actionEvent.getSource();
    Window win = SwingUtilities.getWindowAncestor(comp);
    win.dispose();
}

This won't work if the source is not a Component or if it is not contained within the top level Window of interest. 如果源不是组件,或者它不包含在顶级窗口中,则此方法将无效。


Regarding: 关于:

For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles. 对于我来说,完全重新创建我的问题将涉及发布大量与MCVE原则不符的代码。

I'll bet with a bit of effort you could create and post something that comes close. 我敢打赌,您可以创建和发布接近的内容。

I am hoping someone will know of a more simple and reliable way of closing the active frame 我希望有人会知道关闭活动框架的更简单可靠的方法

In your loop you can add: 在循环中,您可以添加:

if (window.isActive())
   // do something

Or maybe a simpler approach is: 或更简单的方法是:

Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();

Also, assuming your active window is a JFrame, instead of using window.dispose() , I have used code like: 另外,假设您的活动窗口是JFrame,而不是使用window.dispose() ,那么我将使用如下代码:

WindowEvent windowClosing = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
frame.dispatchEvent(windowClosing);

this will simulate the user clicking on the "Close" button which means that any WindowListener you added to the frame will also be executed. 这将模拟用户单击“关闭”按钮,这意味着您添加到框架的任何WindowListener也会被执行。 See Closing an Appplication for more information and ideas. 有关更多信息和想法,请参见关闭应用程序

When you are declaring your JFrames, declre them as final if you cannot use static : 声明JFrame时,如果不能使用static ,请将它们声明为final

final JFrame f = new JFrame();

It would solve the problem. 它将解决问题。

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

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