在C#中: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult result = MessageBox.Show("是否确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { e.Cancel = true; } } } 在JavaScript中: Firefox/IE <mce:script type="text/javascript"><!-- window.οnbefοreunlοad=function(event){ var event=event?event:window.event; event.returnValue="真的要关闭页面吗?"; } // --></mce:script> Chrome <mce:script type="text/javascript"><!-- document.body.οnbefοreunlοad=function(event){ return "真的要关闭页面吗?"; } // --></mce:script> Opera 杯具- - Opera11中也许会实现onbeforeunload 在Java中: package gui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class MainFrame extends JFrame { //退出按钮 JButton exitBtn; //保留本框架实例的指针 JFrame thisFrame; public static void main(String[] args) { MainFrame mainFrame=new MainFrame(); mainFrame.pack(); mainFrame.setVisible(true); } public MainFrame(){ thisFrame=this; initGUI(); } private void initGUI(){ exitBtn=new JButton("退出"); exitBtn.setBounds(0, 0, 30, 20); exitBtn.addActionListener(new exitBtnListener()); thisFrame.getContentPane().add(exitBtn); thisFrame.addWindowListener(new windowListener()); //关键!框架将不会真正关闭 thisFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); } //确认关闭 private void sureExit(){ int result=JOptionPane.showConfirmDialog(this, "你确定要退出本窗口不?", "Information", JOptionPane.YES_NO_OPTION); if(result==JOptionPane.NO_OPTION){ return; } //真正执行关闭的语句 thisFrame.dispose(); } //实现接口 class windowListener implements WindowListener{ @Override public void windowClosed(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { //在关闭时调用“确认关闭”方法 sureExit(); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { } @Override public void windowOpened(WindowEvent e) { } @Override public void windowActivated(WindowEvent e) { } } //实现接口 class exitBtnListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //调用“确认关闭”方法 sureExit(); } } }