这两段程序,就是写监听的方式不一样,准备是让点X时,弹出一个窗口,选择是的话,就会关闭。但是第二个代码,不管选哪个都会关闭import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* Sample application using Frame.

* @author
* @version 1.00 07/06/03
*/
public class TestSwingFrame extends JFrame {/**
* The constructor.
*/
public TestSwingFrame() {   setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  // JOptionPane.showMessageDialog(null, "程序开始运行");
   //JScrollPane jsp = new JScrollPane();
   JTextArea jta = new JTextArea(50, 50);
   //jsp.getViewport().add(jta);
   getContentPane().add(new JScrollPane(jta));   MenuBar menuBar = new MenuBar();
   Menu menuFile = new Menu();
   MenuItem menuFileExit = new MenuItem();   menuFile.setLabel("File");
   menuFileExit.setLabel("Exit");   // Add action listener.for the menu button
   menuFileExit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     TestSwingFrame.this.windowClosed();
    }
   });
   menuFile.add(menuFileExit);
   menuBar.add(menuFile);   setTitle("TestSwing");
   setMenuBar(menuBar);
   setSize(400, 400);   // Add window listener.
   addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {     if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
       TestSwingFrame.this, "真的要退出吗?", "结束程序",
       JOptionPane.OK_CANCEL_OPTION,
       JOptionPane.QUESTION_MESSAGE)) {
      TestSwingFrame.this.windowClosed();
     }    }
   });
}protected void windowClosed() {   System.exit(0);
}
    public static void main(String[] args) {
        TestSwingFrame frame = new TestSwingFrame();
        frame.setVisible(true);
  }
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
 * Sample application using Frame.
 * 
 * @author
 * @version 1.00 07/06/03
 */
public class TestSwingFrame extends JFrame { /**
 * The constructor.
 */
public TestSwingFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JOptionPane.showMessageDialog(null, "程序开始运行");
JScrollPane jsp = new JScrollPane();
JTextArea jta = new JTextArea(50, 50);
jsp.getViewport().add(jta);
getContentPane().add(jsp); MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem(); menuFile.setLabel("File");
menuFileExit.setLabel("Exit"); // Add action listener.for the menu button
menuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestSwingFrame.this.windowClosed();
}
});
 menuFile.add(menuFileExit);
 menuBar.add(menuFile); setTitle("TestSwing");
setMenuBar(menuBar);
setSize(400, 400); // Add window listener.
addWindowListener(new a());
} protected void windowClosed() { System.exit(0);
}
    public static void main(String[] args) {
        TestSwingFrame frame = new TestSwingFrame();
        frame.setVisible(true);
    }
    class a extends WindowAdapter
    {
     public void windowClosing(WindowEvent e) {
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
            TestSwingFrame.this, "真的要退出吗?", "结束程序",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE)) {
           TestSwingFrame.this.windowClosed();
          }
     }
    }
}

解决方案 »

  1.   

    setDefaultCloseOperation
      

  2.   

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JFrame会关闭,无论添加了何种监听
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);JFrame是否关闭取决于监听
      

  3.   

    原始windowClosing方法是会关闭窗口的
    如果叠加了DO_NOTHING_ON_CLOSE效果,则windowClosing,windowClosed之原始效果都会被覆盖,也即不会关闭,可以试试,在第一个程序中去掉System.exit(0);