在java中有一个设置窗口关闭的方法,那事件还有什么用处呢?如以下是一个简单的例子.import javax.swing.JFrame;public class SimpleFrame extends JFrame
{
   public SimpleFrame()
   {
      setTitle("Window1");
      setSize(400,300);
      setDefaultCloseOperation(EXIT_ON_CLOSE);        --关闭窗口
      setResizable(false);
      setVisible(true);
   }   public static void main(String[] args)
   {
       JFrame.setDefaultLookAndFeelDecorated(true);
       new SimpleFrame();
   }
}下面是用了事件的
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;public class SimpleFrame extends JFrame
{
   public SimpleFrame(String title)
   {
      super(title);
      setSize(400,300);
      addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent event)
         {
              System.exit(0);
         }
      });
     setResizable(true);
     setVisible(true);
   }
   public static void main(String[] args)
   {
      JFrame.setDefaultLookAndFeelOperation(true);
      new  SimpleFrame("Window1");
   }
}既然有一个简便的直接关闭窗口的方法,那么为什么还要这么一大段关闭窗口的事件程序,那些WindowListener,WindowEvent,WindowAdapter还有什么用处呢?

解决方案 »

  1.   

    WindowAdapter这个虚类里除了有能完成关闭窗口功能的windowClosing方法之外
    还有很多其他方法诸如windowIconified,windowLostFocus等等
      

  2.   

    请注意了你使用的是SWING窗口界面,然而SWT就没有这么好的方法让你调用了,况且,这只是一种方便你实现的简短语句,对于大多数程序来说是可行的,可是可扩展性不强,如果我在关闭界面的同时想先关闭数据库连接,那就必须得自己重写事件监听代码。
      

  3.   

    2楼的你说得很对,在AWT窗口界面下面就没有这个方法,我刚才用AWT界面写的一个测试那个关闭的方法抱错.
    代码如下;
    import java.awt.Frame;
    import java.awt.Button;
    import java.awt.FlowLatout;public class BtnDemo extends Frame
    {
       private Button btn = new Button("按钮");   public BtnDemo()
       {
         setTitle("Window");
         setSize(400,300);
         setDefaultCloseOperation(EXIT_ON_CLOSE);     --抱错
          setLayout(new FlowLayout());
          setVisible(ture);
       }   public static void main(String[] args)
       {
             new BtnDemo();
       }
       
    }程序抱错,而换成swing界面就可以了.