-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01b_toggle_button.java
More file actions
32 lines (27 loc) · 850 Bytes
/
01b_toggle_button.java
File metadata and controls
32 lines (27 loc) · 850 Bytes
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
import javax.swing.*;
import java.awt.event.*;
public class GUIApp {
public static void main(String[] args) {
// 1. Create application frame
JFrame frame = new JFrame("Cupertinii Swing GUI");
frame.setSize(500, 500);
frame.setLayout(null);
frame.setVisible(true);
// 2. Add a button with its title and set its position and size
JToggleButton btn = new JToggleButton("Enabled");
btn.setBounds(200, 200, 100, 50);
// 3. Add function to handle click event
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked");
if( btn.isSelected() == true) {
btn.setText("Disabled");
} else {
btn.setText("Enabled");
}
}
});
// 4. Finally , add btn to Frame
frame.add(btn);
}
}