我正在尝试将图像添加到JButton,但不确定我丢失了什么。当我运行以下代码时,该按钮看起来与我创建时没有任何图像属性的按钮完全相同。Water.bmp在我的项目文件夹的根目录中。
ImageIcon water = new ImageIcon("water.bmp");
JButton button = new JButton(water);
frame.add(button);
我认为您的问题出在图像的位置。您应将其放置在源中,然后像这样使用它:
JButton button = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("resources/water.bmp"));
button.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
在此示例中,假定映像位于src / resources /文件夹中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句