0% found this document useful (0 votes)
97 views

Java JLabel - Javatpoint

The JLabel class in Java is used to display read-only text or images in a container. It inherits from the JComponent class. JLabel allows setting text, icons, and horizontal alignment. Common methods include getText(), setText(), setIcon(), and getHorizontalAlignment(). Examples demonstrate creating JLabels and setting properties like bounds and layout.

Uploaded by

Arul Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Java JLabel - Javatpoint

The JLabel class in Java is used to display read-only text or images in a container. It inherits from the JComponent class. JLabel allows setting text, icons, and horizontal alignment. Common methods include getText(), setText(), setIcon(), and getHorizontalAlignment(). Examples demonstrate creating JLabels and setting properties like bounds and layout.

Uploaded by

Arul Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java JLabel - javatpoint https://www.javatpoint.

com/java-jlabel

1 of 6 2/2/2018, 3:58 PM
Java JLabel - javatpoint https://www.javatpoint.com/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.

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 Creates a JLabel instance with the specified text,
horizontalAlignment) 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 It sets the alignment of the label's contents


alignment) along the X axis.

2 of 6 2/2/2018, 3:58 PM
Java JLabel - javatpoint https://www.javatpoint.com/java-jlabel

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

import javax.swing.*;

class LabelExample

public static void main(String args[])

JFrame f= new JFrame("Label Example");

JLabel l1,l2;

l1=new JLabel("First Label.");

l1.setBounds(50,50, 100,30);

l2=new JLabel("Second Label.");

l2.setBounds(50,100, 100,30);

f.add(l1); f.add(l2);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

Output: ⇧

3 of 6 2/2/2018, 3:58 PM
Java JLabel - javatpoint https://www.javatpoint.com/java-jlabel

Java JLabel Example with ActionListener

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class LabelExample extends Frame implements ActionListener{

JTextField tf; JLabel l; JButton b;

LabelExample(){

tf=new JTextField();

tf.setBounds(50,50, 150,20);

l=new JLabel();

l.setBounds(50,100, 250,20);

b=new JButton("Find IP");

b.setBounds(50,150,95,30);

b.addActionListener(this);

add(b);add(tf);add(l);

setSize(400,400);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e) {

try{

String host=tf.getText();

String ip=java.net.InetAddress.getByName(host).getHostAddress();

l.setText("IP of "+host+" is: "+ip);

}catch(Exception ex){System.out.println(ex);} ⇧
}

public static void main(String[] args) {

4 of 6 2/2/2018, 3:58 PM
Java JLabel - javatpoint https://www.javatpoint.com/java-jlabel

new LabelExample();

}}

Output:

← prev next →

Please Share

Learn Latest Tutorials

On Mac Framework7

5 of 6 2/2/2018, 3:58 PM
Java JLabel - javatpoint https://www.javatpoint.com/java-jlabel

Phalcon Materialize

6 of 6 2/2/2018, 3:58 PM

You might also like