3

Java 的“如何制作对话框”教程显示了以下代码:

//custom title, custom icon
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.",
        "Inane custom dialog", JOptionPane.INFORMATION_MESSAGE, icon);

这将创建以下对话框:

Java 的示例对话框

为什么JOptionPane.INFORMATION_MESSAGE需要将图标更改为icon参数?

4

2 回答 2

4

The flag also indicates which message style to use on the window decorations, see http://nadeausoftware.com/node/91#Usinglookandfeelspecificwindowdecorations

from the JOptionPane class source code:

private static int styleFromMessageType(int messageType) {
    switch (messageType) {
    case ERROR_MESSAGE:
        return JRootPane.ERROR_DIALOG;
    case QUESTION_MESSAGE:
        return JRootPane.QUESTION_DIALOG;
    case WARNING_MESSAGE:
        return JRootPane.WARNING_DIALOG;
    case INFORMATION_MESSAGE:
        return JRootPane.INFORMATION_DIALOG;
    case PLAIN_MESSAGE:
    default:
        return JRootPane.PLAIN_DIALOG;
    }
}

and in the method showOptionDialog which is called by showMessageDialog...

int style = styleFromMessageType(messageType);
JDialog dialog = pane.createDialog(parentComponent, title, style);
于 2013-08-28T22:13:52.217 回答
0

我怀疑这可能有很多原因......例如......

如果提供的解析为(或由于某种原因无法加载底层图像),它将允许JOptionPane返回到消息类型。iconnull

它将允许外观忽略icon并使用消息类型。

于 2013-08-29T00:08:47.787 回答