Java JButton Example with ActionListener
1. import [Link].*;
2. import [Link].*;
3. public class ButtonExample {
4. public static void main(String[] args) { JFrame f=new JFrame("Button Exampl
e");
5. final JTextField tf=new JTextField(); [Link](50,50, 150,20);
6. JButton b=new JButton("Click Here"); [Link](50,100,95,30);
7. [Link](new ActionListener(){
8. public void actionPerformed(ActionEvent e){
9. [Link]("Welcome to Javatpoint.");
10. }
11. });
12. [Link](b);[Link](tf); [Link](400,400);
13. [Link](null); [Link](true);
14. } }
Output:
Example of displaying image on the button:
1. import [Link].*;
2. public class ButtonExample{
3. ButtonExample(){ JFrame f=new JFrame("Button Example");
4. JButton b=new JButton(new ImageIcon("D:\\[Link]"));
5. [Link](100,100,100, 40);
6. [Link](b); [Link](300,400); [Link](null);
7. [Link](true);
8. [Link](JFrame.EXIT_ON_CLOSE);
9. }
10. public static void main(String[] args) { new ButtonExample();
11. } }
Java JLabel Example
12. import [Link].*;
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."); [Link](50,50, 100,30);
18. l2=new JLabel("Second Label."); [Link](50,100, 100,30);
19. [Link](l1); [Link](l2); [Link](300,300);
20. [Link](null); [Link](true);
21. } }
1
Java JTextField Example
1. import [Link].*;
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. [Link](50,100, 200,30);
10. t2=new JTextField("AWT Tutorial");
11. [Link](50,150, 200,30);
12. [Link](t1); [Link](t2);
13. [Link](400,400);
14. [Link](null);
15. [Link](true);
16. }
17. }
Output:
Java JTextField Example with ActionListener
1. import [Link].*;
2. import [Link].*;
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(); [Link](50,50,150,20);
9. tf2=new JTextField(); [Link](50,100,150,20);
10. tf3=new JTextField(); [Link](50,150,150,20);
11. [Link](false);
12. b1=new JButton("+"); [Link](50,200,50,50);
13. b2=new JButton("-"); [Link](120,200,50,50);
14. [Link](this);
15. [Link](this);
16. [Link](tf1);[Link](tf2);[Link](tf3);[Link](b1);[Link](b2);
17. [Link](300,300);
18. [Link](null);
19. [Link](true); }
20. public void actionPerformed(ActionEvent e) {
21. String s1=[Link]();
22. String s2=[Link]();
23. int a=[Link](s1);
24. int b=[Link](s2);
25. int c=0;
26. if([Link]()==b1){
27. c=a+b;
28. }else if([Link]()==b2){ c=a-b; }
29. String result=[Link](c);
30. [Link](result); }
31. public static void main(String[] args)
2
32. { new TextFieldExample();
33. } }