为什么这段代码给我错误 "imageicon cannot be converted to int"?

Why does this code gives me the error "imageicon cannot be converted to int"?

我在将图片添加到消息屏幕时遇到问题。如果我不在我的框中使用文本字段,代码可以工作,但它也可以使用文本字段但没有图片......我真的不明白为什么我会收到这个错误:

incompatible types: ImageIcon cannot be converted to int

这是我的代码:

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;
    }   
}

我是新来的,所以我希望这是正确的。 :)

您在调用 JOptionPane.showConfirmDialog 时缺少一个参数,messageType 是一个 int,介于 optionType 和 icon 之间。请参阅 JOptionPane.showConfirmDialog

的文档

如果您想将 Icon 传递给 showConfirmDialog,您需要 use the 6 argument overload:

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

我在此示例中使用了 PLAIN_MESSAGE,但您可以使用 ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE 如 API 文档中所述。