Page 216 - CTS - CSA TP - Volume 2
P. 216
COMPUTER SOFTWARE APPLICATION - CITS
JRadioButton radioButton2 = new JRadioButton(“Female”);
JButton submitButton = new JButton(“Submit”);
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedOption = radioButton1.isSelected() ? “Male” : “Female”;
JOptionPane.showMessageDialog(frame, “Selected Option: “ + selectedOption);
}
});
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.getContentPane().add(radioButton1);
frame.getContentPane().add(radioButton2);
frame.getContentPane().add(submitButton);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation:
1 Import Statements:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Import necessary packages for Swing components and event handling.
2 Class Definition:
public class RadioButtonControlExample {
Define a class named RadioButtonControlExample.
3 Main Method:
public static void main(String[] args) {
Start the main method.
201
CITS : IT & ITES - Computer Software Application - Exercise 117