簡體   English   中英

JOptionPane 標題欄圖標

[英]JOptionPane title bar icon

我想替換 JOptionPane 標題欄中的圖標(因為它當前顯示默認的 Java 咖啡徽標)。

我嘗試了以下內容:

JOptionPane.showMessageDialog(null, "Some Text", "Login",
 JOptionPane.INFORMATION_MESSAGE, ImageCacheProvider
   .instance.getImageIcon("img/an image.png"));

它替換了 window 中的圖標,但沒有替換標題欄中的圖標:

截圖標題欄

是否有任何方法可以更改標題欄中的圖標或者隱藏默認的 Java 圖標而無需實現 JDialog class?

非常感謝! 托馬斯

像這樣使用它:

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

這對我很有效:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM