JOptionPane确认对话框

时间:2015-11-02 18:41:11

标签: java

为什么确认对话框不起作用?我花了很多时间试图解决这个问题。我收到以下错误:

  

PokemonDemo.java:40:错误:不兼容的类型:int无法转换为String                        response = JOptionPane.showConfirmDialog(null,“你是”+ intro.getGender()+“。这是正确的吗?”,JOptionPane.YES_NO_OPTION,response);

我已经尝试更改对String的响应(是的,我在使用时使用了.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"));

3 个答案:

答案 0 :(得分:0)

根据你的问题:

  

PokemonDemo.java:40:错误:不兼容的类型:int无法转换为String response = JOptionPane.showConfirmDialog(null,“你是”+ intro.getGender()+“。这是正确的吗?”,JOptionPane。 YES_NO_OPTION,响应);

showConfirmDialog返回一个Integer值,而不是String。 将String response更改为int response并使用整数状态评估您的条件(例如,0表示正常,1表示取消) 阅读文档Documentaction

修改

向上错误的方法,按下一个已接受的

enter image description here

再见!

答案 1 :(得分: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

答案 2 :(得分:0)

您的方法与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);