如何将图像添加到 JButton
我正在尝试将图像添加到 JButton,但不确定我缺少什么。当我运行以下代码时,该按钮看起来与我在没有任何图像属性的情况下创建它时完全相同。 Water.bmp 位于我的项目文件夹的根目录中。
ImageIcon water = new ImageIcon("water.bmp");
JButton button = new JButton(water);
frame.add(button);
I am trying to add an image to a JButton and I'm not sure what I'm missing. When I run the following code the button looks exactly the same as if I had created it without any image attribute. Water.bmp is in the root of my project folder.
ImageIcon water = new ImageIcon("water.bmp");
JButton button = new JButton(water);
frame.add(button);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我认为你的问题在于图像的位置。您应该将其放置在源中,然后像这样使用它:
在本例中,假设图像位于 src/resources/ 文件夹中。
I think that your problem is in the location of the image. You shall place it in your source, and then use it like this:
In this example, it is assumed that image is in src/resources/ folder.
@Rogach
,您可能想添加:
@Rogach
and you may like to add:
它看起来像是一个位置问题,因为该代码非常适合添加图标。
由于我不知道您的文件夹结构,我建议添加一个简单的检查:
这样,如果您的路径名错误,它会告诉您,而不是不显示任何内容。如果文件不存在,则应抛出异常。
It looks like a location problem because that code is perfectly fine for adding the icon.
Since I don't know your folder structure, I suggest adding a simple check:
This way if you ever get your path name wrong it will tell you instead of displaying nothing. Exception should be thrown if file would not exist, tho.
您将图像放入资源文件夹中并使用以下代码:
You put your image in resources folder and use follow code:
或使用此代码
OR use this code
我只做了一件事,它对我有用..检查你的代码是否有这个方法..
如果它是假的,那就让它为真,它会工作得很好..
我希望它有帮助..
I did only one thing and it worked for me .. check your code is this method there ..
if it false make it true and it will work just fine ..
I hope it helped ..
这段代码对我有用:
This code work for me:
例如,如果文件夹
res/image.png
中有图像,您可以这样写:在一行中:
如果图像比按钮大,则不会显示。
For example if you have image in folder
res/image.png
you can write:In one line:
If the image is bigger than button then it will not shown.
日食示例:
读完上面的内容后,我仍然需要更多的研究来了解如何使用 eclipse 放置图像资源以及如何放置图像资源。
结果:在 Eclipse 中,您需要将图像存储在任何“源文件夹”(例如“src”)下或“文件夹”下面。
您可以通过右键单击项目“新建”->“源文件夹”或“新建”->“文件夹”来创建这两个文件夹。任何文件夹名称都可以。 “<源文件夹>/<图像文件夹>”的示例是“src/images”或“resource/img”。
下面是一些功能齐全的示例代码,需要两个按钮图像,“Button-Image-Up.png”和“Button-Image-Down.png”位于文件夹“images”中:
玩得开心!
沃尔克·弗罗利希
PS:
也许有人可以分享更多的实践经验。
eclipse example:
After reading the above, it still took me more research to understand, where and how to place image resources, using eclipse.
The outcome: in eclipse you need to store images underneath any "Source Folder" (such as "src") or below in a "Folder".
You create both by right-clicking on your project, "New"->"Source Folder" or "New"->"Folder". Any folder name is fine. Examples for "<Source Folder>/<image Folder>" are "src/images" or "resource/img".
Here's some fully functioning sample code, which expects two button images, "Button-Image-Up.png" and "Button-Image-Down.png" in folder "images":
Have Fun!
Volker Fröhlich
P.S.:
Perhaps someone can share additional practical experience.