开发者

JButton's border don't go away even when set the Border to EmptyBorder

开发者 https://www.devze.com 2023-04-11 10:32 出处:网络
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 Wind

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:

JButton's border don't go away even when set the Border to EmptyBorder

The borders are all EmptyBorder.

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


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());
        }
    }
}

JButton's border don't go away even when set the Border to EmptyBorder


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 ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号