在Java JFrame中显示图像

Ben*_*gel 7 java swing image jframe

在Java JFrame中以特定坐标显示图像的最佳方法是什么?

我知道有很多方法可以做到这一点,我只需要知道显示图像的最佳方式,我计划在框架周围移动!

Hid*_*dde 2

我建议将其添加为 ImageIcon:

ImageIcon image = new ImageIcon("image.jpeg");
add(image);
Run Code Online (Sandbox Code Playgroud)

编辑(如何添加它添加特定坐标):

首先要注意的是,通常(99% 的情况)不鼓励您自己定位 JComponent。使用 LayoutManager 为您定位它们,并处理调整大小。
我会告诉你如何自己定位:

  • 将您的 LayoutManager 设置为nullsetLayout(null)
  • 将您设置ImageIcon为特定坐标:image.setLocation(x, y)