how to add background color in jframe

how to add background color in jframe

To add a background color to a javax.swing.JFrame in Java, you can use the setBackground method of the JFrame class. This method sets the background color of the frame.

Here's an example of how you could use the setBackground method to add a background color to a JFrame:

import java.awt.Color;

import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBackground(Color.YELLOW); // Set the background color to yellow
    frame.setVisible(true);
  }
}
‮cruoS‬e:www.lautturi.com

In this example, the setBackground method is used to set the background color of the frame frame to yellow. You can use any color value that is supported by the Color class, such as a predefined constant, a hexadecimal value, or a combination of red, green, and blue values.

Keep in mind that the setBackground method only sets the background color of the frame's content pane. If you want to set the background color of the frame itself, including the title bar and the borders, you may need to use a different approach, such as setting the JPanel's background color or using a custom JFrame subclass.

You can also use the setForeground method to set the foreground color of the frame, which is used to render the text and other graphical elements.

Created Time:2017-11-01 12:05:10  Author:lautturi