簡體   English   中英

如何在關閉另一個JFrame之后打開新的JFrame並運行游戲?

[英]How do I open a new JFrame and run my game after closing another JFrame?

我正在制作掃雷游戲,我希望用戶能夠在玩游戲之前從初學者,中級和高級中進行挑選。 到目前為止,我所擁有的是一個JFrame,當我使用每種難度的按鈕打開程序時,該JFrame都會打開。

這是我的主要功能和選擇難度的功能。 我想知道在我調用choiceGameDifficulty之后如何仍然可以在主函數中調用所有函數,因為現在還沒有調用它們。

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. 不要在關閉時退出,這是選擇難度框架上的默認操作。
  2. 我會建議您使用JDialog來選擇難度彈出窗口

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM