forked from Rustam-Z/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandleEvent.java
More file actions
46 lines (38 loc) · 1.19 KB
/
HandleEvent.java
File metadata and controls
46 lines (38 loc) · 1.19 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
package programs;
import javax.swing.*;
import java.awt.event.*;
public class HandleEvent extends JFrame {
public HandleEvent() {
// Create two buttons
JButton jbtOK = new JButton("OK");
JButton jbtCancel = new JButton("Cancel");
// 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
OKListenerClass listener1 = new OKListenerClass();
CancelListenerClass listener2 = new CancelListenerClass();
jbtOK.addActionListener(listener1);
jbtCancel.addActionListener(listener2);
}
public static void main(String[] args) {
JFrame frame = new HandleEvent();
frame.setTitle("Handle Event");
frame.setSize(200, 150);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class OKListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("OK button clicked");
}
}
class CancelListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Cancel button clicked");
}
}