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

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

locking

DBMS Locking Part I (DBMS only) TECHNICAL ARTICLES -> PERFORMANCE ARTICLES [  Back  ] [  Next  ] DBMS is often criticized for excessive locking – resulting in poor database performance when sharing data among multiple concurrent processes. Is this criticism justified, or is DBMS being unfairly blamed for application design and implementation shortfalls? To evaluate this question, we need to understand more about DBMS locking protocols. In this article, we examine how, why, what and when DBMS locks and unlocks database resources. Future articles will address how to minimize the impact of database locking. THE NEED FOR LOCKING In an ideal concurrent environment, many processes can simultaneously access data in a DBMS database, each having the appearance that they have exclusive access to the database. In practice, this environment is closely approximated by careful use of locking protocols. Locking is necessary in a concurrent environment to as...

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