package org.jfml.test;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class MyApp extends JFrame
{
public MyApp()
{
final Container contentPane = getContentPane();
JButton jbutton = new JButton("Display dialog"); contentPane.add(jbutton, BorderLayout.NORTH); jbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int result =
JOptionPane.showConfirmDialog(
(Component) MyApp.this,
"yes or no",
"yes or no",
JOptionPane.YES_NO_OPTION);
System.out.println("result = " + result);
if (0 == result)
{
MyApp.this.dispose();
}
}
});
} public static void main(String[] args)
{
MyApp app = new MyApp();
app.setBounds(0, 0, 300, 300);
app.setVisible(true); app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
app.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}); }
}

解决方案 »

  1.   

    你根本没在监听方法里边写明窗体关闭动作,当然不会有反应了。应该像Patrick_DK(我有我的调调,就是这么屌)那样写明窗口处理事件的方法。
      

  2.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class MyApp extends JFrame
    {  
     public MyApp()
     {
      final Container contentPane = getContentPane();
      JButton jbutton = new JButton("Display dialog");       
      contentPane.add(jbutton,BorderLayout.NORTH);
      jbutton.addActionListener(new ActionListener()
      {
       public void actionPerformed(ActionEvent e)
       {
        int result = JOptionPane.showConfirmDialog((Component)null,"yes or no","yes or no",JOptionPane.YES_NO_OPTION);
        if (result == 0)
         MyApp.this.dispose();//看这里
       }
      }
      );
     } public static void main(String[] args)
     {
      MyApp app = new MyApp();
      app.setBounds(0,0,300,300);
      app.setVisible(true);      
      app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      app.addWindowListener(new WindowAdapter(){public void WindowClosing(WindowEvent e){System.exit(0);}});
     }
    }