1

I can't change the background color of a JFrame, or of a JPanel inside the JFrame, this is my code:

public class MyFrame extends JFrame {

    public MyFrame(){
        setSize(600,600);
        setResizable(false);
        panel = new MyPanel();
        panel.setBackground(Color.BLACK);
        //getContentPane().setBackground(Color.BLACK); doesn't work
        getContentPane().add(panel);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent ev){
                System.exit(-1);
            }
            public void windowClosed(WindowEvent ev){
                System.exit(-1);
            }
        });
    }

 public void paint(Graphics g){
        super.paint(g);
        g.clearRect(0,0,600,600);
        synchronized (this){
            if (positions!=null){
                for (int i=0; i<positions.length; i++){
                    P2d p = positions[i];
                    //int x0 = (int)(180+p.x*180);
                    //int y0 = (int)(180-p.y*180);
                    g.drawOval((int)p.x,(int)p.y,5,5);
                }
            }
        }
    }
}

i have tried in several ways, but I can not change the color, it's always white, how i can do?

Piero
  • 8,533
  • 18
  • 82
  • 153
  • `panel.setBackground(Color.BLACK);` should work. Show your MyPanel class – Mubin Apr 25 '14 at 10:18
  • @kleopatra the question you linked deals with `JFrame` and although this question's title might be misleading, assuming OP's MyPanel follows suit of MyFrame and extends `JPanel` this question appears to be about changing the color of a `JPanel` not working at its core. – Ceiling Gecko Apr 25 '14 at 10:22

4 Answers4

0

Not sure if this will work for you but try

 JPanel panel = new JPanel();
 panel.setBackground(Color.BLACK);

or you could try setting the colour in the JFrame and leaving the colour out of the pannel

 JFrame frame = new JFrame();
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setTitle("MyFirstFrame");
 frame.setBackground(Color.BLACK);
 frame.setLocation(0,0);
 frame.setSize(150,150);
Nemothefish
  • 47
  • 10
0

Try this one. Draw a rectangle filled with your background color.

  • use SwingUtilities.invokeLater() or EventQueue.invokeLater()
  • use paintComponent() method for custom painting
  • don't draw on JFrame directly.
  • use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to close the JFrame

Sample code:

public class MyFrame extends JFrame {

    public MyFrame() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                setSize(new Dimension(600, 600));
                setResizable(false);
                JPanel panel = new JPanel() {
                    @Override
                    public void paintComponent(Graphics g) {
                        super.paintComponent(g);

                        g.clearRect(0, 0, 600, 600);
                        Color prevColor = g.getColor();

                        g.setColor(Color.BLUE); // background color
                        g.fillRect(0, 0, 600, 600); // fill a rectangle with background color
                        g.setColor(prevColor);

                        // your custom painting
                        ...
                    }
                };
                getContentPane().add(panel);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
            }
        });
    }
}
Braj
  • 44,339
  • 5
  • 51
  • 69
  • @kleopatra Have you read the link? – Braj Apr 25 '14 at 10:36
  • @kleopatra I have edited my post. – Braj Apr 25 '14 at 10:53
  • @kleopatra Thanks a lot. Now it looks good. – Braj Apr 25 '14 at 11:05
  • thanks, it works, why i have to use SwingUtilities.invokeLater() or EventQueue.invokeLater()? and why i have to use paintComponent instead of paint? – Piero Apr 25 '14 at 14:24
  • Just search on `StackOverflow`. You will definably find the answer. Please read [SwingUtilities.invokeLater](http://stackoverflow.com/questions/7196889/swingutilities-invokelater) – Braj Apr 25 '14 at 14:30
  • Read [Difference between paint, paintComponent and paintComponents in Swing](http://stackoverflow.com/questions/9389187/difference-between-paint-paintcomponent-and-paintcomponents-in-swing) – Braj Apr 25 '14 at 14:31
  • Have a look at [Why does one have to use the paintComponent method to draw in Java?](http://stackoverflow.com/questions/18005505/why-does-one-have-to-use-the-paintcomponent-method-to-draw-in-java) – Braj Apr 25 '14 at 14:32
0

Check out this answer to change background color

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.EventQueue;

public class ColoredFrame {

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );
        frame.getContentPane().setBackground( Color.PINK );
        //frame contains nothing, so set size
        frame.setSize( 200, 200 );
        frame.setVisible( true );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
    } );
  }
}

Reference of Robin's answer.

Community
  • 1
  • 1
Java Man
  • 1,678
  • 3
  • 19
  • 42
-1

As for your MyPanel it looks like you didn't set the size so atm it is not visible.
And for your jFrame since you override paint, why not try setting g.setBackground(Color.BLACK);.

timbernasley
  • 480
  • 8
  • 21
  • sizing is the exclusive task of the LayoutManager (which by default is a borderLayout on the contentPane which sizes the center component to fill the container). Plus overriding the frame's paint is the wrong approach - custom painting should be handled in the paintComponent of a component, f.i. MyPanel – kleopatra Apr 25 '14 at 11:08