Header javaperspective.com

5.9. Labels
Last updated: 25 January 2013.

This tutorial will show you how to use labels in Java.

A label is an instance of the class JLabel. It is a component which contains uneditable text and/or icon. You can set the font, foreground color, background color and border of a label as you like. The following picture shows a frame containing several labels:


Labels



The code follows:


import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public final class Labels extends JFrame {

   
public Labels() {
         
init();
          addComponents
();

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


   
private void addComponents() {
         
// Create the content pane
         
JPanel contentPane = new JPanel(new GridLayout(0, 1));

          JLabel label1 =
new JLabel("A simple label");
          contentPane.add
(label1);

          JLabel label2 =
new JLabel("A label with centered text and raised border", SwingConstants.CENTER);
          label2.setBorder
(BorderFactory.createBevelBorder(BevelBorder.RAISED));
          contentPane.add
(label2);

          JLabel label3 =
new JLabel("A label with italic font and red foreground");
          label3.setFont
(new Font("Times New Roman", Font.ITALIC, 20));
          label3.setForeground
(Color.RED);
          contentPane.add
(label3);

          ImageIcon icon =
new ImageIcon("../images/icon.gif");
          JLabel label4 =
new JLabel("A label with centered trailing text and icon", icon, SwingConstants.CENTER);
          contentPane.add
(label4);

          JLabel label5 =
new JLabel("A label with centered leading text and icon", icon, SwingConstants.CENTER);
          label5.setHorizontalTextPosition
(JLabel.LEADING);
          contentPane.add
(label5);

          JLabel label6 =
new JLabel(icon); // A label with a centered icon and no text
         
contentPane.add(label6);

          JLabel label7 =
new JLabel("A label with centered text and green background", SwingConstants.CENTER);
          label7.setOpaque
(true);
          label7.setBackground
(Color.GREEN);
          contentPane.add
(label7);

         
// Add the content pane to the JFrame
         
add(contentPane);
   
}


   
private void init() {
         
setTitle("Labels");
          setSize
(400, 400);
          setLocationRelativeTo
(null);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new Labels();
               
}
          })
;
   
}
}


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.9. Labels
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.10. Text Fields

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7