JLabel Component In Java | Free Source Code

JLabel Component in Java

jlabel component in java

This is a tutorial in which we will going to design a program that has the JLabel Component using Java. The JLabel lets the user display a shorten text string or an image, or both. You cannot enter a text on JLabel and cannot have the keyboard focus on it.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of jLabelComponent.java.

2. Import the following packages below:

import java.awt.*; // used to access the FlowLayout Class
import javax.swing.*; //used to access the JFrame and JLabel class

 

3. Initialize your variable in your Main, variable frame for creating JFrame, and variable label1 to label5 for your JLabel component.

JFrame frame = new JFrame("JLabel Component");
 
JLabel label1 = new JLabel("Student ID:", JLabel.LEFT);
JLabel label2 = new JLabel("First Name:", JLabel.RIGHT);
JLabel label3 = new JLabel("Last Name:", JLabel.RIGHT);
JLabel label4 = new JLabel("Age:", JLabel.LEFT);
JLabel label5 = new JLabel("Student Information", JLabel.CENTER);

 

As you can see, I make 5 labels with their corresponding text and alignments. I used the parameter of the JLabel which is the JLabel(String text, int horizontal alignment). And also you can put an image on using the ImageIcon class. The JLabel.LEFT shows the alignment to left, JLabel.RIGHT shows the alignment to left and JLabel.CENTER shows the alignment to the center.

Now, to set the vertical alignment of the label, use the setVerticalAlignment method then use the center orientation in the label5 variable. To have the tooltip in the label, we will use the setToolTipText method.

label5.setVerticalAlignment(JLabel.CENTER);
label5.setToolTipText("This is a label!");

When running the program, this image will be the first to be shown.

output

4. To add the labels to the frame, copy this code below:

frame.getContentPane().add(label1);
frame.getContentPane().add(label2);
frame.getContentPane().add(label3);
frame.getContentPane().add(label4);
frame.getContentPane().add(label5);

 

5. Finally, set the size, visibility, and the close operation of the frame. Have the FlowLayout as the layout manager of the frame. opy this code below:

   frame.setSize(300, 300);
   frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());

 

Output:

output


Here’s the full code of this tutorial:

import java.awt.*; // used to access the FlowLayout Class
import javax.swing.*; //used to access the JFrame and JLabel class
 
 
public class jLabelComponent {
 
  public static void main(String[] args) {
  	JFrame frame = new JFrame("JLabel Component");
 
    JLabel label1 = new JLabel("Student ID:", JLabel.LEFT);
    JLabel label2 = new JLabel("First Name:", JLabel.RIGHT);
    JLabel label3 = new JLabel("Last Name:", JLabel.RIGHT);
    JLabel label4 = new JLabel("Age:", JLabel.LEFT);
    JLabel label5 = new JLabel("Student Information", JLabel.CENTER);
 
    label5.setVerticalAlignment(JLabel.CENTER);
    label5.setToolTipText("This is a label!");
 
    frame.getContentPane().add(label1);
    frame.getContentPane().add(label2);
    frame.getContentPane().add(label3);
    frame.getContentPane().add(label4);
    frame.getContentPane().add(label5);
 
       frame.setSize(300, 300);
       frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new FlowLayout());
  }
}

 

 

Facebook Comments

Leave a Reply

Your email address will not be published.