JFrame color - How to set the JFrame background color

JFrame color FAQ: How do I set the JFrame background color?

In general, to set the JFrame background color, just call the JFrame setBackground method, like this:

jframe.setBackground(Color.RED);

Note that there are many more things you can do with the Java Color class, including:

  • Specifying RGB values.
  • Using methods like lighter, darker, or brighter.

There are also other Color class methods to get color components, and much more.

A complete JFrame background color example

Getting back to the JFrame color example ... if you want to test this on your own computer, here's the source code for a complete JFrame background color example:

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)
  {
    // schedule this for the event dispatch thread (edt)
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        displayJFrame();
      }
    });
  }

  static void displayJFrame()
  {
    // create our jframe as usual
    JFrame jframe = new JFrame("JFrame Background Color");
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jframe.setBackground(Color.red);

    // set the jframe size and location, and make it visible
    jframe.setPreferredSize(new Dimension(400, 300));
    jframe.pack();
    jframe.setLocationRelativeTo(null);
    jframe.setVisible(true);
  }

}

Other color notes

As a final note, here are a few links to the Java classes used in this example:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.