即使将 Border 设置为 EmptyBorder,JButton 的边框也不会消失

发布于 2024-12-07 21:58:19 字数 273 浏览 1 评论 0原文

我正在 Mac 上使用 Eclipse 设计 GUI,我想让 JButton 仅显示我设置的图标。它在 Mac OS 上看起来很好,但是当我将其导出到 jar 文件并在 Windows 操作系统上使用时,JButton 看起来像这样:

在此处输入图像描述

边框均为 EmptyBorder

我做错了什么?如何让后面的方块消失?

I am designing a GUI using Eclipse on Mac, and I want to make the JButton to display only the Icon I set. It looks fine on Mac OS, but when I export it to a jar file, and rut in on Windows OS, the JButton looks like this:

enter image description here

The borders are all EmptyBorder.

What did I do wrong? How can I make the square at the back go away?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

给不了的爱 2024-12-14 21:58:19

要正确回答此问题,很可能需要 SSCCE。无论如何,我相信您会想要调用 JButton 实例上的“noreferrer">setContentAreaFilled(false)。这应该有效地消除“正方形”。

然而,值得注意的是,调用此函数的确切行为因组件和 L&F 的不同而不同。


import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public final class JButtonDemo {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JIconButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JIconButton extends JButton{
        private static final long serialVersionUID = 7274140930080397481L;

        public JIconButton(){
            super(UIManager.getIcon("OptionPane.informationIcon"));
            setContentAreaFilled(false);
            setFocusPainted(false);
            setBorder(BorderFactory.createEmptyBorder());
        }
    }
}

在此处输入图像描述

To answer this question correctly, an SSCCE will most likely be required. Anyway, I believe that you'll want to invoke setContentAreaFilled(false) on your JButton instances. That should effectively remove the "square".

However, it is important to note that the exact behavior of calling this function varies on a component-by-component and L&F-by-L&F basis.


import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public final class JButtonDemo {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JIconButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JIconButton extends JButton{
        private static final long serialVersionUID = 7274140930080397481L;

        public JIconButton(){
            super(UIManager.getIcon("OptionPane.informationIcon"));
            setContentAreaFilled(false);
            setFocusPainted(false);
            setBorder(BorderFactory.createEmptyBorder());
        }
    }
}

enter image description here

左秋 2024-12-14 21:58:19

1)无法重现这一点,如果没有看到您为此编写的代码就很难说,

2)因为您触摸了 XxxButtonUI,这实际上是与本机操作系统相关的问题,外观和感觉用于此目的

3)什么如果你重写 MetalButtonUI 会发生什么吗?

4)是否有半透明背景,或带有某种颜色,或...

1) can't to reproduce that, too hard to say without seeing code that you wrote for that,

2) because you touched XxxButtonUI, and this is really Native OS rellated issue, which Look and Feel is there used for that

3) what happens if you override MetalButtonUI?

4) is there transulcent background, or with some Color, or ...

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