Java: JLabel font and color

The most user-friendly interfaces are usually obtained by using the default appearance (font, color, background), but there are cases where you want to change these.

Appearance: setting the font

The font of a JLabel can be changed like this.

JLabel title = new JLabel("Want a Raise?", JLabel.CENTER);
title.setFont(new Font("Serif", Font.BOLD, 48));

Appearance: setting the text color

Use the setForeground method to set the text color.

JLabel title = new JLabel("Want a Raise?", JLabel.CENTER);
title.setForeground(Color.white);

Appearance: setting the background color

Because a JLabel's background is transparent, there is no effect from using the setBackground method. To make a new background, you need to create a JPanel with the appropriate color and put the label on that. For example

JLabel title = new JLabel("Want a Raise?");
title.setForeground(Color.white);
JPanel titlePanel = new JPanel();
titlePanel.setBackground(Color.blue);
titlePanel.add(title);  // adds to center of panel's default BorderLayout.