简体   繁体   English

为什么这段代码给我错误“无法将imageicon转换为int”?

[英]Why does this code gives me the error “imageicon cannot be converted to int”?

I have a problem with adding a picture to my messagescreen. 将图片添加到消息屏幕时出现问题。 The code works if I don't use textfields in my box, but it also worked with the textfields and without the picture... I really don't get why I'm getting this error: 如果我不在框内使用文本字段,则该代码有效,但也可以在没有文本的情况下使用文本字段……我真的不明白为什么会出现此错误:

incompatible types: ImageIcon cannot be converted to int 不兼容的类型:ImageIcon无法转换为int

This is my code: 这是我的代码:

import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
import javax.swing.ImageIcon;

public class Input{    

    public static String[] geefInputNamen(){    
        JTextField veld1 = new JTextField(); 
        JTextField veld2 = new JTextField();

        Object[] velden = {  
            "Speler 1:", veld1,
            "Speler 2:", veld2
        };

        ImageIcon icon = new ImageIcon("nbalivemobile.png");

        JOptionPane.showConfirmDialog(null, velden, "Spelers vergelijken", 
                                   JOptionPane.OK_CANCEL_OPTION, icon);


        String[] namen = new String[2];

        namen[0] = veld1.getText(); 
        namen[1] = veld2.getText();

        return namen;
    }   
}

I'm new here, so I hope this is posted right. 我是新来的,所以我希望这是正确的。 :) :)

You are missing a parameter in your call to JOptionPane.showConfirmDialog, messageType which is an int and goes between optionType and icon. 调用JOptionPane.showConfirmDialog,messageType时缺少一个参数,它是一个int值,介于optionType和icon之间。 See the doc for JOptionPane.showConfirmDialog 请参阅JOptionPane.showConfirmDialog的文档

If you want to pass an Icon to showConfirmDialog , you need to use the 6 argument overload : 如果要将Icon传递给showConfirmDialog ,则需要使用6参数重载

JOptionPane.showConfirmDialog(
    null,
    velden,
    "Spelers vergelijken",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.PLAIN_MESSAGE, // Add this argument
    icon);

I've used PLAIN_MESSAGE in this example, but you can use any one of ERROR_MESSAGE , INFORMATION_MESSAGE , WARNING_MESSAGE , QUESTION_MESSAGE , or PLAIN_MESSAGE as specified in the API documentation. 在此示例中,我使用了PLAIN_MESSAGE ,但是您可以使用API​​文档中指定的ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE任何一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM