Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing JButton not to draw background

I am coding a little program and this is basically my first time to using a JComponent to draw stuff. I set the background of the component to black.

But as soon as I draw a JButton in it, it gets overwritten by the default grey. I have been searching for this for an hour now and can't seem to find any answers.

like image 887
yawkat Avatar asked May 29 '26 23:05

yawkat


2 Answers

What you are seeing is the frame to which you have added your JComponent, so if you want a black background frame then you need to set the background color of JFrame.

Something like this:

JFrame frame = new JFrame();
frame.add(new GUI());
frame.pack();
frame.getContentPane().setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
like image 174
mprabhat Avatar answered May 31 '26 13:05

mprabhat


try setting the button to opaque use the setOpaque(boolean opaque); method

I'm not sure if I'm right but I might be

Edit:

Try using these methods:

 button.setBorderPainted(false); 
 button.setContentAreaFilled(false); 
 button.setFocusPainted(false); 
 button.setOpaque(false);
like image 34
DCSoft Avatar answered May 31 '26 12:05

DCSoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!