Academic Java    Java Tutorial  >  Swing  >  JLabel

JLabel Example

Java JLabel EXAMPLE output In this JLabel example a JLabel is added to a JFrame's content pane.

The JLabel has a background color.

For this to be shown the method JLabel.setOpaque() must be called for the label, with a true parameter.
// JLabel Example
import java.awt.*;
import javax.swing.*;

class JLabelExample {

   public static void main(String[] args) {

      JLabel label = new JLabel("billabong");
      label.setOpaque(true);
      label.setBackground(Color.green);

      JFrame frame = new JFrame();

      Container cp = frame.getContentPane();
      cp.setLayout(new FlowLayout());
      cp.setBackground(Color.yellow);

      cp.add(label);

      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}


* JLabel Examples