我目前正在为游戏创建 GUI。我目前正在使用 JLabel 将图像加载到我的 JPanel 上,我想知道是否可以在图像的各个部分添加按钮,例如(5,7) 和 (12,12)。
最佳答案
如果您希望在图像上显示 JButtons
,更好的方法是覆盖 JPanel
的 paintComponent
并使用 drawImage绘制类似于 this example 的图像。这将允许您将组件添加到容器中。
绝对定位(null
布局)在设置组件位置时通常不受欢迎。这个 DragLayout 应该是处理组件大小的更好替代品。
DragLayout was designed to replace a null layout. It will respect the location of a component. By default it will use the preferred size of the component to determines its size. Finally, it will automatically calculate the preferred size of the Container.
关于java - 如何在图片上添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14663932/