gpt4 book ai didi

java - 如何重写 JFrame 中的 windowsClosing 事件

转载 作者:行者123 更新时间:2023-12-03 05:29:33 26 4
gpt4 key购买 nike

我正在开发一个 JFrame,它有一个按钮可以显示另一个 JFrame。在第二个 JFrame 上,我想重写 WindowsClosing 事件以隐藏此框架,但不关闭所有应用程序。所以我喜欢这样:

在第二个 JFrame 上

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});

private void formWindowClosing(java.awt.event.WindowEvent evt) {
this.dispose();
}

但是当我单击窗口上的x按钮时应用程序仍然关闭。为什么?你能帮我吗?

我无法使用

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

因为我需要再次显示 JFrame,并在第一个 JFrame 的操作过程中添加了一些信息。所以我用属性 visible false 初始化第二个 JFrame。如果我使用 dispose,我会丢失另一个 JFrame 在第二时刻添加的信息。所以我用

private void formWindowClosing(java.awt.event.WindowEvent evt) {
this.setVisible(false);
}

但它仍然继续终止我的整个应用程序。

最佳答案

不要创建新的JFrame,对于新容器使用JDialog,如果你想隐藏JFrame那么最好重写正确的例如 DefaultCloseOperations(JFrame.HIDE_ON_CLOSE),方法 JFrame.EXIT_ON_CLOSE 终止当前 JVM 实例 simlair 作为调用 System.exit(int)

编辑

but it still continue to terminate my entire app. 

1) 那么一定还有另一个问题,您的代码可能调用另一个 JFrameformWindowClosing <> WindowClosing,使用 API 实现的方法

public void windowClosing(WindowEvent e) {

2)我更喜欢DefaultCloseOperations(JFrame.HIDE_ON_CLOSE)

3) 使用JDialog代替JFrame

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

public class ClosingFrame extends JFrame {

private JMenuBar MenuBar = new JMenuBar();
private static JFrame frame = new JFrame();
private static JFrame frame1 = new JFrame("DefaultCloseOperation(JFrame.HIDE_ON_CLOSE)");
private static final long serialVersionUID = 1L;
private JMenu File = new JMenu("File");
private JMenuItem Exit = new JMenuItem("Exit");

public ClosingFrame() {
File.add(Exit);
MenuBar.add(File);
Exit.addActionListener(new ExitListener());
WindowListener exitListener = new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
frame.setVisible(false);
/*int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(1);
}*/
}
};
JButton btn = new JButton("Show second JFrame");
btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
frame1.setVisible(true);
}
});
frame.add(btn, BorderLayout.SOUTH);
frame.addWindowListener(exitListener);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setJMenuBar(MenuBar);
frame.setPreferredSize(new Dimension(400, 300));
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}

private class ExitListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(1);
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
ClosingFrame cf = new ClosingFrame();
JButton btn = new JButton("Show first JFrame");
btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
frame.setVisible(true);
}
});
frame1.add(btn, BorderLayout.SOUTH);
frame1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame1.setPreferredSize(new Dimension(400, 300));
frame1.setLocation(100, 400);
frame1.pack();
frame1.setVisible(true);
}
});
}
}

关于java - 如何重写 JFrame 中的 windowsClosing 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9725311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com