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.JMenu;
 13  import javax.swing.JMenuBar;
 14  import javax.swing.JMenuItem;
 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 has a menu with commands to change the font 
 22     of a text sample.
 23  */
 24  public class FontViewer2Frame extends JFrame
 25  {
 26     private static final int FRAME_WIDTH = 300;
 27     private static final int FRAME_HEIGHT = 400;
 28     
 29     private JLabel sampleField;
 30     private String facename;
 31     private int fontstyle;
 32     private int fontsize;
 33  
 34     /**
 35        Constructs the frame.
 36     */
 37     public FontViewer2Frame()
 38     {  
 39        // Construct text sample     
 40        sampleField = new JLabel("Big Java");
 41        add(sampleField, BorderLayout.CENTER);
 42  
 43        // Construct menu      
 44        JMenuBar menuBar = new JMenuBar();     
 45        setJMenuBar(menuBar);
 46        menuBar.add(createFileMenu());
 47        menuBar.add(createFontMenu());
 48  
 49        facename = "Serif";
 50        fontsize = 24;
 51        fontstyle = Font.PLAIN;
 52  
 53        setSampleFont();
 54        setSize(FRAME_WIDTH, FRAME_HEIGHT);
 55     }
 56  
 57     /**
 58        Creates the File menu.
 59        @return the menu
 60     */
 61     public JMenu createFileMenu()
 62     {
 63        JMenu menu = new JMenu("File");
 64        menu.add(createFileExitItem());
 65        return menu;
 66     }
 67  
 68     /**
 69        Creates the File->Exit menu item and sets its action listener.
 70        @return the menu item
 71     */
 72     public JMenuItem createFileExitItem()
 73     {
 74        JMenuItem item = new JMenuItem("Exit");      
 75        class MenuItemListener implements ActionListener
 76        {
 77           public void actionPerformed(ActionEvent event)
 78           {
 79              System.exit(0);
 80           }
 81        }      
 82        ActionListener listener = new MenuItemListener();
 83        item.addActionListener(listener);
 84        return item;
 85     }
 86  
 87     /**
 88        Creates the Font submenu.
 89        @return the menu
 90     */
 91     public JMenu createFontMenu()
 92     {
 93        JMenu menu = new JMenu("Font");
 94        menu.add(createFaceMenu());
 95        menu.add(createSizeMenu());
 96        menu.add(createStyleMenu());
 97        return menu;
 98     }  
 99  
100     /**
101        Creates the Face submenu.
102        @return the menu
103     */
104     public JMenu createFaceMenu()
105     {
106        JMenu menu = new JMenu("Face");
107        menu.add(createFaceItem("Serif"));
108        menu.add(createFaceItem("SansSerif"));
109        menu.add(createFaceItem("Monospaced"));
110        return menu;
111     }  
112  
113     /**
114        Creates the Size submenu.
115        @return the menu
116     */
117     public JMenu createSizeMenu()
118     {
119        JMenu menu = new JMenu("Size");
120        menu.add(createSizeItem("Smaller", -1));
121        menu.add(createSizeItem("Larger", 1));
122        return menu;
123     }  
124  
125     /**
126        Creates the Style submenu.
127        @return the menu
128     */
129     public JMenu createStyleMenu()
130     {
131        JMenu menu = new JMenu("Style");
132        menu.add(createStyleItem("Plain", Font.PLAIN));
133        menu.add(createStyleItem("Bold", Font.BOLD));
134        menu.add(createStyleItem("Italic", Font.ITALIC));
135        menu.add(createStyleItem("Bold Italic", Font.BOLD 
136              + Font.ITALIC));
137        return menu;
138     }  
139  
140     /**
141        Creates a menu item to change the font face and set its action listener.
142        @param name the name of the font face
143        @return the menu item
144     */
145     public JMenuItem createFaceItem(final String name)
146     {
147        JMenuItem item = new JMenuItem(name);      
148        class MenuItemListener implements ActionListener
149        {
150           public void actionPerformed(ActionEvent event)
151           {
152              facename = name;
153              setSampleFont();
154           }
155        }      
156        ActionListener listener = new MenuItemListener();
157        item.addActionListener(listener);
158        return item;
159     }
160  
161     /**
162        Creates a menu item to change the font size
163        and set its action listener.
164        @param name the name of the menu item
165        @param ds the amount by which to change the size
166        @return the menu item
167     */
168     public JMenuItem createSizeItem(String name, final int ds)
169     {
170        JMenuItem item = new JMenuItem(name);      
171        class MenuItemListener implements ActionListener
172        {
173           public void actionPerformed(ActionEvent event)
174           {
175              fontsize = fontsize + ds;
176              setSampleFont();
177           }
178        }      
179        ActionListener listener = new MenuItemListener();
180        item.addActionListener(listener);
181        return item;
182     }
183  
184     /**
185        Creates a menu item to change the font style
186        and set its action listener.
187        @param name the name of the menu item
188        @param style the new font style
189        @return the menu item
190     */
191     public JMenuItem createStyleItem(String name, final int style)
192     {
193        JMenuItem item = new JMenuItem(name);      
194        class MenuItemListener implements ActionListener
195        {
196           public void actionPerformed(ActionEvent event)
197           {
198              fontstyle = style;
199              setSampleFont();
200           }
201        }      
202        ActionListener listener = new MenuItemListener();
203        item.addActionListener(listener);
204        return item;
205     }
206  
207     /**
208        Sets the font of the text sample.
209     */
210     public void setSampleFont()
211     { 
212        Font f = new Font(facename, fontstyle, fontsize);
213        sampleField.setFont(f);
214        sampleField.repaint();
215     }
216  }
217  
218