Java “作业窗格确认”对话框

Java “作业窗格确认”对话框,java,Java,为什么确认对话框不工作?我花了很多时间想弄明白这一点。我得到以下错误: java:40:错误:不兼容的类型:int无法转换为字符串 response=JOptionPane.showConfirmDialognull,您是一个+intro.getGender+。正确吗?,JOptionPane.YES\u NO\u选项,响应 我尝试过将响应更改为字符串,是的,我使用了.equals方法,但什么也没发生。即使程序中没有int,我仍然会得到错误。如果您需要我的对象代码,请告诉我,但我不明白在这种情况

为什么确认对话框不工作?我花了很多时间想弄明白这一点。我得到以下错误:

java:40:错误:不兼容的类型:int无法转换为字符串 response=JOptionPane.showConfirmDialognull,您是一个+intro.getGender+。正确吗?,JOptionPane.YES\u NO\u选项,响应

我尝试过将响应更改为字符串,是的,我使用了.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"));

根据你的问题:

java:40:错误:不兼容的类型:int无法转换为字符串response=JOptionPane.showConfirmDialognull,您是+intro.getGender+。正确吗?,JOptionPane.YES\u NO\u选项,响应

showConfirmDialog返回一个整数值,而不是字符串。 将字符串响应更改为int-response,并使用整数状态评估您的条件,例如,0为Ok,1为Cancel 读文件

编辑

向上移动方法,向下移动一个接受对象


再见

我想你的问题是:

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);
据我所知,我猜你正在尝试使用这种方法:

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

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

请参见Chase-Henslee的答案,您的方法与JOptionPane的方法不匹配。您应该会看到类似这样的情况,类型JOptionPane中的showConfirmDialogComponent、Object、String、int方法不适用于参数null、String、int、int