Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关闭另一个JFrame后,如何打开新的JFrame并运行游戏?_Java_Jframe_Minesweeper - Fatal编程技术网

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

Java 关闭另一个JFrame后,如何打开新的JFrame并运行游戏?,java,jframe,minesweeper,Java,Jframe,Minesweeper,我正在制作一个扫雷游戏,我希望用户能够在玩游戏之前从初学者、中级和高级中进行选择。到目前为止,我有一个JFrame,当我打开程序时,它会打开,每个困难都有按钮 下面是我的主要函数和选择函数的难度。我想知道在调用choosegameedefestion之后,我怎么还能调用main函数中的所有函数,因为现在它们没有被调用 public static void main(String[] args) { chooseGameDifficulty(); game.initGame();

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

下面是我的主要函数和选择函数的难度。我想知道在调用choosegameedefestion之后,我怎么还能调用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);
}
  • 在“选择难度”框中,不要在关闭时退出默认操作
  • 我会建议您使用JDialog来选择难度分组

  • 这里有一种安排Swing组件和模型/视图/控制器类的方法。