使用 Java AWT 将图像添加到面板
我之前发布过关于面板非常混乱的文章,但是,我通过简单地更改布局来解决这个问题(感谢查理·马丁帮助我)。 现在,我尝试将图像添加到面板中,以便我可以将该面板添加到框架中。 这是我正在搞乱的课程的一部分。
http://friendpaste.com/13zibFC4oVxCbm83500KVj (死链接)
这是我运行程序并点击开始游戏(在启动弹出窗口上)时出现的情况。
本质上,主窗口上应该有一个图像以及按钮,但我不确定如何我会着手实施这一点。
I posted earlier about having a really messed up panel but, I fixed that by simply changing the layout (thank you to Charlie Martin for helping me). Now, I'm trying to add an image to a panel, so I can add that panel to the frame. This is part of the class that I am messing around with.
http://friendpaste.com/13zibFC4oVxCbm83500KVj
(dead link)
This is what comes up when I run the program and hit start game (on the startup popup)..
Essentially, there is supposed to be an image on the main window along with the buttons and I'm not exactly sure how I would go about implementing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能只是在 JLabel 上设置 ImageIcon 并将 JLabel 添加到面板中所需的位置。
I'd probably just set an ImageIcon on a JLabel and add the JLabel where you want in the panel.
拜托,拜托,拜托,不要使用 JLabel。 虽然这是最简单的方法,但它也是不好的做法,并且当您的图像不是您想要显示的实际尺寸时会导致问题。
第二个答案是正确的,但仍然存在同样的问题。
这是我过去编写的一个类,它正是您所需要的:
如果您愿意,它还允许您设置特定的大小; 它将缩放图像以最适合面板,并对齐图像。
Please, please, please -don't- use the JLabel. While that -is- the easiest way, it's also bad practice and causes problems when you have images that aren't the actual size you want displayed.
The second answer is on the right path, but still has the same problem.
Here's a class I've written in the past which is what you need:
It will also allow you to set the specific size if you want; it will scale the image to best fit the panel, and align the image as well.