3

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
yawkat
  • 374
  • 2
  • 14
  • Are you referring to the greyness of the button's background, or of the entire panel's background? You may need to explicitly set the panel's background if that's the one you are talking about. `getContentPane().setBackground(Color.BLACK)`. – FThompson May 12 '12 at 18:06

2 Answers2

1

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);
DCSoft
  • 217
  • 1
  • 4
  • 17
  • [source](http://pastebin.com/Q9KDGru1) is the class most stuff is in. In the main method I create a frame and add an instance of that to it. – yawkat May 12 '12 at 18:00
  • oh, the other methods made it look like [this](http://screens.jonalu.net/JKFtN4TOcn.png) – yawkat May 12 '12 at 18:01
  • btw, [this](http://screens.jonalu.net/eVCZR37dD6.png) is what it looks like without a button, yes, I added an aero effect, but it doesnt work without it either. – yawkat May 12 '12 at 18:08
  • okay, try button.setContentAreaFilled(true); but I'm not entirely sure. – DCSoft May 12 '12 at 18:09
  • none of these work: `button.setBorderPainted(false); button.setContentAreaFilled(false); button.setFocusPainted(false); button.setOpaque(false);` – yawkat May 12 '12 at 18:11
1

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);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • This is the code for my frame: `JFrame f = new JFrame(); GUI gui = new GUI(); f.add(gui); f.pack(); f.setBackground(Color.BLACK); f.setSize(600, 400); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true);` – yawkat May 12 '12 at 18:18
  • This worked: `frame.getContentPane().setBackground(Color.black);` Thank you. – yawkat May 12 '12 at 18:20
  • Only problem is that I cant use the aero effect any more because I cant set a transparent background color. That should be ok though. – yawkat May 12 '12 at 18:29