Skip to main content

Java AWT Button, Label

Java AWT Button

The button class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed.

AWT Button Class declaration

  1. public class Button extends Component implements Accessible  

Java AWT Button Example

  1. import java.awt.*;  
  2. public class ButtonExample {  
  3. public static void main(String[] args) {  
  4.     Frame f=new Frame("Button Example");  
  5.     Button b=new Button("Click Here");  
  6.     b.setBounds(50,100,80,30);  
  7.     f.add(b);  
  8.     f.setSize(400,400);  
  9.     f.setLayout(null);  
  10.     f.setVisible(true);   
  11. }  
  12. }  


Java AWT Button Example with ActionListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class ButtonExample {  
  4. public static void main(String[] args) {  
  5.     Frame f=new Frame("Button Example");  
  6.     final TextField tf=new TextField();  
  7.     tf.setBounds(50,50150,20);  
  8.     Button b=new Button("Click Here");  
  9.     b.setBounds(50,100,60,30);  
  10.     b.addActionListener(new ActionListener(){  
  11.     public void actionPerformed(ActionEvent e){  
  12.             tf.setText("Welcome to Javatpoint.");  
  13.         }  
  14.     });  
  15.     f.add(b);f.add(tf);  
  16.     f.setSize(400,400);  
  17.     f.setLayout(null);  
  18.     f.setVisible(true);   
  19. }  
  20. }  


ava AWT Label

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. }  

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. }  
Anurag Rana

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&