如何在关闭另一个JFrame后打开一个新的JFrame并运行我的游戏?

时间:2015-06-30 21:40:37

标签: java jframe minesweeper

我正在制作一个扫雷游戏,我希望用户能够在玩游戏之前从初学者,中级和高级中选择。到目前为止,我所拥有的是一个JFrame,当我用每个难度的按钮打开程序时会打开它。

这是我的主要功能和选择难度的功能。我想知道在调用chooseGameDifficulty之后我仍然可以调用main函数中的所有函数,因为现在它们没有被调用。

public static void main(String[] args) {
    chooseGameDifficulty();
    game.initGame();
    initBoard();
    getClick();

    while (gameOver == false) {
        showUserTile();
        checkGameWon();
    }
}



  public static void chooseGameDifficulty() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame chooseDifficulty = new JFrame("Minesweeper");
    chooseDifficulty.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    chooseDifficulty.setPreferredSize(new Dimension(500,500));
    chooseDifficulty.setLayout(new GridLayout(1, 3));
    JButton B = new JButton("Beginner");
    JButton I = new JButton("Intermediate");
    JButton E = new JButton("Expert");
    chooseDifficulty.add(B);
    chooseDifficulty.add(I);
    chooseDifficulty.add(E);
    B.addMouseListener(
        new MouseListener() {
            public void mouseClicked(MouseEvent event) {
            game.setGameDifficulty("Beginner");
            chooseDifficulty.setVisible(false);
            }
            public void mouseExited(MouseEvent event) {
            }
            public void mouseEntered(MouseEvent event) {
            }
            public void mouseReleased(MouseEvent event) {
            }
            public void mousePressed(MouseEvent event) {
            }
        }
    ); 


    // same thing for the other buttons

    chooseDifficulty.pack();
    chooseDifficulty.setLocationRelativeTo(null);
    chooseDifficulty.setVisible(true);
}

1 个答案:

答案 0 :(得分:0)

  1. 请勿在选择难度框架上作为默认操作关闭。
  2. 我会建议你使用JDialog来选择难度popoup