2013-10-24 86 views
0

試圖創建一個joptionpane來運行任何想法,爲什麼這不起作用?JOptionPane發生故障並使其工作

public void playerLost() { 

    if(player.getStaminaLevel() < player.getStaminaNeededToMove(Terrain.SAND)) { 

       JOptionPane.YES_NO_OPTION(

     if (JOptionPane.NO_OPTION) { 
      dispose(); 
     } 
     else 
     { 
      game = new Game(); 
      createGridSquarePanels(); 
      update(); 
     } 
} 

我也試過,但說JOptionPane.NO_OPTION不能從int轉換爲字符串

public void playerLost() { 

    if(player.getStaminaLevel() < player.getStaminaNeededToMove(Terrain.SAND)) { 
     int choice = JOptionPane.showConfirmDialog(
     this, 
       "sorry you lost\nWould you like to restart?", 
       JOptionPane.YES_NO_OPTION, 
       JOptionPane.INFORMATION_MESSAGE); 
     if (choice == JOptionPane.NO_OPTION) { 
      dispose(); 
     } 
     else 
     { 
      game = new Game(); 
      createGridSquarePanels(); 
      update(); 
     } 
} 
} 

回答

1

第一個代碼段不編譯的語法是用於使用JOptionPane無效。

在第二一段代碼,你錯過了標題參數

int choice = JOptionPane.showConfirmDialog(this, 
       "sorry you lost\nWould you like to restart?", 
       "Title", 
       JOptionPane.YES_NO_OPTION, 
       JOptionPane.INFORMATION_MESSAGE); 

諮詢JOptionPane javadoc