在Java JFrame中显示图像 [英] Display image in Java JFrame
问题描述
在java JFrame中以特定坐标显示图像的最佳方法是什么?
What's the best way to display an image at specific coordinates in a java JFrame?
我知道有很多方法可以做到这一点,我只需要知道显示我计划在框架周围移动的图像的最佳方式!
I know there are a number of ways to do this, I just need to know the best way to display an image that I am planning on moving around the frame!
推荐答案
我建议将其添加为ImageIcon:
I would suggest adding it as an ImageIcon:
ImageIcon image = new ImageIcon("image.jpeg");
add(image);
编辑(如何添加添加特定坐标) :
EDIT (How to add it add specific coordinates):
首先是一个注释,通常(99%的情况)不鼓励自己定位JComponents。使用LayoutManager为你定位它们,并处理调整大小。
我会告诉你如何自己定位:
First a note, usually (99% of the cases) it is NOT encouraged to position JComponents yourself. Use a LayoutManager to position them for you, and handle the resizing.
I will tell you how to position it yourself though:
- 将LayoutManager设置为
null
:setLayout(null)
。 - 将
ImageIcon
设置为特定坐标:image.setLocation(x,y)
。
- Set your LayoutManager to
null
:setLayout(null)
. - set your
ImageIcon
to specific coordinates:image.setLocation(x, y)
.
这篇关于在Java JFrame中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!