Java 如何将图像添加到JButton
我正在尝试向JButton添加一个图像,但我不确定我缺少了什么。当我运行下面的代码时,按钮看起来与我创建它时没有任何图像属性完全相同。Water.bmp位于我的项目文件夹的根目录中Java 如何将图像添加到JButton,java,swing,icons,jbutton,embedded-resource,Java,Swing,Icons,Jbutton,Embedded Resource,我正在尝试向JButton添加一个图像,但我不确定我缺少了什么。当我运行下面的代码时,按钮看起来与我创建它时没有任何图像属性完全相同。Water.bmp位于我的项目文件夹的根目录中 ImageIcon water = new ImageIcon("water.bmp"); JButton button = new JButton(water); frame.add(button); 或使用此代码 class MyButton extends JButton { Im
ImageIcon water = new ImageIcon("water.bmp");
JButton button = new JButton(water);
frame.add(button);
或使用此代码
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);
}
}
我认为您的问题在于图像的位置。你应该把它放在你的源头,然后像这样使用它:
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/folder中。这看起来像是位置问题,因为该代码完全适合添加图标 由于我不知道您的文件夹结构,我建议添加一个简单的检查:
File imageCheck = new File("water.bmp");
if(imageCheck.exists())
System.out.println("Image file found!")
else
System.out.println("Image file not found!");
这样,如果您的路径名出错,它将告诉您,而不是什么也不显示。若文件不存在,那个么应该抛出异常。我只做了一件事,它对我有效。。检查您的代码是否存在此方法
setResizable(false);
如果它是假的,那就把它变成真的,它就会很好地工作。。
我希望这有帮助 @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);
这段代码对我有用:
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);
例如,如果文件夹
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();
}
如果图像大于按钮,则不会显示。将图像放入资源文件夹,并使用以下代码:
JButton btn = new JButton("");
btn.setIcon(new ImageIcon(Class.class.getResource("/resources/img.png")));
太复杂了。有一个内置的机制可以为按钮添加图标,为什么会产生额外的问题?@Rogach不是专门针对这个问题,但在我的例子中,进一步控制图像在按钮中的位置是很有用的。这应该可以工作……您可以尝试使用
ImageIcon
构造函数的URL
形式,看看它做了什么吗?可能是因为某种原因,它找不到图像文件。是的,它正在工作。代码没有更改。谢谢大家的建议。框架(?)是否可调整大小与你可能是对的无关。。但是当你有一个边界布局,然后你做这个声明。。你限制了画面。。我不知道为什么,但这对我也不起作用。我甚至建立了一个功能,搜索目录中想要的文件,并找到它们-仍然没有图标。所以,我只使用以下行:button1.setIcon(“path/pic.png”);-这是我的。你知道为什么吗?
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);
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();
}
JButton btn = new JButton("");
btn.setIcon(new ImageIcon(Class.class.getResource("/resources/img.png")));