如何去除按钮周围的边框?
我有一个带有 GridLayout 的 JPanel。在网格的每个单元格中我都有一个按钮。我看到每个按钮都被灰色边框包围。我想删除这些边界。有人知道如何做到吗?
I have a JPanel with the GridLayout. In every cell of the grid I have a button. I see that every button is surrounded by a gray border. I would like to remove these borders. Does anybody know how it can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有关边框的更多详细信息,请参阅 BorderFactory
For more details on borders see the BorderFactory
在最新的 Java 版本中,需要调用 setContentAreaFilled(false) 来完全删除边框。添加一个空边框作为一些填充:
In most recent Java versions it's necessary to call setContentAreaFilled(false) to remove the border entirely. Add an empty border for some padding:
我认为边框很可能是按钮 GUI 的一部分。您可以尝试在所有按钮上调用
.setBorder(null)
,看看会发生什么!I think it's very likely the borders are part of the buttons' GUI. You could try calling
.setBorder(null)
on all the buttons and see what happens!虽然所有这些答案都以某种方式起作用,但我想我应该通过示例对每个答案进行更深入的比较。
第一个默认按钮:
边框绘制设置为 false 的按钮会删除边框和悬停操作,但保留填充:
具有空边框或空边框的按钮会删除边框,悬停操作和填充:
或
带有空边框和尺寸的按钮会删除边框和悬停操作,并将填充设置为提供的值:
最后,将它们与背景和悬停操作以获取在悬停时突出显示的自定义遮罩按钮:
While all of these answers work in some way, I thought I'd provide a little more in depth comparison of each along with examples.
First default buttons:
Buttons with border painted set to false removes the border and hover action but keeps the padding:
Buttons with a null border or empty border removes the border, hover action and padding:
or
Buttons with an empty border plus dimensions removes the border and hover action and sets the padding to the provided values:
Lastly, combine these with a background and hover action to get custom matte buttons that get highlighted on hover: