Changing the background of a JLabel

Call the method setBackground on your component but don’t forget to set your component to be opaque. A component that is not opaque (default) lets whatever is behind them show through. Their backgrounds are determined by the component that is shown behind them.

import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
      JLabel label = new JLabel("Colored label");
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
      setSize(300, 100);
   public static void main(String[] args) {
      (new Main()).show();