To set the background color of a JFrame
in Java, you can use the setBackground()
method of the JFrame
class. Here's an example:
import javax.swing.JFrame; // ... JFrame frame = new JFrame(); // Set the background color to red frame.getContentPane().setBackground(java.awt.Color.RED); // Set the size and title of the frame frame.setSize(300, 300); frame.setTitle("My Frame"); // Make the frame visible frame.setVisible(true);
This will set the background color of the JFrame
to red. You can use any color you like by passing a Color
object to the setBackground()
method.
Alternatively, you can use the setBackground()
method of the JComponent
class, which is the superclass of JFrame
. This method can be called on any component, not just a JFrame
.
import javax.swing.JFrame; import javax.swing.JComponent; // ... JFrame frame = new JFrame(); // Set the background color to red ((JComponent) frame.getContentPane()).setBackground(java.awt.Color.RED); // Set the size and title of the frame frame.setSize(300, 300); frame.setTitle("My Frame"); // Make the frame visible frame.setVisible(true);