我想知道是否有一种快速简便的方法可以尝试将图像定位在 GUI 中的特定位置。除此之外,还可以在此图片上放置 Jlables/Buttons。该图片应该仅构成 JFrame 的一部分。我知道你可以使用 setbounds(width, height, width, height);但这需要很高的准确性,而且很难完全正确。
最佳答案
不要调用setBounds
,这意味着您正在使用null
布局。始终使用布局管理器。
一种方法是添加一个自定义 JPanel
来覆盖 paintComponent
并调用 drawImage(Image, x, y, width, height, ...)
。 JLabels/JButtons
等仍然可以添加到面板中。
不要忘记调用 super.paintComponent
- 这将确保您的子组件被绘制。
此外,请查看 Background Panel .
关于java - 在java中的GUI JFrame上定位图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14224794/