-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.java
69 lines (58 loc) · 2.18 KB
/
GUI.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI implements ActionListener {
private JFrame frame;
private JPanel panel;
private JComboBox<String> choice;
private JCheckBox checkbox;
private JRadioButton radio1, radio2;
private ButtonGroup radioGroup;
private JButton submit;
public GUI() {
// Create the window and panel
frame = new JFrame("GUI");
panel = new JPanel();
panel.setLayout(new GridLayout(6, 1));
// Create the choice
String[] options = {"Option 1", "Option 2", "Option 3"};
choice = new JComboBox<String>(options);
// Create the checkbox
checkbox = new JCheckBox("Checkbox");
// Create the radio buttons
radio1 = new JRadioButton("Radio 1");
radio2 = new JRadioButton("Radio 2");
radioGroup = new ButtonGroup();
radioGroup.add(radio1);
radioGroup.add(radio2);
// Create the submit button and add an action listener
submit = new JButton("Submit");
submit.addActionListener(this);
// Add the components to the panel
panel.add(new JLabel("Select an option:"));
panel.add(choice);
panel.add(new JLabel("Check the box:"));
panel.add(checkbox);
panel.add(new JLabel("Select a radio button:"));
panel.add(radio1);
panel.add(radio2);
panel.add(submit);
// Set the frame properties
frame.add(panel);
frame.setSize(300, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// Get the selected options and print them
String selectedChoice = (String) choice.getSelectedItem();
boolean selectedCheckbox = checkbox.isSelected();
String selectedRadio = radio1.isSelected() ? "Radio 1" : "Radio 2";
System.out.println("Selected choice: " + selectedChoice);
System.out.println("Selected checkbox: " + selectedCheckbox);
System.out.println("Selected radio button: " + selectedRadio);
}
public static void main(String[] args) {
new GUI();
}
}