4

我在 Mac 上使用 Eclipse 设计一个 GUI,我想让JButton它只显示我设置的图标。它在 Mac OS 上看起来不错,但是当我将它导出到 jar 文件并在 Windows 操作系统上运行时,JButton看起来像这样:

在此处输入图像描述

边界都是EmptyBorder

我做错什么了?我怎样才能让后面的方块消失?

4

2 回答 2

10

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

但是,重要的是要注意调用此函数的确切行为会因组件和 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());
        }
    }
}

在此处输入图像描述

于 2011-10-05T11:52:18.740 回答
2

1)不能重现,很难说没有看到你为此编写的代码,

2)因为您触摸了 XxxButtonUI,而这确实是与 Native OS 相关的问题,其中 Look and Feel 用于该问题

3)如果你覆盖MetalButtonUI会发生什么?

4)是否有半透明的背景,或者带有一些颜色,或者......

于 2011-10-05T11:36:39.850 回答