-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioButtonExample.java
More file actions
47 lines (36 loc) · 1.27 KB
/
Copy pathRadioButtonExample.java
File metadata and controls
47 lines (36 loc) · 1.27 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
import java.awt.*;
import java.awt.event.*;
public class RadioButtonExample extends Frame implements ActionListener {
RadioButton rb1, rb2;
Label label;
public RadioButtonExample() {
setTitle("Radio Button Example");
setLayout(new FlowLayout());
rb1 = new RadioButton("Option 1");
rb2 = new RadioButton("Option 2");
// Create a ButtonGroup to ensure only one radio button can be selected at a time
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
label = new Label("Selected Option: ");
// Add ActionListener to each radio button
rb1.addActionListener(this);
rb2.addActionListener(this);
add(rb1);
add(rb2);
add(label);
setSize(300, 200);
setVisible(true);
}
// Implement the actionPerformed method to handle the selection change
public void actionPerformed(ActionEvent e) {
if (rb1.isSelected()) {
label.setText("Selected Option: Option 1");
} else if (rb2.isSelected()) {
label.setText("Selected Option: Option 2");
}
}
public static void main(String args[]) {
new RadioButtonExample();
}
}