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

xia*_*406 4 java user-interface swing jbutton imageicon

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

在此输入图像描述

边界都是EmptyBorder.

我做错了什么?怎么能让后面的广场消失?

mre*_*mre 10

要正确回答这个问题,很可能需要SSCCE.无论如何,我相信你会想要调用setContentAreaFilled(false)你的JButton实例.这应该有效地删除"方块".

但是,重要的是要注意,调用此函数的确切行为因逐个组件和L&F-by-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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述