我做了个示例程序,你可作为参考:import java.awt.event.*;
import javax.swing.*;class Test extends JFrame implements ActionListener
{
 JButton buttonExit;
 
 Test()
 {
  super("Exit test");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  JPanel pane=new JPanel();
  buttonExit=new JButton("退出");
  buttonExit.addActionListener(this);
  pane.add(buttonExit);
  setContentPane(pane);
  setSize(200,200);
  show();
 }
 
 public static void main(String[] arguments)
 {
  Test test=new Test();
 }  
 
 public void actionPerformed(ActionEvent evt)
 {
  Object obj=evt.getSource();
  if(obj instanceof JButton)
    {
     JButton source=(JButton)obj;
     
     //注意下面的代码可能是你想要的,实现询问对话框和处理
     if(source==buttonExit)
       {
        int response;
        response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
        if(response==JOptionPane.YES_OPTION)
          {
           System.exit(0);
          }
        }  
    }
  }
}

解决方案 »

  1.   

    我想任何窗体应该都有onClose()事件吧
      

  2.   

    jbuilder中控制关闭窗体的代码是什么,怎么找不到
      

  3.   

    把JFrame放入窗体程序中。修改那个程序就OK了。你不要在JFrame中写  
              if(response==JOptionPane.YES_OPTION)
              {
               System.exit(0);
              }
    而在他的父窗体中
      

  4.   

    我用下列代码来控制右上角的关闭按钮,但无论选择JOptionPane的拿一个选项,窗体都关闭
    void this_windowClosing(WindowEvent e) {
        int response;
        response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
        if(response==JOptionPane.YES_OPTION)
          {
           System.exit(0);
          }
    }
      

  5.   

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JFrame;
    import javax.swing.JOptionPane;public class Temp
    {
        public static void main(String[] args)
        {
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            frame.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    int select = JOptionPane.showConfirmDialog(frame, "真的要退出吗?");
                    if (select == JOptionPane.YES_OPTION)
                        frame.dispose();
                }            public void windowClosed(WindowEvent e)
                {
                    System.exit(0);
                }
            });
            frame.setSize(320, 240);
            frame.setLocation(240, 180);
            frame.setVisible(true);
        }
    }
      

  6.   

    根据你的要求处理右上角的关闭按钮,并且解决你上面刚提出的问题,我又写了一遍程序如下:import java.awt.event.*;
    import javax.swing.*;class Test extends JFrame implements ActionListener
    {
     JButton buttonExit;
     
     Test()
     {
      super("Exit test");
      
      //我又改动了下面这句:
      setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
       
      JPanel pane=new JPanel();
      buttonExit=new JButton("退出");
      buttonExit.addActionListener(this);
      pane.add(buttonExit);
      setContentPane(pane);
      setSize(200,200);
      show();
     }
     
     public static void main(String[] arguments)
     {
      Test test=new Test();
      ExitWindow exit=new ExitWindow();
      test.addWindowListener(exit);
     }  
     
     public void actionPerformed(ActionEvent evt)
     {
      Object obj=evt.getSource();
      if(obj instanceof JButton)
        {
         JButton source=(JButton)obj;
         
         if(source==buttonExit)
           {
            int response;
            response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
            if(response==JOptionPane.YES_OPTION)
              {
               System.exit(0);
              }
            }  
        }
      }
    }//WindowAdapter实现了WindowListener接口
    class ExitWindow extends WindowAdapter
    {
     public void windowClosing(WindowEvent e)
     {
      int response;
      response=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
      if(response==JOptionPane.YES_OPTION)
          {
           System.exit(0);
          }
     }
    }
      

  7.   

    哦,回完贴才看到楼上已经有人做好了,而且代码比较精练,不过
    他的程序可以改进是部分代码多余:
    public void windowClosed(WindowEvent e)
     {
      System.exit(0);
     }