2015-11-02 121 views
0

爲什麼確認對話框不起作用?我花了很長時間試圖弄清楚這一點。我得到以下錯誤:JOptionPane確認對話框

PokemonDemo.java:40: error: incompatible types: int cannot be converted to String response = JOptionPane.showConfirmDialog(null, "You are a " + intro.getGender() + ". Is that correct?", JOptionPane.YES_NO_OPTION, response);

我試圖改變響應字符串(是的,我用.equals()方法時,我這樣做),但沒有任何反應。即使程序中沒有int,我仍然會得到錯誤。請讓我知道,如果你需要我的對象的代碼,但我不明白爲什麼它需要在這種情況下。

public static void main(String [] args) 
{ 
    String holder; 
    int response; 

    Pokemon intro = new Pokemon(); 

    JOptionPane.showMessageDialog(null, "Hello there!"); 
    JOptionPane.showMessageDialog(null, "Glad to meet you!"); 
    JOptionPane.showMessageDialog(null, "Welcome to the world of Pokémon. My name is Oak."); 
    JOptionPane.showMessageDialog(null, "People affectionately refer to me as the Pokémon Professor."); 
    JOptionPane.showMessageDialog(null, "For some people, Pokémon are pets. Others use them for battling."); 
    JOptionPane.showMessageDialog(null, "As for myself... I study Pokémon as a profession."); 
    JOptionPane.showMessageDialog(null, "But first tell me a little bit about yourself..."); 

    do 
    { 
     do 
     { 
      holder = JOptionPane.showInputDialog("Now tell me, are you a boy, or are you a girl?"); 
      intro.setGender(holder); 
     }while(!(intro.getGender().equals("Boy") || intro.getGender().equals("boy") || intro.getGender().equals("BOY") || intro.getGender().equals("Girl") || intro.getGender().equals("girl") || intro.getGender().equals("GIRL"))); 

     if(intro.getGender().equals("Boy") || intro.getGender().equals("boy") || intro.getGender().equals("BOY")) 
     { 
      holder = "boy"; 
      intro.setGender(holder); 
     } 
     else if(intro.getGender().equals("Girl") || intro.getGender().equals("girl") || intro.getGender().equals("GIRL")) 
     { 
      holder = "girl"; 
      intro.setGender(holder); 
     } 

       response = JOptionPane.showConfirmDialog(null, "You are a " + intro.getGender() + ". Is that correct?", JOptionPane.YES_NO_OPTION, response); 

       if(response == JOptionPane.NO_OPTION) 
       { 
        intro.setConfirmationOne("no"); 
       } 
       else if(response == JOptionPane.YES_OPTION) 
       { 
        intro.setConfirmationOne("yes"); 
       } 
    }while(intro.getConfirmationOne().equals("No") ||* intro.getConfirmationOne().equals("no") || intro.getConfirmationOne().equals("NO")); 

回答

0

你我thod與JOptionPane的任何可用方法都不匹配。

的選項有:

static int showConfirmDialog(Component parentComponent, Object message) 

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) 

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType) 

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) 

但你使用:

JOptionPane.showConfirmDialog(null, String, int, int) 

試着改變你的方法:

JOptionPane.showConfirmDialog(null, "You are a " + intro.getGender() + ". Is that correct?", "Title", JOptionPane.YES_NO_OPTION); 
0

根據你的問題:

PokemonDemo.java:40: error: incompatible types: int cannot be converted to String response = JOptionPane.showConfirmDialog(null, "You are a " + intro.getGender() + ". Is that correct?", JOptionPane.YES_NO_OPTION, response);

的showConfirmDialog的返回一個整數值,而不是字符串。 更改String responseint response和一個整數狀態EVAL你的條件(例如,0這是確定的,1它取消) 讀解DOC Documentaction

編輯

了錯誤的方法,下一個在accepteds

enter image description here

再見!

+0

'response'已經是一個int –

+0

見大通Henslee答案,你的方法不符合JOptionPane的方法。你應該看到類似這樣的東西:類型JOptionPane中的方法showConfirmDialog(Component,Object,String,int)不適用於參數(null,String,int,int)' –

0

我猜你的問題是:

response = JOptionPane.showConfirmDialog(null, "You are a " + 
     intro.getGender() + ". Is that correct?", 
     JOptionPane.YES_NO_OPTION, response); 

應該是:

response = JOptionPane.showConfirmDialog(null, "You are a " + 
     intro.getGender() + ". Is that correct?", 
     "YOUR TITLE", 
     JOptionPane.YES_NO_OPTION, response); 

javadoc,我想你要使用這個方法:

public static int showConfirmDialog(Component parentComponent, 
       Object message, 
       String title, // <-- this is what is missing 
       int optionType, 
       int messageType) 
         throws HeadlessException