1 |
import java.awt.BorderLayout; |
2 |
import java.awt.Container; |
3 |
import java.awt.Font; |
4 |
import java.awt.GridLayout; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.WindowAdapter; |
8 |
import java.awt.event.WindowEvent; |
9 |
import javax.swing.ButtonGroup; |
10 |
import javax.swing.JButton; |
11 |
import javax.swing.JCheckBox; |
12 |
import javax.swing.JComboBox; |
13 |
import javax.swing.JFrame; |
14 |
import javax.swing.JLabel; |
15 |
import javax.swing.JPanel; |
16 |
import javax.swing.JRadioButton; |
17 |
import javax.swing.border.EtchedBorder; |
18 |
import javax.swing.border.TitledBorder; |
19 |
|
20 |
/** |
21 |
This frame contains a text field and a control panel |
22 |
to change the font of the text. |
23 |
*/ |
24 |
public class ChoiceFrame extends JFrame |
25 |
{ |
26 |
/** |
27 |
Constructs the frame. |
28 |
*/ |
29 |
public ChoiceFrame() |
30 |
{ |
31 |
// construct text sample |
32 |
sampleField = new JLabel("Big Java"); |
33 |
getContentPane().add(sampleField, BorderLayout.CENTER); |
34 |
|
35 |
// this listener is shared among all components |
36 |
class ChoiceListener implements ActionListener |
37 |
{ |
38 |
public void actionPerformed(ActionEvent event) |
39 |
{ |
40 |
setSampleFont(); |
41 |
} |
42 |
} |
43 |
|
44 |
listener = new ChoiceListener(); |
45 |
|
46 |
createControlPanel(); |
47 |
setSampleFont(); |
48 |
pack(); |
49 |
} |
50 |
|
51 |
/** |
52 |
Creates the control panel to change the font. |
53 |
*/ |
54 |
public void createControlPanel() |
55 |
{ |
56 |
JPanel facenamePanel = createComboBox(); |
57 |
JPanel sizeGroupPanel = createCheckBoxes(); |
58 |
JPanel styleGroupPanel = createRadioButtons(); |
59 |
|
60 |
// line up component panels |
61 |
|
62 |
JPanel controlPanel = new JPanel(); |
63 |
controlPanel.setLayout(new GridLayout(3, 1)); |
64 |
controlPanel.add(facenamePanel); |
65 |
controlPanel.add(sizeGroupPanel); |
66 |
controlPanel.add(styleGroupPanel); |
67 |
|
68 |
// add panels to content pane |
69 |
|
70 |
getContentPane().add(controlPanel, BorderLayout.SOUTH); |
71 |
} |
72 |
|
73 |
/** |
74 |
Creates the combo box with the font style choices. |
75 |
@return the panel containing the combo box |
76 |
*/ |
77 |
public JPanel createComboBox() |
78 |
{ |
79 |
facenameCombo = new JComboBox(); |
80 |
facenameCombo.addItem("Serif"); |
81 |
facenameCombo.addItem("SansSerif"); |
82 |
facenameCombo.addItem("Monospaced"); |
83 |
facenameCombo.setEditable(true); |
84 |
facenameCombo.addActionListener(listener); |
85 |
|
86 |
JPanel panel = new JPanel(); |
87 |
panel.add(facenameCombo); |
88 |
return panel; |
89 |
} |
90 |
|
91 |
/** |
92 |
Creates the check boxes for selecting bold and italic style |
93 |
@return the panel containing the check boxes |
94 |
*/ |
95 |
public JPanel createCheckBoxes() |
96 |
{ |
97 |
italicCheckBox = new JCheckBox("Italic"); |
98 |
italicCheckBox.addActionListener(listener); |
99 |
|
100 |
boldCheckBox = new JCheckBox("Bold"); |
101 |
boldCheckBox.addActionListener(listener); |
102 |
|
103 |
JPanel panel = new JPanel(); |
104 |
panel.add(italicCheckBox); |
105 |
panel.add(boldCheckBox); |
106 |
panel.setBorder |
107 |
(new TitledBorder(new EtchedBorder(), "Style")); |
108 |
|
109 |
return panel; |
110 |
} |
111 |
|
112 |
/** |
113 |
Creates the radio buttons to select the font size |
114 |
@return the panel containing the radio buttons |
115 |
*/ |
116 |
public JPanel createRadioButtons() |
117 |
{ |
118 |
smallButton = new JRadioButton("Small"); |
119 |
smallButton.addActionListener(listener); |
120 |
|
121 |
mediumButton = new JRadioButton("Medium"); |
122 |
mediumButton.addActionListener(listener); |
123 |
|
124 |
largeButton = new JRadioButton("Large"); |
125 |
largeButton.addActionListener(listener); |
126 |
largeButton.setSelected(true); |
127 |
|
128 |
// add radio buttons to button group |
129 |
|
130 |
ButtonGroup group = new ButtonGroup(); |
131 |
group.add(smallButton); |
132 |
group.add(mediumButton); |
133 |
group.add(largeButton); |
134 |
|
135 |
JPanel panel = new JPanel(); |
136 |
panel.add(smallButton); |
137 |
panel.add(mediumButton); |
138 |
panel.add(largeButton); |
139 |
panel.setBorder |
140 |
(new TitledBorder(new EtchedBorder(), "Size")); |
141 |
|
142 |
return panel; |
143 |
} |
144 |
|
145 |
/** |
146 |
Gets user choice for font name, style, and size |
147 |
and sets the font of the text sample. |
148 |
*/ |
149 |
public void setSampleFont() |
150 |
{ // get font name |
151 |
|
152 |
String facename |
153 |
= (String)facenameCombo.getSelectedItem(); |
154 |
|
155 |
// get font style |
156 |
|
157 |
int style = 0; |
158 |
if (italicCheckBox.isSelected()) |
159 |
style = style + Font.ITALIC; |
160 |
if (boldCheckBox.isSelected()) |
161 |
style = style + Font.BOLD; |
162 |
|
163 |
// get font size |
164 |
|
165 |
int size = 0; |
166 |
|
167 |
final int SMALL_SIZE = 24; |
168 |
final int MEDIUM_SIZE = 36; |
169 |
final int LARGE_SIZE = 48; |
170 |
|
171 |
if (smallButton.isSelected()) |
172 |
size = SMALL_SIZE; |
173 |
else if (mediumButton.isSelected()) |
174 |
size = MEDIUM_SIZE; |
175 |
else if (largeButton.isSelected()) |
176 |
size = LARGE_SIZE; |
177 |
|
178 |
// set font of text field |
179 |
|
180 |
sampleField.setFont(new Font(facename, style, size)); |
181 |
sampleField.repaint(); |
182 |
} |
183 |
|
184 |
private JLabel sampleField; |
185 |
private JCheckBox italicCheckBox; |
186 |
private JCheckBox boldCheckBox; |
187 |
private JRadioButton smallButton; |
188 |
private JRadioButton mediumButton; |
189 |
private JRadioButton largeButton; |
190 |
private JComboBox facenameCombo; |
191 |
private ActionListener listener; |
192 |
} |