如何更改JOptionPane对话框中图标的位置?

4
我想知道是否可以将JOptionPane中图标的位置从左侧改为右侧?
public void popupMessage(){
    JCheckBox checkbox = new JCheckBox("Do not show this message again.");
        String message = "Attempt to set icon to right side is successfully approached.";
        Object[] params = {message, checkbox};
        int n = JOptionPane.showConfirmDialog(null, params, "Icon to right side",JOptionPane.YES_NO_CANCEL_OPTION);
        BasicOptionPaneUI.getIcon().paintIcon(  );
}

4
请使用“PLAIN_MESSAGE”类型,该类型隐藏图标,然后自己创建具有所需布局的组件。您可能需要使用“UIManager”查找外观和感觉所使用的图标... - MadProgrammer
1个回答

1

你可以使用JPanel实现。只需创建一个JPanel,将自己的图标或现有图标添加到JLabel中。然后将文本添加到另一个JLabel并将这些JLabel添加到JPanel中。使用BorderLayout,您可以控制文本JLabel和图标JLabel的位置。

示例(已运行和测试,效果良好):

public static void main(String[] args) 
{   
    Icon icon = UIManager.getIcon("OptionPane.errorIcon");
    JLabel iconLabel = new JLabel(icon);
    JLabel textLabel = new JLabel("Some text");

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(iconLabel,BorderLayout.EAST);
    panel.add(textLabel,BorderLayout.CENTER);

    JOptionPane.showMessageDialog(
            null,
            panel,
            "Hello", JOptionPane.PLAIN_MESSAGE);

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接