如何去除按钮周围的边框?

发布于 2024-08-30 06:19:12 字数 87 浏览 2 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不喜欢何必死缠烂打 2024-09-06 06:19:12
Border emptyBorder = BorderFactory.createEmptyBorder();
yourButton.setBorder(emptyBorder);

有关边框的更多详细信息,请参阅 BorderFactory

Border emptyBorder = BorderFactory.createEmptyBorder();
yourButton.setBorder(emptyBorder);

For more details on borders see the BorderFactory

指尖上得阳光 2024-09-06 06:19:12
yourButton.setBorderPainted(false);
yourButton.setBorderPainted(false);
鹿港小镇 2024-09-06 06:19:12

在最新的 Java 版本中,需要调用 setContentAreaFilled(false) 来完全删除边框。添加一个空边框作为一些填充:

button.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
button.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:

button.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
button.setContentAreaFilled(false);
灯角 2024-09-06 06:19:12

我认为边框很可能是按钮 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!

梦途 2024-09-06 06:19:12

虽然所有这些答案都以某种方式起作用,但我想我应该通过示例对每个答案进行更深入的比较。

第一个默认按钮:

在此处输入图像描述

边框绘制设置为 false 的按钮会删除边框和悬停操作,但保留填充:

button.setBorderPainted(false);

在此处输入图像描述

具有空边框或空边框的按钮会删除边框,悬停操作和填充:

button.setBorder(BorderFactory.createEmptyBorder());

button.setBorder(null);

在此处输入图像描述

带有空边框和尺寸的按钮会删除边框和悬停操作,并将填充设置为提供的值:

border.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));

在此处输入图像描述

最后,将它们与背景和悬停操作以获取在悬停时突出显示的自定义遮罩按钮:

button.setBackground(Color.WHITE);
button.setBorderPainted(false);

button.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseEntered(java.awt.event.MouseEvent evt) {
        button.setBackground(Color.GRAY);
    }

    public void mouseExited(java.awt.event.MouseEvent evt) {
        button.setBackground(Color.WHITE);
    }
});

在此处输入图像描述

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:

enter image description here

Buttons with border painted set to false removes the border and hover action but keeps the padding:

button.setBorderPainted(false);

enter image description here

Buttons with a null border or empty border removes the border, hover action and padding:

button.setBorder(BorderFactory.createEmptyBorder());

or

button.setBorder(null);

enter image description here

Buttons with an empty border plus dimensions removes the border and hover action and sets the padding to the provided values:

border.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));

enter image description here

Lastly, combine these with a background and hover action to get custom matte buttons that get highlighted on hover:

button.setBackground(Color.WHITE);
button.setBorderPainted(false);

button.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseEntered(java.awt.event.MouseEvent evt) {
        button.setBackground(Color.GRAY);
    }

    public void mouseExited(java.awt.event.MouseEvent evt) {
        button.setBackground(Color.WHITE);
    }
});

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文