      1  import java.awt.BorderLayout;

      2  import java.awt.Font;

      3  import java.awt.GridLayout;

      4  import java.awt.event.ActionEvent;

      5  import java.awt.event.ActionListener;

      6  import javax.swing.ButtonGroup;

      7  import javax.swing.JButton;

      8  import javax.swing.JCheckBox;

      9  import javax.swing.JComboBox;

     10  import javax.swing.JFrame;

     11  import javax.swing.JLabel;

     12  import javax.swing.JPanel;

     13  import javax.swing.JRadioButton;

     14  import javax.swing.border.EtchedBorder;

     15  import javax.swing.border.TitledBorder;

     16

     17  /**

     18     This frame contains a text field and a control panel

     19     to change the font of the text.

     20  */

     21  public class FontViewerFrame extends JFrame

     22  {

     23     private static final int FRAME_WIDTH = 300;

     24     private static final int FRAME_HEIGHT = 400;

     25

     26     private JLabel sampleField;

     27     private JCheckBox italicCheckBox;

     28     private JCheckBox boldCheckBox;

     29     private JRadioButton smallButton;

     30     private JRadioButton mediumButton;

     31     private JRadioButton largeButton;

     32     private JComboBox facenameCombo;

     33     private ActionListener listener;

     34

     35     /**

     36        Constructs the frame.

     37     */

     38     public FontViewerFrame()

     39     {

     40        // Construct text sample

     41        sampleField = new JLabel("Big Java");

     42        add(sampleField, BorderLayout.CENTER);

     43

     44        // This listener is shared among all components

     45        class ChoiceListener implements ActionListener

     46        {

     47           public void actionPerformed(ActionEvent event)

     48           {

     49              setSampleFont();

     50           }

     51        }

     52

     53        listener = new ChoiceListener();

     54

     55        createControlPanel();

     56        setSampleFont();

     57        setSize(FRAME_WIDTH, FRAME_HEIGHT);

     58     }

     59

     60     /**

     61        Creates the control panel to change the font.

     62     */

     63     public void createControlPanel()

     64     {

     65        JPanel facenamePanel = createComboBox();

     66        JPanel sizeGroupPanel = createCheckBoxes();

     67        JPanel styleGroupPanel = createRadioButtons();

     68

     69        // Line up component panels

     70

     71        JPanel controlPanel = new JPanel();

     72        controlPanel.setLayout(new GridLayout(3, 1));

     73        controlPanel.add(facenamePanel);

     74        controlPanel.add(sizeGroupPanel);

     75        controlPanel.add(styleGroupPanel);

     76

     77        // Add panels to content pane

     78

     79        add(controlPanel, BorderLayout.SOUTH);

     80     }

     81

     82     /**

     83        Creates the combo box with the font style choices.

     84        @return the panel containing the combo box

     85     */

     86     public JPanel createComboBox()

     87     {

     88        facenameCombo = new JComboBox();

     89        facenameCombo.addItem("Serif");

     90        facenameCombo.addItem("SansSerif");

     91        facenameCombo.addItem("Monospaced");

     92        facenameCombo.setEditable(true);

     93        facenameCombo.addActionListener(listener);

     94

     95        JPanel panel = new JPanel();

     96        panel.add(facenameCombo);

     97        return panel;

     98     }

     99

    100     /**

    101        Creates the check boxes for selecting bold and italic styles.

    102        @return the panel containing the check boxes

    103     */

    104     public JPanel createCheckBoxes()

    105     {

    106        italicCheckBox = new JCheckBox("Italic");

    107        italicCheckBox.addActionListener(listener);

    108

    109        boldCheckBox = new JCheckBox("Bold");

    110        boldCheckBox.addActionListener(listener);

    111

    112        JPanel panel = new JPanel();

    113        panel.add(italicCheckBox);

    114        panel.add(boldCheckBox);

    115        panel.setBorder(new TitledBorder(new EtchedBorder(), "Style"));

    116

    117        return panel;

    118     }

    119

    120     /**

    121        Creates the radio buttons to select the font size

    122        @return the panel containing the radio buttons

    123     */

    124     public JPanel createRadioButtons()

    125     {

    126        smallButton = new JRadioButton("Small");

    127        smallButton.addActionListener(listener);

    128

    129        mediumButton = new JRadioButton("Medium");

    130        mediumButton.addActionListener(listener);

    131

    132        largeButton = new JRadioButton("Large");

    133        largeButton.addActionListener(listener);

    134        largeButton.setSelected(true);

    135

    136        // Add radio buttons to button group

    137

    138        ButtonGroup group = new ButtonGroup();

    139        group.add(smallButton);

    140        group.add(mediumButton);

    141        group.add(largeButton);

    142

    143        JPanel panel = new JPanel();

    144        panel.add(smallButton);

    145        panel.add(mediumButton);

    146        panel.add(largeButton);

    147        panel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));

    148

    149        return panel;

    150     }

    151

    152     /**

    153        Gets user choice for font name, style, and size

    154        and sets the font of the text sample.

    155     */

    156     public void setSampleFont()

    157     {

    158        // Get font name

    159        String facename

    160              = (String) facenameCombo.getSelectedItem();

    161

    162        // Get font style

    163

    164        int style = 0;

    165        if (italicCheckBox.isSelected())

    166        {

    167           style = style + Font.ITALIC;

    168        }

    169        if (boldCheckBox.isSelected())

    170        {

    171           style = style + Font.BOLD;

    172        }

    173

    174        // Get font size

    175

    176        int size = 0;

    177

    178        final int SMALL_SIZE = 24;

    179        final int MEDIUM_SIZE = 36;

    180        final int LARGE_SIZE = 48;

    181

    182        if (smallButton.isSelected()) { size = SMALL_SIZE; }

    183        else if (mediumButton.isSelected()) { size = MEDIUM_SIZE; }

    184        else if (largeButton.isSelected()) { size = LARGE_SIZE; }

    185

    186        // Set font of text field

    187

    188        sampleField.setFont(new Font(facename, style, size));

    189        sampleField.repaint();

    190     }

    191   }