package examples; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ex42c implements ItemListener, ActionListener { private JMenuItem exit; private JMenuItem open; private JMenuItem colorItem; private JRadioButtonMenuItem[] fonts; private JCheckBoxMenuItem bold; private JCheckBoxMenuItem italic; private JLabel label; private static Color[] colorValues = {Color.black, Color.white, Color.red, Color.blue, Color.green}; public ex42c() { JFrame window = new JFrame("Example 42c"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); colorItem = new JMenuItem("Color..."); colorItem.addActionListener(this); open = new JMenuItem("Open..."); open.addActionListener(this); /* * 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.add(open); 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); /* * The Font and Color menus are contained as submenus in a Format menu * created here. */ JMenu formatMenu = new JMenu("Format"); formatMenu.add(colorItem); 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) { Object o = e.getSource(); if(o == open) { JFileChooser fc = new JFileChooser(); int ret = fc.showOpenDialog(null); if(ret == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); label.setText(file.getName()); } } else if(o == colorItem) { Color c = JColorChooser.showDialog(null, "Pick Color", label.getForeground()); label.setForeground(c); } else 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(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 ex42c(); } }