Solved

JAVA: How to customize  JFrame  background color?

Posted on 2009-03-29
9
1,606 Views
Last Modified: 2012-05-06
HI, experts:

I tried to customize the background color of JFrame as the follows:

myFrame  =  new JFrame ( );

myFrame.setBackground  (new Color  ( 222, 222, 222 ));

But it did not change the color.

Thanks a lot!
0
Comment
Question by:duta
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 50 total points
ID: 24013118
myFrame.getContentPane().setBackground(new Color  ( 222, 222, 222 ));
0
 
LVL 12

Assisted Solution

by:basav_com
basav_com earned 50 total points
ID: 24013482
0
 

Author Comment

by:duta
ID: 24013593
Masters, thank you so much.

I tried this: myFrame.getContentPane().setBackground(new Color  ( 222, 222, 222 ));
But it did not work for some reason.

I checked this site: http://java2everyone.blogspot.com/2009/02/jframe-tutorial-5-change-background.html

For a total newbie like me, it is a little challenge to locate a right tip from hundreds of links.

Thanks a lot!
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 25

Accepted Solution

by:
InteractiveMind earned 400 total points
ID: 24013918
Can you post your full code please? Changing the panel's background usually does the job..
0
 
LVL 12

Expert Comment

by:basav_com
ID: 24013921
Post your code.
0
 

Author Comment

by:duta
ID: 24014075
Masters, thank you so much. My trimmed code is below in the Code Snippet.



import    java.awt.*;
import    javax.swing.*;
import    java.awt.event.*;
 
 
 
public  class  COLOR  extends   JFrame
{
   
  private   JFrame          myFrame;
    
  
   //  Constructor
   
   public COLOR (  )
   {
      
     
      setTitle  ( "Testing" );
      
      setSize   ( 400, 300 );
      
  
    
      setDefaultCloseOperation  ( JFrame.EXIT_ON_CLOSE );
      
  
     
      
      buildPanel ( );
      
      setVisible ( true );
   }    
  
    
   
      private  void  buildPanel ( )
      {
      
      
      
         setLayout ( new BorderLayout ( ) );
         
        
         myFrame  =  new JFrame ( );
         myFrame.getContentPane( ).setBackground( new Color  ( 255, 255, 100 ) );
      
      
      }
    
      
      
   
     
      // Main function
   
      public static void main ( String [ ] args )
      {
           new COLOR( );
      }

Open in new window

0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24014237
You created two JFrames, set the colour for one and then showed the other. Try this instead.
import    java.awt.*;
import    javax.swing.*;
import    java.awt.event.*;
 
 
 
public  class  COLOR  extends   JFrame
{
   
  private   JFrame          myFrame;
    
  
   //  Constructor
   
   public COLOR (  )
   {
      
     
      setTitle  ( "Testing" );
      
      setSize   ( 400, 300 );
      
  
    
      setDefaultCloseOperation  ( JFrame.EXIT_ON_CLOSE );
      
  
     
      
      buildPanel ( );
      
      setVisible ( true );
   }    
  
    
   
      private  void  buildPanel ( )
      {
      
      
      
         setLayout ( new BorderLayout ( ) );
         
        getContentPane( ).setBackground( new Color  ( 255, 255, 100 ) );
      
      
      }
    
      
      
   
     
      // Main function
   
      public static void main ( String [ ] args )
      {
           new COLOR( );
      }
 
}

Open in new window

0
 

Author Comment

by:duta
ID: 24014242
I could change the color of panels, but not the entire frame.
Thank you so much for your kind, patient help.

Sincerely,
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24014243
Also, you can get rid of this line:

  private   JFrame          myFrame;

as it's now redundant.

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question