dispose()不是可以关闭当前容器吗?可为什么我写的一个程序不能关掉当前的JFrame呢?
下面程序的思路是:当重新游戏时,重新建立一个JFrame,同时要关掉原来那个。可是我试了好久,怎么也关不掉。还是我本身这个思路就不对。请大家帮忙!拜谢!import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class Minesweeper2 extends JFrame implements ActionListener {
    int NUM = 9;
    int random;
    JButton[] button = new JButton[NUM];
    JButton bomb = new JButton(); // 有雷的按钮
    int total = 0;// 计算有几次没踩到雷    public Minesweeper2() {
        super("Bomb");        setLayout(new GridLayout(3, 3));        for (int i = 0; i < NUM; i++) {
            button[i] = new JButton();
            button[i].setBackground(Color.gray);
            getContentPane().add(button[i]);
        } // 布局
        setBomb(); // 设置雷        for (int i = 0; i < NUM; i++)
            button[i].addActionListener(this);
        
        dispose(); //关掉当前的JFrame,好像没用
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700, 300);
        setVisible(true);    }    public void setBomb() {
        int randomNum = (int) (Math.random() * 9);
        bomb = button[randomNum];
        random = randomNum;
        System.out.println(random); // 随机生成雷    }    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bomb) {
            bomb.setBackground(Color.black);
            LoseDialog lose = new LoseDialog();
            if (lose.ans == 1)
                System.exit(0);
            else {
                   new Minesweeper2();// 重新游戏              
            }
        }        for (int j = 0; j < NUM; j++) {
            if (e.getSource() == button[j] && j != random) {
                if(button[j].getBackground().equals(Color.gray)){                 
                  total += 1; 
                  button[j].setBackground(Color.white);
                }                      
            }
            if (total == 8) {
                WinDialog win = new WinDialog();
                if (win.ans == 1)
                    System.exit(0);
                else {
                  new Minesweeper2();// 重新游戏
                }            }
        }
    }    public static void main(String[] args) {
        // TODO Auto-generated method stub        BeginDialog begin = new BeginDialog();        JFrame frame = new Minesweeper2();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 300);
        frame.setVisible(true);    }
}
import javax.swing.JOptionPane;
//开始游戏的对话框
public class BeginDialog {
    BeginDialog(){   
        JOptionPane.showMessageDialog(null, "Welcome!", "Message", JOptionPane.INFORMATION_MESSAGE);
    }
}import javax.swing.JOptionPane;
// 成功踩雷的对话框
public class WinDialog {
    int ans;
    WinDialog(){
        Object[] options = {"Yes","No"};
        ans = JOptionPane.showOptionDialog(null, "Win!!! Would you like to play again?", "", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
       
    }
   
}import javax.swing.JOptionPane;
//失败后的对话框
public class LoseDialog {
    int ans;
    LoseDialog(){
        Object[] options = {"Yes","No"};
        ans = JOptionPane.showOptionDialog(null, "BOOoom!!! Would you like to play again?", "",  JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
       
    }

解决方案 »

  1.   

    dispose是释放当前java虚拟机中的进程你程序里的那个dispose应该搁在函数的最后,不然到那行就直接退出了
      

  2.   

    将dispose()放在下面就可以了
    public void actionPerformed(ActionEvent e) { 
            if (e.getSource() == bomb) { 
                bomb.setBackground(Color.black); 
                LoseDialog lose = new LoseDialog(); 
                if (lose.ans == 1) 
                    System.exit(0); 
                else { 
                      dispose();                   
                        new Minesweeper2();// 重新游戏              
                } 
            }