-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.java
More file actions
134 lines (93 loc) · 3.8 KB
/
sample.java
File metadata and controls
134 lines (93 loc) · 3.8 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Java program to show JRadioButton Example.
// in java. Importing different Package.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Chart extends JFrame {
// Declaration of object of JRadioButton class.
JRadioButton jRadioButton1;
// Declaration of object of JRadioButton class.
JRadioButton jRadioButton2;
// Declaration of object of JButton class.
JButton jButton;
// Declaration of object of ButtonGroup class.
ButtonGroup G1;
// Declaration of object of JLabel class.
JLabel L1;
// Constructor of Demo class.
public Chart()
{
// Setting layout as null of JFrame.
this.setLayout(null);
// Initialization of object of "JRadioButton" class.
jRadioButton1 = new JRadioButton();
// Initialization of object of "JRadioButton" class.
jRadioButton2 = new JRadioButton();
// Initialization of object of "JButton" class.
jButton = new JButton("Click");
// Initialization of object of "ButtonGroup" class.
G1 = new ButtonGroup();
// Initialization of object of " JLabel" class.
L1 = new JLabel("Qualification");
// setText(...) function is used to set text of radio button.
// Setting text of "jRadioButton2".
jRadioButton1.setText("Under-Graduate");
// Setting text of "jRadioButton4".
jRadioButton2.setText("Graduate");
// Setting Bounds of "jRadioButton2".
jRadioButton1.setBounds(120, 30, 120, 50);
// Setting Bounds of "jRadioButton4".
jRadioButton2.setBounds(250, 30, 80, 50);
// Setting Bounds of "jButton".
jButton.setBounds(125, 90, 80, 30);
// Setting Bounds of JLabel "L2".
L1.setBounds(20, 30, 150, 50);
// "this" keyword in java refers to current object.
// Adding "jRadioButton2" on JFrame.
this.add(jRadioButton1);
// Adding "jRadioButton4" on JFrame.
this.add(jRadioButton2);
// Adding "jButton" on JFrame.
this.add(jButton);
// Adding JLabel "L2" on JFrame.
this.add(L1);
// Adding "jRadioButton1" and "jRadioButton3" in a Button Group "G2".
G1.add(jRadioButton1);
G1.add(jRadioButton2);
// Adding Listener to JButton.
jButton.addActionListener(new ActionListener() {
// Anonymous class.
public void actionPerformed(ActionEvent e)
{
// Override Method
// Declaration of String class Objects.
String qual = " ";
// If condition to check if jRadioButton2 is selected.
if (jRadioButton1.isSelected()) {
qual = "Under-Graduate";
}
else if (jRadioButton2.isSelected()) {
qual = "Graduate";
}
else {
qual = "NO Button selected";
}
// MessageDialog to show information selected radion buttons.
JOptionPane.showMessageDialog(Chart.this, qual);
}
});
}
}
class RadioButton {
// Driver code.
public static void main(String args[])
{ // Creating object of demo class.
Chart f = new Chart();
// Setting Bounds of JFrame.
f.setBounds(100, 100, 400, 200);
// Setting Title of frame.
f.setTitle("RadioButtons");
// Setting Visible status of frame as true.
f.setVisible(true);
}
}