Java JLabel

Java JLabel

The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class.

JLabel class declaration

Let’s see the declaration for javax.swing.JLabel class.

  1. public class JLabel extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:

Constructor Description
JLabel() Creates a JLabel instance with no image and with an empty string for the title.
JLabel(String s) Creates a JLabel instance with the specified text.
JLabel(Icon i) Creates a JLabel instance with the specified image.
JLabel(String s, Icon i, int horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment.

Commonly used Methods:

Methods Description
String getText() t returns the text string that a label displays.
void setText(String text) It defines the single line of text this component will display.
void setHorizontalAlignment(int alignment) It sets the alignment of the label’s contents along the X axis.
Icon getIcon() It returns the graphic image that the label displays.
int getHorizontalAlignment() It returns the alignment of the label’s contents along the X axis.

 

Java JLabel Example

  1. import javax.swing.*;
  2. class LabelExample
  3. {
  4. public static void main(String args[])
  5.     {
  6.     JFrame f= new JFrame(“Label Example”);
  7.     JLabel l1,l2;
  8.     l1=new JLabel(“First Label.”);
  9.     l1.setBounds(50,50100,30);
  10.     l2=new JLabel(“Second Label.”);
  11.     l2.setBounds(50,100100,30);
  12.     f.add(l1); f.add(l2);
  13.     f.setSize(300,300);
  14.     f.setLayout(null);
  15.     f.setVisible(true);
  16.     }
  17.     }

Output:

JAVA Jlabel 1


Java JLabel Example with ActionListener

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class LabelExample extends Frame implements ActionListener{
  5.     JTextField tf; JLabel l; JButton b;
  6.     LabelExample(){
  7.         tf=new JTextField();
  8.         tf.setBounds(50,50150,20);
  9.         l=new JLabel();
  10.         l.setBounds(50,100250,20);
  11.         b=new JButton(“Find IP”);
  12.         b.setBounds(50,150,95,30);
  13.         b.addActionListener(this);
  14.         add(b);add(tf);add(l);
  15.         setSize(400,400);
  16.         setLayout(null);
  17.         setVisible(true);
  18.     }
  19.     public void actionPerformed(ActionEvent e) {
  20.         try{
  21.         String host=tf.getText();
  22.         String ip=java.net.InetAddress.getByName(host).getHostAddress();
  23.         l.setText(“IP of “+host+” is: “+ip);
  24.         }catch(Exception ex){System.out.println(ex);}
  25.     }
  26.     public static void main(String[] args) {
  27.         new LabelExample();
  28.     } }

Output:

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java JLabel The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class. JLabel class declaration Let’s see the declaration for […]

Tags:

....
...