Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class RadioButtonExample extends JFrame {
- public RadioButtonExample() {
- setTitle("Radio Button Example");
- setSize(300, 200);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JPanel panel = new JPanel();
- add(panel);
- ButtonGroup group = new ButtonGroup();
- JRadioButton radio1 = new JRadioButton("Option 1");
- group.add(radio1);
- panel.add(radio1);
- JRadioButton radio2 = new JRadioButton("Option 2");
- group.add(radio2);
- panel.add(radio2);
- JRadioButton radio3 = new JRadioButton("Option 3");
- group.add(radio3);
- panel.add(radio3);
- ActionListener listener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("Selected option: " + ((JRadioButton)e.getSource()).getText());
- }
- };
- radio1.addActionListener(listener);
- radio2.addActionListener(listener);
- radio3.addActionListener(listener);
- }
- }
- public class GUI{
- public static void main(String[] args) {
- RadioButtonExample example = new RadioButtonExample();
- example.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement