SEARCH HERE

Saturday, December 17, 2022

Java AWT Label

Java AWT Label

The object of the Label 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 a programmer but a user cannot edit it directly.

It is called a passive control as it does not create any event when it is accessed. To create a label, we need to create the object of Label class.

AWT Label Class Declaration

  1. public class Label extends Component implements Accessible  

AWT Label Fields

The java.awt.Component class has following fields:

  1. static int LEFT: It specifies that the label should be left justified.
  2. static int RIGHT: It specifies that the label should be right justified.
  3. static int CENTER: It specifies that the label should be placed in center.

Label class Constructors

Sr. no.ConstructorDescription
1.Label()It constructs an empty label.
2.Label(String text)It constructs a label with the given string (left justified by default).
3.Label(String text, int alignement)It constructs a label with the specified string and the specified alignment.

Label Class Methods

Specified

Sr. no.Method nameDescription
1.void setText(String text)It sets the texts for label with the specified text.
2.void setAlignment(int alignment)It sets the alignment for label with the specified alignment.
3.String getText()It gets the text of the label
4.int getAlignment()It gets the current alignment of the label.
5.void addNotify()It creates the peer for the label.
6.AccessibleContext getAccessibleContext()It gets the Accessible Context associated with the label.
7.protected String paramString()It returns the string the state of the label.

Method inherited

The above methods are inherited by the following classes:

  • java.awt.Component
  • java.lang.Object

Java AWT Label Example

In the following example, we are creating two labels l1 and l2 using the Label(String text) constructor and adding them into the frame.

LabelExample.java

import java.awt.*;    

  

public class LabelExample {    

public static void main(String args[]){   

  

    // creating the object of Frame class and Label class  

    Frame f = new Frame ("Label example");  

    Label l1, l2;    

  

    // initializing the labels   

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

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

  

    // set the location of label  

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

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

  

    // adding labels to the frame    

    f.add(l1);  

    f.add(l2);   

  

    // setting size, layout and visibility of frame   

    f.setSize(400,400);    

    f.setLayout(null);    

    f.setVisible(true);    

}    

Output:

Java AWT Label

Java AWT Label Example with ActionListener

In the following example, we are creating the objects of TextField, Label and Button classes and adding them to the Frame. Using the actionPerformed() method an event is generated over the button. When we add the website in the text field and click on the button, we get the IP address of website.

LabelExample2.java

  1. import java.awt.*;    
  2. import java.awt.event.*;    
  3.   
  4. // creating class which implements ActionListener interface and inherits Frame class  
  5. public class LabelExample2 extends Frame implements ActionListener{    
  6.       
  7.     // creating objects of TextField, Label and Button class  
  8.     TextField tf;   
  9.     Label l;   
  10.     Button b;   
  11.   
  12.     // constructor to instantiate the above objects  
  13.     LabelExample2() {    
  14.         tf = new TextField();    
  15.         tf.setBounds(505015020);   
  16.   
  17.         l = new Label();    
  18.         l.setBounds(5010025020);    
  19.   
  20.         b = new Button("Find IP");    
  21.         b.setBounds(50,150,60,30);    
  22.         b.addActionListener(this);   
  23.   
  24.         add(b);  
  25.         add(tf);  
  26.         add(l);   
  27.   
  28.         setSize(400,400);    
  29.         setLayout(null);    
  30.         setVisible(true);    
  31.     }    
  32.   
  33.     // defining actionPerformed method to generate an event  
  34.     public void actionPerformed(ActionEvent e) {    
  35.         try {    
  36.         String host = tf.getText();    
  37.         String ip = java.net.InetAddress.getByName(host).getHostAddress();    
  38.         l.setText("IP of "+host+" is: "+ip);    
  39.         }   
  40.         catch (Exception ex) {  
  41.             System.out.println(ex);  
  42.         }    
  43.     }    
  44.   
  45.     // main method  
  46.     public static void main(String[] args) {    
  47.         new LabelExample2();    
  48.     }    
  49. }    

Output:

Java AWT Label

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top