Search Your Topic

config
Tuesday, 01 August 2017 06:18

Java AWT Label

Written by
Rate this item
(0 votes)

The object of 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 an application but a user cannot edit it directly.

AWT Label Class Declaration

 
  1. public class Label extends Component implements Accessible  

Java Label Example

 
  1. import java.awt.*;  
  2. class LabelExample{  
  3. public static void main(String args[]){  
  4.     Frame f= new Frame("Label Example");  
  5.     Label l1,l2;  
  6.     l1=new Label("First Label.");  
  7.     l1.setBounds(50,100100,30);  
  8.     l2=new Label("Second Label.");  
  9.     l2.setBounds(50,150100,30);  
  10.     f.add(l1); f.add(l2);  
  11.     f.setSize(400,400);  
  12.     f.setLayout(null);  
  13.     f.setVisible(true);  
  14. }  
  15. }  

Output:

java awt label example 1

Java AWT Label Example with ActionListener

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

Output:

TextField: www.toshiyas.in

IP Of www.toshiyas.in is 144.18.16.18

Button : Find IP

 

Read 270 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « Java AWT Button Java Swing »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Java Training

Facebook