高手们 java swing中点击按钮后弹出JFrame窗口,关闭弹出的窗口,程序都关了如何解决啊!

2024-12-04 10:28:50
推荐回答(3个)
回答1:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TestFrame {

public static void main(String[] args) {

JFrame frame=new JFrame();
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int value=JOptionPane.showConfirmDialog(null, "确定要关闭吗?");
if (value==JOptionPane.OK_OPTION) {
System.exit(0);
}
}

});
}

}

回答2:

给Frame添加一个窗口监听,在实现的windowClosing方法中调用dipose()
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dipose();
}

回答3:

关闭JFrame用dispose()函数关闭 用System,exit(0)是关闭整个程序