|
| |
We can set the background color, too, by calling the setBackground
method:
package swingExamples;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
class BackgroundColorDisplayPanel extends JPanel
{
public BackgroundColorDisplayPanel()
{
setBackground(Color.yellow);
}
}
class BackgroundColorDisplayFrame extends JFrame
{
public BackgroundColorDisplayFrame()
{
setTitle("Background Color Display");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.add(new BackgroundColorDisplayPanel());
}
}
public class BackgroundColorDisplay
{
public static void main(String[] args)
{
BackgroundColorDisplayFrame frame =
new BackgroundColorDisplayFrame();
frame.setVisible(true);
}
}
|
Which produces:

Note that, in the above program, we set the background color for the BackgroundColorDisplayPanel
class, not for the BackgroundColorDisplayFrame
class.
Why?
|