package examples; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * CS401 Example 42 * * Demonstrates the construction of a menu heirarchy. We create an instance of * JMenubar, which represents the menu bar of our window. To this, an instance * of the JMenu class is added for each menu displayed in the menu bar. Each * JMenu also needs to be constructed to contain a set of JMenuItems to display. * This program creates a window with JLabel displayed in its center. A set of * menus is provided to allow the user to change the color, font, and style of * the JLabel as well as exit the program. The menu heiracrchy is created in a * bottom-up fashion. * * @author PJ Dillon */ public class ex42 implements ActionListener, ItemListener { private JMenuItem exit; private JRadioButtonMenuItem[] fonts; private JRadioButtonMenuItem[] colors; private JCheckBoxMenuItem bold; private JCheckBoxMenuItem italic; private JLabel label; private static Color[] colorValues = {Color.black, Color.white, Color.red, Color.blue, Color.green}; public ex42() { JFrame window = new JFrame("Example 42"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* * A specialized type of JMenuItem is the JRadioButtonMenuItem, a group of * which act just like JRadioButtons but can be added to a menu. */ ButtonGroup colorGroup = new ButtonGroup(); colors = new JRadioButtonMenuItem[colorValues.length]; colors[0] = new JRadioButtonMenuItem("Black"); colors[0].setSelected(true); colors[0].addItemListener(this); colorGroup.add(colors[0]); colors[1] = new JRadioButtonMenuItem("White"); colors[1].addItemListener(this); colorGroup.add(colors[1]); colors[2] = new JRadioButtonMenuItem("Red"); colors[2].addItemListener(this); colorGroup.add(colors[2]); colors[3] = new JRadioButtonMenuItem("Blue"); colors[3].addItemListener(this); colorGroup.add(colors[3]); colors[4] = new JRadioButtonMenuItem("Green"); colors[4].addItemListener(this); colorGroup.add(colors[4]); /* * Create another group for the text font */ ButtonGroup fontGroup = new ButtonGroup(); fonts = new JRadioButtonMenuItem[4]; fonts[0] = new JRadioButtonMenuItem("Sans"); fonts[0].setSelected(true); fonts[0].addItemListener(this); fontGroup.add(fonts[0]); fonts[1] = new JRadioButtonMenuItem("Serif"); fonts[1].addItemListener(this); fontGroup.add(fonts[1]); fonts[2] = new JRadioButtonMenuItem("Monospaced"); fonts[2].addItemListener(this); fontGroup.add(fonts[2]); fonts[3] = new JRadioButtonMenuItem("Comic Sans MS"); fonts[3].addItemListener(this); fontGroup.add(fonts[3]); /* * As we've seen with previous examples, different styles can be applied * simultaneously, which makes JCheckBoxMenuItems a good choice to use here. */ bold = new JCheckBoxMenuItem("Bold"); bold.addItemListener(this); italic = new JCheckBoxMenuItem("Italics"); italic.addItemListener(this); //create a MenuItem that lets the user exit the program exit = new JMenuItem("Exit"); exit.addActionListener(this); /* * With all of the menu items created, we start grouping them into JMenus. * A 'File' menu will contain the exit JMenuItem that exits the program. */ JMenu fileMenu = new JMenu("File"); fileMenu.addSeparator(); fileMenu.add(exit); /* * Create a font menu to display all the Font options */ JMenu fontMenu = new JMenu("Font"); for(AbstractButton a : fonts) fontMenu.add(a); fontMenu.addSeparator(); fontMenu.add(bold); fontMenu.add(italic); /* * Create a color menu to display all the Color options */ JMenu colorMenu = new JMenu("Color"); for(AbstractButton b : colors) colorMenu.add(b); /* * The Font and Color menus are contained as submenus in a Format menu * created here. */ JMenu formatMenu = new JMenu("Format"); formatMenu.add(colorMenu); formatMenu.add(fontMenu); /* * The two upper level menus are then added to a menu bar */ JMenuBar menubar = new JMenuBar(); menubar.add(fileMenu); menubar.add(formatMenu); //add the menu bar to the window. window.setJMenuBar(menubar); label = new JLabel("Text To Change", JLabel.CENTER); label.setFont(new Font("Sans", Font.PLAIN, 50)); window.getContentPane().add(label, BorderLayout.CENTER); window.setSize(500, 200); window.setVisible(true); } public void actionPerformed(ActionEvent e) { System.exit(0); } public void itemStateChanged(ItemEvent e) { String fontName = null; /* * With each event that occurs, we don't bother checking the source of the * event. Instead, we simply find the selected font and color from their * respective groups, compute the style of the text, and set the new Font * object for the JLabel. */ for(int i = 0; i < colors.length; i++) if(colors[i].isSelected()) label.setForeground(colorValues[i]); for(AbstractButton a : fonts) if(a.isSelected()) fontName = a.getText(); int style = Font.PLAIN; if(bold.isSelected()) style += Font.BOLD; if(italic.isSelected()) style += Font.ITALIC; label.setFont(new Font(fontName, style, 50)); } public static void main(String[] args) { new ex42(); } }