2 Replies - 51602 Views - Last Post: 29 July 2008 - 10:41 AM Rate Topic: -----

#1 penguin2  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 90
  • Joined: 22-July 08

Can't set JFrame background color

Posted 29 July 2008 - 07:55 AM

The following code is supposed to display a JFrame with a background color other than the default gray, but always displays a gray frame anyway:

package graphicstest;
import java.awt.*;
import javax.swing.*;


public class Main {

	public static void main(String[] args) {
		JFrame f1 = new JFrame();
		Color c = new Color(0,255,0);
		f1.setBackground(c);
		f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f1.setSize(300, 300);
		f1.setVisible(true);
		}
}



Thanks for any input.

Is This A Good Question/Topic? 0
  • +

Replies To: Can't set JFrame background color

#2 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

Re: Can't set JFrame background color

Posted 29 July 2008 - 09:09 AM

Basically a JFrame contains a content pane to which you add your various components. It you want the background color of the frame you need to know the background color of the content pane

like this

	public static void main(String[] args) {
		JFrame f1 = new JFrame();
		Color c = new Color(0,255,0);
		Container con = f1.getContentPane();
		con.setBackground( c ); 
		f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f1.setSize(300, 300);
		f1.setVisible(true);


Was This Post Helpful? 3

#3 penguin2  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 90
  • Joined: 22-July 08

Re: Can't set JFrame background color

Posted 29 July 2008 - 10:41 AM

View Postzukker, on 29 Jul, 2008 - 09:09 AM, said:

Basically a JFrame contains a content pane to which you add your various components. It you want the background color of the frame you need to know the background color of the content pane


Thanks, that fixed my problem. I didn't understand what a container was before.
Was This Post Helpful? 2

Page 1 of 1