001: import java.awt.BorderLayout;
002: import java.awt.Font;
003: import java.awt.GridLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import javax.swing.ButtonGroup;
007: import javax.swing.JButton;
008: import javax.swing.JCheckBox;
009: import javax.swing.JComboBox;
010: import javax.swing.JFrame;
011: import javax.swing.JLabel;
012: import javax.swing.JPanel;
013: import javax.swing.JRadioButton;
014: import javax.swing.border.EtchedBorder;
015: import javax.swing.border.TitledBorder;
016: 
017: /**
018:    This frame contains a text field and a control panel
019:    to change the font of the text.
020: */
021: public class ChoiceFrame extends JFrame
022: {
023:    /**
024:       Constructs the frame.
025:    */
026:    public ChoiceFrame()
027:    {  
028:       // Construct text sample
029:       sampleField = new JLabel("Big Java");
030:       add(sampleField, BorderLayout.CENTER);
031: 
032:       // This listener is shared among all components
033:       class ChoiceListener implements ActionListener
034:       {  
035:          public void actionPerformed(ActionEvent event)
036:          {  
037:             setSampleFont();
038:          }
039:       }
040:    
041:       listener = new ChoiceListener();
042: 
043:       createControlPanel();
044:       setSampleFont();
045:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
046:    }
047: 
048:    /**
049:       Creates the control panel to change the font.
050:    */
051:    public void createControlPanel()
052:    {
053:       JPanel facenamePanel = createComboBox();
054:       JPanel sizeGroupPanel = createCheckBoxes();
055:       JPanel styleGroupPanel = createRadioButtons();
056: 
057:       // Line up component panels
058: 
059:       JPanel controlPanel = new JPanel();
060:       controlPanel.setLayout(new GridLayout(3, 1));
061:       controlPanel.add(facenamePanel);
062:       controlPanel.add(sizeGroupPanel);
063:       controlPanel.add(styleGroupPanel);
064: 
065:       // Add panels to content pane
066: 
067:       add(controlPanel, BorderLayout.SOUTH);
068:    }
069: 
070:    /**
071:       Creates the combo box with the font style choices.
072:       @return the panel containing the combo box
073:    */
074:    public JPanel createComboBox()
075:    {
076:       facenameCombo = new JComboBox();
077:       facenameCombo.addItem("Serif");
078:       facenameCombo.addItem("SansSerif");
079:       facenameCombo.addItem("Monospaced");
080:       facenameCombo.setEditable(true);
081:       facenameCombo.addActionListener(listener);
082: 
083:       JPanel panel = new JPanel();
084:       panel.add(facenameCombo);
085:       return panel;
086:    }
087: 
088:    /**
089:       Creates the check boxes for selecting bold and italic styles.
090:       @return the panel containing the check boxes
091:    */
092:    public JPanel createCheckBoxes()
093:    {
094:       italicCheckBox = new JCheckBox("Italic");
095:       italicCheckBox.addActionListener(listener);
096: 
097:       boldCheckBox = new JCheckBox("Bold");
098:       boldCheckBox.addActionListener(listener);
099: 
100:       JPanel panel = new JPanel();
101:       panel.add(italicCheckBox);
102:       panel.add(boldCheckBox);
103:       panel.setBorder
104:          (new TitledBorder(new EtchedBorder(), "Style"));
105: 
106:       return panel;
107:    }
108: 
109:    /**
110:       Creates the radio buttons to select the font size
111:       @return the panel containing the radio buttons
112:    */
113:    public JPanel createRadioButtons()
114:    {
115:       smallButton = new JRadioButton("Small");
116:       smallButton.addActionListener(listener);
117: 
118:       mediumButton = new JRadioButton("Medium");
119:       mediumButton.addActionListener(listener);
120: 
121:       largeButton = new JRadioButton("Large");
122:       largeButton.addActionListener(listener);
123:       largeButton.setSelected(true);
124: 
125:       // Add radio buttons to button group
126: 
127:       ButtonGroup group = new ButtonGroup();
128:       group.add(smallButton);
129:       group.add(mediumButton);
130:       group.add(largeButton);
131: 
132:       JPanel panel = new JPanel();
133:       panel.add(smallButton);
134:       panel.add(mediumButton);
135:       panel.add(largeButton);
136:       panel.setBorder
137:             (new TitledBorder(new EtchedBorder(), "Size"));
138: 
139:       return panel;
140:    }
141: 
142:    /**
143:       Gets user choice for font name, style, and size
144:       and sets the font of the text sample.
145:    */
146:    public void setSampleFont()
147:    {  
148:       // Get font name   
149:       String facename 
150:             = (String) facenameCombo.getSelectedItem();
151:       
152:       // Get font style
153:       
154:       int style = 0;
155:       if (italicCheckBox.isSelected()) 
156:          style = style + Font.ITALIC;
157:       if (boldCheckBox.isSelected()) 
158:          style = style + Font.BOLD;
159:          
160:       // Get font size   
161: 
162:       int size = 0;
163:       
164:       final int SMALL_SIZE = 24;
165:       final int MEDIUM_SIZE = 36;
166:       final int LARGE_SIZE = 48;
167: 
168:       if (smallButton.isSelected()) 
169:          size = SMALL_SIZE;
170:       else if (mediumButton.isSelected()) 
171:          size = MEDIUM_SIZE;
172:       else if (largeButton.isSelected()) 
173:          size = LARGE_SIZE;
174:          
175:       // Set font of text field
176:       
177:       sampleField.setFont(new Font(facename, style, size));      
178:       sampleField.repaint();
179:    }
180:    
181:    private JLabel sampleField;
182:    private JCheckBox italicCheckBox;
183:    private JCheckBox boldCheckBox;
184:    private JRadioButton smallButton;
185:    private JRadioButton mediumButton;
186:    private JRadioButton largeButton;
187:    private JComboBox facenameCombo;
188:    private ActionListener listener;
189: 
190:    private static final int FRAME_WIDTH = 300;
191:    private static final int FRAME_HEIGHT = 400;
192: }