如何检测java中特定窗口何时关闭

时间:2015-06-18 03:30:55

标签: java events window action windowlistener

我对Java很陌生,我已经尝试过这样做,但实际上找不到任何有用的东西。我想知道如何在特定窗口关闭时检测到它?我一直在使用windowClosing()但这适用于任何关闭的窗口,因此如果窗口B关闭,我希望由于窗口A关闭而发生的事件也会发生。仅当窗口A关闭时,我该如何检测?对不起,如果措辞严重,我不知道那么多Java术语。在此先感谢:)

package gui_login;

//awt classes
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

//swing classes
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JButton;

public class windowtest implements ActionListener, WindowListener
{
//get JFrame
    JFrame JFrame = new JFrame();

    public static JFrame frameA = new JFrame("FrameA");
    public static JFrame frameB = new JFrame("FrameB");
    static windowtest windowtest = new windowtest();

public static void main(String[] args) 
{
    windowtest.frames();
}

public void frames()
{
    frameA.setLayout(new FlowLayout());
    frameA.setSize(220, 130);
    frameA.setVisible(true);
    frameA.addWindowListener(this);

    frameB.setLayout(new FlowLayout());
    frameB.setSize(220, 130);
    frameB.setVisible(true);
    frameB.addWindowListener(this);
}

public void windowClosing(WindowEvent e) {
    System.out.println("Yo");
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void actionPerformed(ActionEvent e) {
}
}

1 个答案:

答案 0 :(得分:2)

由于frameAframeB都使用WindowListener(this)的相同实例,因此当任一帧关闭时,会通知您WindowListener({1}} )。

您可以使用WindowEvent#getSource来获取事件的来源,但更简单的解决方案可能是为每个框架提供自己的WindowListener实例,这样,您就不需要了做出假设

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WindowTest {
//get JFrame

    JFrame JFrame = new JFrame();

    public static JFrame frameA = new JFrame("FrameA");
    public static JFrame frameB = new JFrame("FrameB");
    static WindowTest windowtest = new WindowTest();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                windowtest.frames();
            }
        });
    }

    public void frames() {
        frameA.setLayout(new FlowLayout());
        frameA.setSize(220, 130);
        frameA.setVisible(true);
        frameA.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("A is closing");
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("A has closed");
            }

        });

        frameB.setLayout(new FlowLayout());
        frameB.setSize(220, 130);
        frameB.setVisible(true);
        frameB.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("B is closing");
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("B has closed");
            }

        });
    }
}

您之前可能没有看过匿名课程,请查看Anonymous Classes

一般来说,您不再需要实施不能为您的班级提供直接功能的界面(就像您使用ActionListenerWindowListener一样),推理是的,这些接口要求您实现的方法是public,但没有人应该直接调用它们。

这可以让你隐藏"您的类内部的实现细节,并防止未知调用者可能误用

您可能还想查看Nested Classes