我正在尝试为JButton添加一张图片,但不确定缺少了什么。当我运行以下代码时,该按钮的外观与没有添加任何图像属性的情况下创建的按钮完全相同。Water.bmp位于我的项目文件夹的根目录中。
ImageIcon water = new ImageIcon("water.bmp");
JButton button = new JButton(water);
frame.add(button);
我正在尝试为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/文件夹中。@Rogach
而且您可能想添加:
// to remote the spacing between the image and button's borders
button.setMargin(new Insets(0, 0, 0, 0));
// to add a different background
button.setBackground( ... );
// to remove the border
button.setBorder(null);
看起来是位置问题,因为那段代码完全可以添加图标。
由于我不知道您的文件夹结构,建议添加一个简单的检查:
File imageCheck = new File("water.bmp");
if(imageCheck.exists())
System.out.println("Image file found!")
else
System.out.println("Image file not found!");
JButton btn = new JButton("");
btn.setIcon(new ImageIcon(Class.class.getResource("/resources/img.png")));
public class ImageButton extends JButton {
protected ImageButton(){
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Image img = Toolkit.getDefaultToolkit().getImage("water.bmp");
g2.drawImage(img, 45, 35, this);
g2.finalize();
}
}
或者使用这段代码
class MyButton extends JButton {
Image image;
ImageObserver imageObserver;
MyButtonl(String filename) {
super();
ImageIcon icon = new ImageIcon(filename);
image = icon.getImage();
imageObserver = icon.getImageObserver();
}
public void paint( Graphics g ) {
super.paint( g );
g.drawImage(image, 0 , 0 , getWidth() , getHeight() , imageObserver);
}
}
我只做了一件事,这对我起了作用。检查你的代码中是否有这个方法。
setResizable(false);
如果它是假的,将其改为真,并且它就可以正常工作了...
希望对你有所帮助...
buttonB.setIcon(new ImageIcon(this.getClass().getResource("imagename")));
BufferedImage image = null;
try {
URL file = getClass().getResource("water.bmp");
image = ImageIO.read(file);
} catch (IOException ioex) {
System.err.println("load error: " + ioex.getMessage());
}
ImageIcon icon = new ImageIcon(image);
JButton quitButton = new JButton(icon);
//paste required image on C disk
JButton button = new JButton(new ImageIcon("C:water.bmp");
res/image.png
中有图像,则可以编写以下内容:try
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("image.png");
// URL input = classLoader.getResource("image.png"); // <-- You can use URL class too.
BufferedImage image = ImageIO.read(input);
button.setIcon(new ImageIcon(image));
}
catch(IOException e)
{
e.printStackTrace();
}
一行代码:
try
{
button.setIcon(new ImageIcon(ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("image.png"))));
}
catch(IOException e)
{
e.printStackTrace();
}
ImageIcon
构造函数的URL
形式并查看它的效果?可能是因为某些原因无法找到图像文件。 - Cameron Skinner