Search code examples
javaswingjbuttonjcomponent

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.


Solution

  • 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);