我想替换JOptionPane标题栏中的图标(因为它当前显示的是默认的Java咖啡徽标)。
我尝试了以下几点:
JOptionPane.showMessageDialog(null, "Some Text", "Login",
JOptionPane.INFORMATION_MESSAGE, ImageCacheProvider
.instance.getImageIcon("img/an image.png"));它替换窗口中的图标,但不替换标题栏中的图标:

是否有任何方法可以更改标题栏中的图标,或者在不需要实现JDialog类的情况下隐藏默认的Java图标?
谢谢一堆人!托马斯
发布于 2014-11-02 18:12:52
像这样使用它:
Icon icon = new ImageIcon("d:/temp/CheckBox.gif");
JOptionPane jp = new JOptionPane("Session Expired - Please Re Login"),
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION,
icon);
JDialog dialog = jp.createDialog(null, "Session Expired - Please Re Login");
((Frame)dialog.getParent()).setIconImage(((ImageIcon)icon).getImage());
dialog.setResizable(true);
dialog.setVisible(true); 发布于 2022-05-27 14:32:46
这对我来说效果很好:
private static final Image myImage = ...;
/*
* Copied from javax.swing.JOptionPane.showOptionDialog(Component, Object, String, int, int, Icon, Object[], Object)
*/
@SuppressWarnings("deprecation")
public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType,
int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException {
JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue);
pane.setInitialValue(initialValue);
JDialog dialog = pane.createDialog(parentComponent, title);
// Added this line
dialog.setIconImage(myImage);
pane.selectInitialValue();
dialog.show();
dialog.dispose();
Object selectedValue = pane.getValue();
if (selectedValue == null)
return JOptionPane.CLOSED_OPTION;
if (options == null) {
if (selectedValue instanceof Integer)
return ((Integer) selectedValue).intValue();
return JOptionPane.CLOSED_OPTION;
}
for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) {
if (options[counter].equals(selectedValue))
return counter;
}
return JOptionPane.CLOSED_OPTION;
}https://stackoverflow.com/questions/26702624
复制相似问题