我想在关闭窗口时出现的对话框中点“撤消”时,窗口不退出。
代码如下:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JFrame;
import javax.swing.JOptionPane;public class WindowClosing extends JFrame {
WindowClosing() {
super("关闭窗口");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int selection = JOptionPane.showConfirmDialog(
WindowClosing.this, "Are you sure?", "Logout",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE); if (selection == JOptionPane.OK_OPTION) {
System.exit(0);
}
} });
setSize(300, 200);
setVisible(true);
} public static void main(String[] args) {
new WindowClosing();
}}

解决方案 »

  1.   

    JFrame j=new JFrame();
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      

  2.   

    我试过,可还是不行啊,是不是下面这样写啊:
    //DialogExample.javaimport java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;public class DialogExample {
    public static void main(String[] args) {
    DialogFrame frame = new DialogFrame();
    // frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }class DialogFrame extends JFrame {
    public DialogFrame() {
    setTitle("DialogExample");
    setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); // 建立容器面板
    JPanel buttonPanel = new JPanel(); logoutButton = new JButton("退出");
    // 注册事件监听器
    logoutButton.addActionListener(new LogoutAction());
    buttonPanel.add(logoutButton);
    addWindowListener(new good());
    contentPane.add(buttonPanel, BorderLayout.SOUTH);
    } // 实现事件监听器
    private class LogoutAction implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    // 新建确认型对话框
    int selection = JOptionPane.showConfirmDialog(DialogFrame.this,
    "Are you sure?", "Logout", JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.WARNING_MESSAGE); // 如果选择了“确认”按钮,则退出
    if (selection == JOptionPane.OK_OPTION) {
    System.exit(0);
    }
    }
    } class good extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
    int selection = JOptionPane.showConfirmDialog(DialogFrame.this,
    "Are you sure?", "Logout", JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.WARNING_MESSAGE); // 如果选择了“确认”按钮,则退出
    if (selection == JOptionPane.OK_OPTION) {
    // System.exit(0);
    }
    } } public static final int WIDTH = 200; public static final int HEIGHT = 120; private JButton logoutButton;
    }
      

  3.   

    System.exit(0);
    是结束整个进程..主窗口关闭..JVM推出..
    你可以设置JOptionPane
    setVisiable(false);
    ------
    不就是实现了..
      

  4.   

    我已经知道怎样解决了,谢谢大家了。
    这样写:
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    不是:
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);