有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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"));

共 (3) 个答案

  1. # 1 楼答案

    我想你的问题是:

    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. # 2 楼答案

    根据你的问题:

    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,并使用整数状态评估您的条件(例如,0表示Ok,1表示Cancel) 阅读文件Documentaction

    编辑

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

    enter image description here

    再见

  3. # 3 楼答案

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