-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioButtons.java
47 lines (38 loc) · 1.03 KB
/
RadioButtons.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
import java.awt.event.*;
import javax.swing.*;
public class RadioButtons implements ActionListener
{
JRadioButton r1,r2;
JFrame newFrame = new JFrame();
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == r1)
JOptionPane.showMessageDialog(newFrame, "You are a male!");
if( e.getSource() == r2)
JOptionPane.showMessageDialog(newFrame, "You are a female!");
}
RadioButtons()
{
JFrame frame = new JFrame("Choose your gender");
frame.setSize(400,400);
frame.setResizable(false);
frame.setLayout(null);
r1 = new JRadioButton("Male");
r2 = new JRadioButton("Female");
r1.setBounds(90,100,100,20);
r2.setBounds(90,150,100,20);
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
r1.addActionListener(this);
r2.addActionListener(this);
frame.add(r1);
frame.add(r2);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new RadioButtons();
}
}