我在使用 swing 在 Java 中創建自定義 imageIcon 時遇到問題。檔案名有效,但似乎 ImageIcon 根本沒有改變。這是代碼:
ImageIcon icon = new ImageIcon("Hnet.com-image.png");
JFrame frame = new JFrame("NumberPad");
frame.setIconImage(icon.getImage());
frame.setName("Pin Pad");
frame.setContentPane(new NumberPad().NumberPadPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
有人對我應該做什么有建議嗎?
uj5u.com熱心網友回復:
你應該做一個可編譯的例子。
public class IconCheck{
public static void main(String[] args){
JFrame frame = new JFrame("icon test");
ImageIcon icon = new ImageIcon("Hnet.com-image.png");
JLabel label = new JLabel(icon);
frame.setIconImage( icon.getImage() );
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}
這會在 JFrame 中顯示您的圖示嗎?如果是,那么它會更新您的 JFrame 圖示嗎?
您可能希望將您的 png 用作資源。
ImageIcon icon = new ImageIcon( IconCheck.class.getResource("/sample.png") );
它在類路徑上查找路徑,intellij 知道如何包含該路徑。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/482335.html
