2015-11-02 5 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 response к int response и Eval свое состояние с состоянием целого (например, 0 это нормально, 1 это Отмена) Read де док Documentaction

EDIT

Up некорректно метод, вниз один из accepteds

enter image description here

Bye!

+0

'response' уже в междунар –

+0

Смотрите Chase Henslee ответ, ваш метод не совпадает с методом JOptionPane. Вы должны увидеть что-то вроде этого. Метод showConfirmDialog (Component, Object, String, int) в типе JOptionPane не применим для аргументов (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