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:
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());
}
}
}
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With