-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHandleEvent1.java
42 lines (34 loc) · 1.12 KB
/
HandleEvent1.java
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
package programs;
import javax.swing.*;
import java.awt.event.*;
public class HandleEvent1 extends JFrame {
JButton jbtOK = new JButton("OK");
JButton jbtCancel = new JButton("Cancel");
public HandleEvent1() {
// Create a panel to hold buttons
JPanel panel = new JPanel();
panel.add(jbtOK);
panel.add(jbtCancel);
add(panel); // Add panel to the frame
// Register listeners
ListenerClass listener = new ListenerClass();
jbtOK.addActionListener(listener);
jbtCancel.addActionListener(listener);
}
public static void main(String[] args) {
JFrame frame = new HandleEvent1();
frame.setTitle("Handle Event");
frame.setSize(400, 400);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class ListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtOK)
System.out.println("OK button clicked");
else if (e.getSource() == jbtCancel)
System.out.println("Cancel button clicked");
}
}
}