我想问下,当点击窗体关闭按钮时,如何捕获这个讯息?就是说当按下关闭按钮时,我们如何像平时按一个Button一样可以监听,以实现我们想要的效果(比如弹出一个对话框)。

解决方案 »

  1.   

    function delete()
    {
    return  window.confirm("请确定删除?");

    }onclick="del()"function del(){
    //弹出一个框如果用户点确定
    if(delete()){
    //处理逻辑}
    }
      

  2.   


    this.addWindowListener(new WindowAdapter() 

    public void windowClosing(WindowEvent e) 

    int intExit = JOptionPane.showConfirmDialog(this,"请问是否要关闭?","退出",JOptionPane.YES_NO_OPTION);
            if (intExit == 0){
                System.exit(0);
    }} 
    });  试试看行不行
      

  3.   

    一般是添加一个匿名内部类来解决:
       this.addWindowListener(new WindowAdapter(){
               public void closing(WindowEvent e){
                      //此处添加操作
               }
           }   
       );
             
    个人的想法,有不对的地方请见谅
      

  4.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function tijiao(type){
    document.getElementById("ss").value=type;
    alert(type);
    document.forms[0].submit();
    }
    </script></head><body>
    <form id="form1" name="form1" method="post" action="untitled.html">
    <input  id="ss"type="hidden" name="op" />
      <input  type="submit" name="Submit2" value="新建" onclick="tijiao('你点下了新建按钮')" />
      <input   type="submit" name="Submit" value="修改" onclick="tijiao('你点下了更新按钮')" />
    </form>
    </body>
    </html>这是自己前段时间些的小Demo,是对不同按钮实现不同效果的操作。里面缺少一个跳转页面,就是
    action="untitled.html"这里对应的网页,你可以测试看看就明白啦。