如何检测特定 window 何时在 java 中关闭
How to detect when a specific window is closed in java
我是 Java 的新手,我试过查找它,但找不到任何有用的东西。我想知道如何检测特定 window 何时关闭?我一直在使用 windowClosing() 但这适用于任何已关闭的 window,因此我希望因 window 关闭而发生的事件也会发生如果 window B 关闭。如何仅在 window 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) {
}
}
因为 frameA
和 frameB
使用相同的 WindowListener
实例(this),当任一框架关闭时,您的 WindowListener
会收到通知(both/either).
您可以使用 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");
}
});
}
}
你可能没看过匿名classes,看看Anonymous Classes
一般来说,不再需要您实现不向您的 class 提供直接功能的接口(就像您使用 ActionListener
和 WindowListener
一样),推理也就是说,这些接口需要你实现的方法是public
,但实际上没有人应该直接调用它们。
这允许您 "hide" 在您的 class 内部实现细节,并防止未知调用者可能的误用
您可能还想看看 Nested Classes
我是 Java 的新手,我试过查找它,但找不到任何有用的东西。我想知道如何检测特定 window 何时关闭?我一直在使用 windowClosing() 但这适用于任何已关闭的 window,因此我希望因 window 关闭而发生的事件也会发生如果 window B 关闭。如何仅在 window 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) {
}
}
因为 frameA
和 frameB
使用相同的 WindowListener
实例(this),当任一框架关闭时,您的 WindowListener
会收到通知(both/either).
您可以使用 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");
}
});
}
}
你可能没看过匿名classes,看看Anonymous Classes
一般来说,不再需要您实现不向您的 class 提供直接功能的接口(就像您使用 ActionListener
和 WindowListener
一样),推理也就是说,这些接口需要你实现的方法是public
,但实际上没有人应该直接调用它们。
这允许您 "hide" 在您的 class 内部实现细节,并防止未知调用者可能的误用
您可能还想看看 Nested Classes