-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradiobutton.java
91 lines (79 loc) · 2.19 KB
/
radiobutton.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
class radiobutton implements ActionListener{
JFrame frame;
JPanel panel;
JRadioButton r1,r2,r3,r4;
ButtonGroup bg;
JButton show;
JLabel footer_text;
radiobutton()
{
//Creating instance of JFrame
frame = new JFrame();
//Creating instance of JPanel
panel = new JPanel();
footer_text = new JLabel();
//creating RadioButtons
r1 = new JRadioButton("English");
r2 = new JRadioButton("German");
r3 = new JRadioButton("French");
r4 = new JRadioButton("Spanish");
//adding radiobuttons to buttongroup
bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
bg.add(r4);
show = new JButton("Show");
show.addActionListener(this);
//adding radiobuttons to panel
panel.add(r1);
panel.add(r2);
panel.add(r3);
panel.add(r4);
panel.add(show);
panel.add(footer_text);
//adding panel to frame
frame.add(panel);
//layout set to BoxLayout
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
//This method sets the width and height of the frame
frame.setSize(400,400);
frame.setVisible(true);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String msg="";
if(r1.isSelected())
{
msg = "You selected English";
}
else if(r2.isSelected())
{
msg = "You selected German";
}
else if(r3.isSelected())
{
msg = "You selected French";
}
else if(r4.isSelected())
{
msg = "You selected Spanish";
}
footer_text.setText(msg);
}
public static void main(String[] args)
{
new radiobutton();
}
}