2015-06-30 59 views
0

我正在製作掃雷遊戲,我希望用戶在玩遊戲之前能夠從初學者,中級和高級中挑選。到目前爲止,我所擁有的是一個JFrame,當我用每個難度的按鈕打開程序時打開。如何在關閉另一個JFrame後打開新的JFrame並運行我的遊戲?

這裏是我的主要功能,並選擇難度的功能。我想知道在我調用chooseGameDifficulty後,仍然可以調用主函數中的所有函數,因爲現在它們不會被調用。

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); 
} 
+0

下面就來安排你的Swing組件和模型/視圖/控制器類的一種方式,[掃雷小程序(http://java-articles.info /文章/?p = 300)。 –

回答

0
  1. 請在接近默認操作的難度選擇框上沒有退出。
  2. 我會建議你使用的JDialog的選擇難度popoup
相關問題