[英]ImageIcon in swing package of Java not functioning correctly
我在使用 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);
有人对我应该做什么有建议吗?
你应该做一个可编译的例子。
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 知道如何包含该路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.