Java 编写JFrame窗体右上角红色打叉关闭按钮的自定义事件

2024-12-15 11:14:58
推荐回答(1个)
回答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);
}
}

});
}

}