【问题标题】:JOptionPane Confirm Dialog BoxJOptionPane 确认对话框
【发布时间】:2016-02-02 17:59:04
【问题描述】:

为什么确认对话框不起作用?我一直在努力解决这个问题。我收到以下错误:

PokemonDemo.java:40:错误:不兼容的类型:int 无法转换为 String response = JOptionPane.showConfirmDialog(null, "你是" + intro.getGender() + "。对吗?", 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"));

【问题讨论】:

    标签: java


    【解决方案1】:

    根据你的问题:

    PokemonDemo.java:40:错误:不兼容的类型:int 无法转换为字符串 response = JOptionPane.showConfirmDialog(null, "You are a " + intro.getGender() + "。对吗?", JOptionPane. YES_NO_OPTION,响应);

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

    编辑

    错误的方法向上,接受的方法之一向下

    再见!

    【讨论】:

    • 查看 Chase Henslee 的答案,您的方法与 JOptionPane 的方法不匹配。你应该会看到类似The method showConfirmDialog(Component, Object, String, int) in the type JOptionPane is not applicable for the arguments (null, String, int, int)
    【解决方案2】:

    我猜你的问题是:

    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
    

    【讨论】:

      【解决方案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);
      

      【讨论】:

        猜你喜欢
        • 2012-01-31
        • 2012-08-31
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 2020-02-16
        • 2012-08-27
        • 2011-05-24
        相关资源
        最近更新 更多