-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSwingEventDemo.java
More file actions
59 lines (50 loc) · 1.76 KB
/
SwingEventDemo.java
File metadata and controls
59 lines (50 loc) · 1.76 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
57
58
59
package src.uni.lessons.jswing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingEventDemo extends JFrame implements ActionListener {
private JLabel jLabel;
private JButton jButtonAlpha, jButtonBeta;
public SwingEventDemo(String title) {
super(title);
}
// user defined class to create frame
public void createFrame() {
// JFrame configurations
setLayout(new FlowLayout());
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Adding components like labels and button to the frame
jLabel = new JLabel("Press a button");
jButtonAlpha = new JButton("Alpha");
jButtonBeta = new JButton("Beta");
// attaching event listener
jButtonAlpha.addActionListener(this);
jButtonBeta.addActionListener(this);
// finally adding the components to the frame
// Note: since layout is set to flow layout, components
// appear in the order in which they are added.
add(jLabel);
add(jButtonAlpha);
add(jButtonBeta);
}
// override the abstract method of the ActionListener interface
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButtonAlpha) {
jLabel.setText("Alpha was clicked");
}
if (e.getSource() == jButtonBeta) {
jLabel.setText("Beta was clicked");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// creating SwingEventDemo object and calling createFrame method.
new SwingEventDemo("Swing Event Demo").createFrame();
}
});
}
}