java - JFrame.HIDE_ON_CLOSE onHide 事件?

标签 java swing jframe windowlistener

我有一个 WorldEditor JFrame,它启动 Game JFrame。但是,当Game关闭时,我不希望它结束​​整个程序,因此我将默认关闭操作设置为HIDE_ON_CLOSE。但是,为了节省资源,我在 Game 运行时暂停 WorldEditor

如何检测 Game 窗口何时隐藏,以便恢复 WorldEditor

最佳答案

为什么不自己隐藏框架而不是使用默认的 HIDE_ON_CLOSE

// inside WindowListener class
public windowClosing(WindowEvent e) {
    yourFrame.setVisible( false );
    // your code here...
}

编辑内容:来自文档:

The default close operation is executed after any window listeners handle the window-closing event. So, for example, assume that you specify that the default close operation is to dispose of a frame. You also implement a window listener that tests whether the frame is the last one visible and, if so, saves some data and exits the application. Under these conditions, when the user closes a frame, the window listener will be called first. If it does not exit the application, then the default close operation — disposing of the frame — will then be performed.

带有工作示例的新编辑:

import java.awt.event.*;
import javax.swing.JFrame;

public class ListenerTest extends JFrame implements WindowListener {

public static void main(String[] args) {
    ListenerTest frame = new ListenerTest();
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.setVisible( true );
}

public ListenerTest() {
    this.addWindowListener( this );
}

public void windowActivated(WindowEvent e) {
    System.out.println(" activated ");
}
public void windowClosed(WindowEvent e){
    System.out.println(" closed ");
}
public void windowClosing(WindowEvent e){
    System.out.println(" closing ");
}
public void windowDeactivated(WindowEvent e){
    System.out.println(" deactivated ");
}
public void windowDeiconified(WindowEvent e){
    System.out.println(" deiconified ");
}
public void windowIconified(WindowEvent e){
    System.out.println(" iconified ");
}
public void windowOpened(WindowEvent e){
    System.out.println(" opened ");
}
}

对此进行测试,以捕获正在触发的内容事件。

关于java - JFrame.HIDE_ON_CLOSE onHide 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17119960/

相关文章:

java - NetBeans 中的 JFrame 中的 JPanel

java - 当语言被覆盖时搜索停用词无法按预期工作

java - 无法使用maven在spring mvc项目中启动服务器

java - Selenium 网络驱动程序 : Able to open Firefox browser but unable to load Url

java - 以编程方式展开子 JMenuItems

java - JTable 渲染器不改变背景

java - 使用 for 循环更改 JLabel 文本

Java 查找最大元素

java - 在打开另一个 JFrame 之前关闭一个 JFrame

java - 将 JFrame 添加到 JFrame,在再次使用第一个 JFrame 之前强制关闭新的 JFrame