JButton border does not disappear even when setting border in EmptyBorder

I am designing a GUI using Eclipse on a Mac, and I want JButton to only display the icon I installed. It looks great on Mac OS, but when I export it to a jar file and run on Windows, JButton looks like this:

enter image description here

All Borders EmptyBorder .

What I did wrong? How can I make a square on the back?

+4
source share
2 answers

To answer this question correctly, SSCCE is most likely required. In any case, I believe that you will want to call setContentAreaFilled(false) on your JButton instances. This should effectively remove the "square".

However, it is important to note that the exact behavior of calling this function depends on the component by component and the basis of 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()); } } } 

enter image description here

+10
source

1) cannot reproduce this, it is too difficult to say without seeing the code that you wrote for this,

2), because you touched XxxButtonUI, and this is really a problem with the native OS, which looks like this: Look and Feel is used for this

3) what happens if you redefine MetalButtonUI?

4) there is a transcultural background with either some color or ...

+2
source

All Articles