    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.JMenu;
    013: import javax.swing.JMenuBar;
    014: import javax.swing.JMenuItem;
    015: import javax.swing.JPanel;
    016: import javax.swing.JRadioButton;
    017: import javax.swing.border.EtchedBorder;
    018: import javax.swing.border.TitledBorder;
    019:
    020: /**
    021:    This frame has a menu with commands to change the font
    022:    of a text sample.
    023: */
    024: public class MenuFrame extends JFrame
    025: {
    026:    /**
    027:       Constructs the frame.
    028:    */
    029:    public MenuFrame()
    030:    {
    031:       // Construct text sample
    032:       sampleField = new JLabel("Big Java");
    033:       add(sampleField, BorderLayout.CENTER);
    034:
    035:       // Construct menu
    036:       JMenuBar menuBar = new JMenuBar();
    037:       setJMenuBar(menuBar);
    038:       menuBar.add(createFileMenu());
    039:       menuBar.add(createFontMenu());
    040:
    041:       facename = "Serif";
    042:       fontsize = 24;
    043:       fontstyle = Font.PLAIN;
    044:
    045:       setSampleFont();
    046:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
    047:    }
    048:
    049:    /**
    050:       Creates the File menu.
    051:       @return the menu
    052:    */
    053:    public JMenu createFileMenu()
    054:    {
    055:       JMenu menu = new JMenu("File");
    056:       menu.add(createFileExitItem());
    057:       return menu;
    058:    }
    059:
    060:    /**
    061:       Creates the File->Exit menu item and sets its action listener.
    062:       @return the menu item
    063:    */
    064:    public JMenuItem createFileExitItem()
    065:    {
    066:       JMenuItem item = new JMenuItem("Exit");
    067:       class MenuItemListener implements ActionListener
    068:       {
    069:          public void actionPerformed(ActionEvent event)
    070:          {
    071:             System.exit(0);
    072:          }
    073:       }
    074:       ActionListener listener = new MenuItemListener();
    075:       item.addActionListener(listener);
    076:       return item;
    077:    }
    078:
    079:    /**
    080:       Creates the Font submenu.
    081:       @return the menu
    082:    */
    083:    public JMenu createFontMenu()
    084:    {
    085:       JMenu menu = new JMenu("Font");
    086:       menu.add(createFaceMenu());
    087:       menu.add(createSizeMenu());
    088:       menu.add(createStyleMenu());
    089:       return menu;
    090:    }
    091:
    092:    /**
    093:       Creates the Face submenu.
    094:       @return the menu
    095:    */
    096:    public JMenu createFaceMenu()
    097:    {
    098:       JMenu menu = new JMenu("Face");
    099:       menu.add(createFaceItem("Serif"));
    100:       menu.add(createFaceItem("SansSerif"));
    101:       menu.add(createFaceItem("Monospaced"));
    102:       return menu;
    103:    }
    104:
    105:    /**
    106:       Creates the Size submenu.
    107:       @return the menu
    108:    */
    109:    public JMenu createSizeMenu()
    110:    {
    111:       JMenu menu = new JMenu("Size");
    112:       menu.add(createSizeItem("Smaller", -1));
    113:       menu.add(createSizeItem("Larger", 1));
    114:       return menu;
    115:    }
    116:
    117:    /**
    118:       Creates the Style submenu.
    119:       @return the menu
    120:    */
    121:    public JMenu createStyleMenu()
    122:    {
    123:       JMenu menu = new JMenu("Style");
    124:       menu.add(createStyleItem("Plain", Font.PLAIN));
    125:       menu.add(createStyleItem("Bold", Font.BOLD));
    126:       menu.add(createStyleItem("Italic", Font.ITALIC));
    127:       menu.add(createStyleItem("Bold Italic", Font.BOLD
    128:             + Font.ITALIC));
    129:       return menu;
    130:    }
    131:
    132:
    133:    /**
    134:       Creates a menu item to change the font face and set its action listener.
    135:       @param name the name of the font face
    136:       @return the menu item
    137:    */
    138:    public JMenuItem createFaceItem(final String name)
    139:    {
    140:       JMenuItem item = new JMenuItem(name);
    141:       class MenuItemListener implements ActionListener
    142:       {
    143:          public void actionPerformed(ActionEvent event)
    144:          {
    145:             facename = name;
    146:             setSampleFont();
    147:          }
    148:       }
    149:       ActionListener listener = new MenuItemListener();
    150:       item.addActionListener(listener);
    151:       return item;
    152:    }
    153:
    154:    /**
    155:       Creates a menu item to change the font size
    156:       and set its action listener.
    157:       @param name the name of the menu item
    158:       @param ds the amount by which to change the size
    159:       @return the menu item
    160:    */
    161:    public JMenuItem createSizeItem(String name, final int ds)
    162:    {
    163:       JMenuItem item = new JMenuItem(name);
    164:       class MenuItemListener implements ActionListener
    165:       {
    166:          public void actionPerformed(ActionEvent event)
    167:          {
    168:             fontsize = fontsize + ds;
    169:             setSampleFont();
    170:          }
    171:       }
    172:       ActionListener listener = new MenuItemListener();
    173:       item.addActionListener(listener);
    174:       return item;
    175:    }
    176:
    177:    /**
    178:       Creates a menu item to change the font style
    179:       and set its action listener.
    180:       @param name the name of the menu item
    181:       @param style the new font style
    182:       @return the menu item
    183:    */
    184:    public JMenuItem createStyleItem(String name, final int style)
    185:    {
    186:       JMenuItem item = new JMenuItem(name);
    187:       class MenuItemListener implements ActionListener
    188:       {
    189:          public void actionPerformed(ActionEvent event)
    190:          {
    191:             fontstyle = style;
    192:             setSampleFont();
    193:          }
    194:       }
    195:       ActionListener listener = new MenuItemListener();
    196:       item.addActionListener(listener);
    197:       return item;
    198:    }
    199:
    200:    /**
    201:       Sets the font of the text sample.
    202:    */
    203:    public void setSampleFont()
    204:    {
    205:       Font f = new Font(facename, fontstyle, fontsize);
    206:       sampleField.setFont(f);
    207:       sampleField.repaint();
    208:    }
    209:
    210:    private JLabel sampleField;
    211:    private String facename;
    212:    private int fontstyle;
    213:    private int fontsize;
    214:
    215:    private static final int FRAME_WIDTH = 300;
    216:    private static final int FRAME_HEIGHT = 400;
    217: }
    218:
    219: