java - 如何在图片上添加按钮?

标签 java image swing jbutton

我目前正在为游戏创建 GUI。我目前正在使用 JLabel 将图像加载到我的 JPanel 上,我想知道是否可以在图像的各个部分添加按钮,例如(5,7) 和 (12,12)。

最佳答案

如果您希望在图像上显示 JButtons,更好的方法是覆盖 JPanelpaintComponent 并使用 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/

相关文章:

java - 正则表达式提取文件名

html - 在固定大小的 div 中调整图像大小

java - JTable、JLabel 和它的图标(JLabel 不能用图标自定义)?

java - 为什么这个程序不画正方形?

java - TitledBorder 标题更新/刷新延迟,为什么?

java - JDK7泛型未编译

java - 如何使用java将日语编码文本(Unicode类型除外)复制到剪贴板?

java:在运行时跟踪文件大小?

javascript - 使用带有自定义 header 的 Ajax 加载图像

php - 如何使用 PHP 调整高分辨率图片的大小