-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadio_Button_Test_1.java
More file actions
94 lines (75 loc) · 3.07 KB
/
Radio_Button_Test_1.java
File metadata and controls
94 lines (75 loc) · 3.07 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
// Creating radio buttons using ButtonGroup and JRadioButton.
package Radio_Button_Test_1;
// Java core packages
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class Radio_Button_Test_1 extends JFrame {
private JTextField field;
private Font plainFont, boldFont, italicFont, boldItalicFont;
private JRadioButton plainButton, boldButton, italicButton, boldItalicButton;
private ButtonGroup radioGroup;
// create GUI and fonts
public Radio_Button_Test_1() {
super("RadioButton Test");
// get content pane and set its layout
Container container = getContentPane();
container.setLayout(new FlowLayout());
// set up JTextField
field = new JTextField("Watch the font style change", 25);
container.add(field);
// create radio buttons
plainButton = new JRadioButton("Plain", true);
container.add(plainButton);
boldButton = new JRadioButton("Bold", false);
container.add(boldButton);
italicButton = new JRadioButton("Italic", false);
container.add(italicButton);
boldItalicButton = new JRadioButton("Bold/Italic", false);
container.add(boldItalicButton);
// register events for JRadioButtons
RadioButtonHandler handler = new RadioButtonHandler();
plainButton.addItemListener(handler);
boldButton.addItemListener(handler);
italicButton.addItemListener(handler);
boldItalicButton.addItemListener(handler);
// create logical relationship between JRadioButtons
radioGroup = new ButtonGroup();
radioGroup.add(plainButton);
radioGroup.add(boldButton);
radioGroup.add(italicButton);
radioGroup.add(boldItalicButton);
// create font objects
plainFont = new Font("Serif", Font.PLAIN, 14);
boldFont = new Font("Serif", Font.BOLD, 14);
italicFont = new Font("Serif", Font.ITALIC, 14);
boldItalicFont = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
field.setFont(plainFont);
setSize(300, 100);
setVisible(true);
}
// execute application
public static void main(String args[]) {
Radio_Button_Test_1 application = new Radio_Button_Test_1();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// private inner class to handle radio button events
private class RadioButtonHandler implements ItemListener {
// handle radio button events
public void itemStateChanged(ItemEvent event) {
// user clicked plainButton
if (event.getSource() == plainButton)
field.setFont(plainFont);
// user clicked boldButton
else if (event.getSource() == boldButton)
field.setFont(boldFont);
// user clicked italicButton
else if (event.getSource() == italicButton)
field.setFont(italicFont);
// user clicked boldItalicButton
else if (event.getSource() == boldItalicButton)
field.setFont(boldItalicFont);
}
}
}