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

Java Jbutton Example With Actionlistener: "Button Exampl E" "Click Here"

This document provides code examples for using buttons, labels, and text fields in Java Swing. It includes examples of: - Creating a button and adding an action listener to update a text field when clicked - Displaying an image on a button - Creating and positioning labels - Creating and positioning text fields - Creating a text field example with action listeners on buttons to perform addition and subtraction and display the result

Uploaded by

IT LAb 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Java Jbutton Example With Actionlistener: "Button Exampl E" "Click Here"

This document provides code examples for using buttons, labels, and text fields in Java Swing. It includes examples of: - Creating a button and adding an action listener to update a text field when clicked - Displaying an image on a button - Creating and positioning labels - Creating and positioning text fields - Creating a text field example with action listeners on buttons to perform addition and subtraction and display the result

Uploaded by

IT LAb 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java JButton Example with ActionListener

1. import java.awt.event.*;  
2. import javax.swing.*;    
3. public class ButtonExample {  
4. public static void main(String[] args) {      JFrame f=new JFrame("Button Exampl
e");  
5.     final JTextField tf=new JTextField();      tf.setBounds(50,50, 150,20);  
6.     JButton b=new JButton("Click Here");      b.setBounds(50,100,95,30);  
7.     b.addActionListener(new ActionListener(){  
8. public void actionPerformed(ActionEvent e){  
9.             tf.setText("Welcome to Javatpoint.");  
10.         }  
11.     });  
12.     f.add(b);f.add(tf);     f.setSize(400,400);  
13.     f.setLayout(null);      f.setVisible(true);   
14. }  }  

Output:

Example of displaying image on the button:


1. import javax.swing.*;      
2. public class ButtonExample{    
3. ButtonExample(){    JFrame f=new JFrame("Button Example");            
4. JButton b=new JButton(new ImageIcon("D:\\icon.png"));    
5. b.setBounds(100,100,100, 40);    
6. f.add(b);    f.setSize(300,400);    f.setLayout(null);    
7. f.setVisible(true);    
8. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
9.     }         
10. public static void main(String[] args) {        new ButtonExample();    
11. }    }    

Java JLabel Example


12. import javax.swing.*;  
13. class LabelExample  
14. {  public static void main(String args[])  
15.     {      JFrame f= new JFrame("Label Example");  
16.     JLabel l1,l2;  
17.     l1=new JLabel("First Label.");      l1.setBounds(50,50, 100,30);   
18.    l2=new JLabel("Second Label.");   l2.setBounds(50,100, 100,30);  
19.     f.add(l1); f.add(l2);      f.setSize(300,300);  
20.     f.setLayout(null);      f.setVisible(true);
21.    }      }  

1
Java JTextField Example
1. import javax.swing.*;  
2. class TextFieldExample  
3. {  
4. public static void main(String args[])  
5.     {  
6.     JFrame f= new JFrame("TextField Example");  
7.     JTextField t1,t2;  
8.     t1=new JTextField("Welcome to Javatpoint.");  
9.     t1.setBounds(50,100, 200,30);  
10.     t2=new JTextField("AWT Tutorial");  
11.     t2.setBounds(50,150, 200,30);  
12.     f.add(t1); f.add(t2);  
13.     f.setSize(400,400);  
14.     f.setLayout(null);  
15.     f.setVisible(true);  
16.     }  
17.     }  

Output:

Java JTextField Example with ActionListener


1. import javax.swing.*;  
2. import java.awt.event.*;  
3. public class TextFieldExample implements ActionListener{  
4.     JTextField tf1,tf2,tf3;  
5.     JButton b1,b2;  
6.     TextFieldExample(){  
7.         JFrame f= new JFrame();  
8.         tf1=new JTextField();          tf1.setBounds(50,50,150,20);  
9.         tf2=new JTextField();          tf2.setBounds(50,100,150,20);  
10.         tf3=new JTextField();          tf3.setBounds(50,150,150,20);  
11.         tf3.setEditable(false);   
12.         b1=new JButton("+");          b1.setBounds(50,200,50,50);  
13.         b2=new JButton("-");          b2.setBounds(120,200,50,50);  
14.         b1.addActionListener(this);  
15.         b2.addActionListener(this);  
16.         f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);  
17.         f.setSize(300,300);  
18.         f.setLayout(null);  
19.         f.setVisible(true);      }         
20.     public void actionPerformed(ActionEvent e) {  
21.         String s1=tf1.getText();  
22.         String s2=tf2.getText();  
23.         int a=Integer.parseInt(s1);  
24.         int b=Integer.parseInt(s2);  
25.         int c=0;  
26.         if(e.getSource()==b1){  
27.             c=a+b;  
28.         }else if(e.getSource()==b2){              c=a-b;         }  
29.          String result=String.valueOf(c);  
30.         tf3.setText(result);       }  
31. public static void main(String[] args)

2
32.  {      new TextFieldExample();  
33. } }  

You might also like