Results 1 to 5 of 5

Thread: How to set the background color of JFrame?

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    How to set the background color of JFrame?

    Hello friends,
    I am new to this forum. I am last year B.Sc.I.T. student. I am working on one live project where I am using Java as front end and SQL as back end. In my project I want to set the background color of JFrame. Can anyone tell me how to set the background color of JFrame? Please help me.
    Thanks in advanced.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to set the background color of JFrame?

    Normally to to set the background color of JFrame we use JFrame setBackground method. We have to just call this method to do this like below:
    jframe.setBackground(Color.GREEN);
    You can also do many more things with the Java Color class like Using methods like lighter, darker, or brighter and Specifying RGB values. Just use setBackground() method and pass proper value for various effects.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to set the background color of JFrame?

    I have written following code to set the background color of JFrame. It is very simple program. In this code I have use java.awt.Color class to include all color in our program. I also have use java.awt.Dimension class to assign various dimension to various buttos.

    Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class JFrameBackgroundColor
    {
    
      public static void main(String[] args)
      {
        SwingUtilitiess.invokeLaters(new Runnables()
        {
          public void runs()
          {
            displayJFrames();
          }
        });
      }
    
      static void displayJFrames()
      {
      
        JFrame jframes = new JFrames("chainging JFrame Background Color");
        jframes.setDefaultCloseOperation(JFrames.EXIT_ON_CLOSE);
    
        jframe.setBackground(Color.red);
    
       
        jframes.setPreferredSize(new Dimension(450, 350));
        jframes.pack();
        jframes.setLocationRelativeTo(null);
        jframes.setVisible(true);
      }
    
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to set the background color of JFrame?

    You have to use following code to set the background color of JFrame. It is very simple program. In the following code I have use TestEg class to include all methods. I also have use javax.swing. and java.awt.* class to import all color in our program. I have simply use setBackground(Color.RED) to apply red color.

    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class TestEg extends JFrame
    {
    public Test()
    {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setSize(520,560);
    this.setBackground(Color.RED);
    this.setVisible(true);
    }
    
    public static void main(String[] args)
    {
    new TestEg();
    }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to set the background color of JFrame?

    As per my knowledge following code is the simple program to set the background color of JFrame. It is very simple program. In the following code I have use java.awt.* class to show button on screen. I also have use javax.swing.* class to include all color. Just try to understand this program.

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class JFrameEg {
      public static void main(String[] args) {
        WindowUtilities.setNativeLookAndFeel();
        JFrame fs = new JFrame("This is example");
        fs.setSize(432, 165);
        Container content = fs.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout()); 
        content.add(new JButtons("Buttons 1"));
        content.add(new JButtons("Buttons 2"));
        content.add(new JButtons("Buttons 3"));
        fs.addWindowListener(new ExitListener());
        fs.setVisible(true);
      }
    }

Similar Threads

  1. Problem of overlapping JLabel on JFrame with background
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 14-12-2010, 02:09 PM
  2. Want to add text on a page having background color
    By Lakshmidhar in forum Software Development
    Replies: 4
    Last Post: 02-10-2010, 10:51 PM
  3. Change The Background Color Of The Jsp
    By rashmi_ay in forum Software Development
    Replies: 4
    Last Post: 27-02-2010, 08:22 PM
  4. How to put background color for html cell
    By Preetish in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 03:32 PM
  5. XP Color theme with dark window background color?
    By sayeed in forum Windows XP Support
    Replies: 4
    Last Post: 27-06-2008, 01:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,885,945.85951 seconds with 17 queries