Set Background Color in JFrame - Swing

Here is a sample tutorial for setting the background color for a JFrame. By default, as in the AWT, a call to the setBackground(Color) method won't do the thing. Here is how-to achieve this.

Background Color for JFrame


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundColorJFrame extends JFrame
{
public BackgroundColorJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
// Get the container and set the background
getContentPane().setBackground(Color.lightGray);
}
public static void main(String args[])
{
new BackgroundColorJFrame();
}
}


Posted by at on

Tags: Swing Components,

-->