-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionTester.java
More file actions
56 lines (50 loc) · 1.18 KB
/
ActionTester.java
File metadata and controls
56 lines (50 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ActionTester extends JFrame implements ActionListener
{
JButton b1,b2;
JLabel l1;
int clicked;
final String str="Number of Times button Clicked=";
public ActionTester(String strName)
{
super(strName);
getContentPane().setLayout(new FlowLayout());
JPanel p=new JPanel();
p.setBackground(Color.red);
b1=new JButton("Click Me");
b2=new JButton("Quit");
p.setLayout(new FlowLayout());
p.add(b1);
p.add(b2);
getContentPane().add(p);
//-------------------------------
l1=new JLabel(str+clicked ,JLabel.CENTER);
getContentPane().add(l1);
//-----------------------------------
//step3
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)//step4
{
String s=e.getActionCommand();
System.out.println(s);
if("Click Me".equals(s))
{
clicked++;
l1.setText(str+clicked);
}
if("Quit".equals(s))
{
System.exit(0);
}
}
public static void main(String args[])
{
ActionTester t=new ActionTester("Action Event");
t.setSize(300,300);
t.setVisible(true);
}
}