// MenuDemoApplet.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-10-03 File created. // 2006-10-04 Last modification. // This is an applet that provides menus for its user. import java.awt.* ; import java.awt.event.*; import javax.swing.* ; public class MenuDemoApplet extends JApplet implements ActionListener { JMenuItem file_open_menu_item = new JMenuItem( "Open" ) ; JMenuItem file_save_menu_item = new JMenuItem( "Save" ) ; JMenuItem file_exit_menu_item = new JMenuItem( "Exit" ) ; JMenuItem edit_copy_menu_item = new JMenuItem( "Copy" ) ; JMenuItem edit_paste_menu_item = new JMenuItem( "Paste" ) ; JCheckBoxMenuItem cyan_background_menu_item = new JCheckBoxMenuItem( "Cyan background" ) ; JRadioButtonMenuItem small_text_menu_item = new JRadioButtonMenuItem( "Small" ) ; JRadioButtonMenuItem medium_text_menu_item = new JRadioButtonMenuItem( "Medium" ) ; JRadioButtonMenuItem large_text_menu_item = new JRadioButtonMenuItem( "Large" ) ; JMenuItem about_menu_item = new JMenuItem( "About" ) ; String last_selected_menu_item = "NO SELECTIONS MADE." ; public void init() { getContentPane().setBackground( Color.white ) ; file_open_menu_item.setMnemonic( 'O' ) ; file_open_menu_item.addActionListener( this ) ; file_save_menu_item.setMnemonic( 'S' ) ; file_save_menu_item.addActionListener( this ) ; file_exit_menu_item.setMnemonic( 'x' ) ; file_exit_menu_item.addActionListener( this ) ; JMenu file_menu = new JMenu( "File" ) ; file_menu.setMnemonic( 'F' ) ; file_menu.add( file_open_menu_item ) ; file_menu.add( file_save_menu_item ) ; file_menu.add( file_exit_menu_item ) ; edit_copy_menu_item.addActionListener( this ) ; edit_paste_menu_item.addActionListener( this ) ; JMenu edit_menu = new JMenu( "Edit" ) ; edit_menu.add( edit_copy_menu_item ) ; edit_menu.add( edit_paste_menu_item ) ; JMenu settings_menu = new JMenu( "Settings" ) ; cyan_background_menu_item.addActionListener( this ) ; settings_menu.add( cyan_background_menu_item ) ; small_text_menu_item.addActionListener( this ) ; medium_text_menu_item.addActionListener( this ) ; large_text_menu_item.addActionListener( this ) ; medium_text_menu_item.setSelected( true ) ; ButtonGroup text_size_button_group = new ButtonGroup() ; text_size_button_group.add( small_text_menu_item ) ; text_size_button_group.add( medium_text_menu_item ) ; text_size_button_group.add( large_text_menu_item ) ; JMenu text_size_menu = new JMenu( "Text size" ) ; text_size_menu.add( small_text_menu_item ) ; text_size_menu.add( medium_text_menu_item ) ; text_size_menu.add( large_text_menu_item ) ; settings_menu.add( text_size_menu ) ; JMenu help_menu = new JMenu( "Help" ) ; about_menu_item.addActionListener( this ) ; help_menu.add( about_menu_item ) ; JMenuBar menubar_of_this_window = new JMenuBar() ; menubar_of_this_window.add( file_menu ) ; menubar_of_this_window.add( edit_menu ) ; menubar_of_this_window.add( settings_menu ) ; menubar_of_this_window.add( help_menu ) ; setJMenuBar( menubar_of_this_window ) ; } // The following method reacts to the menu selections. // In most cases the only reaction is that the text that // appears on the screen is changed. // Background color is set to cyan if that selection is made. // Help -> About and File -> Exit selections bring a message // box to the screen. public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JMenuItem ) { JMenuItem selected_menu_item = (JMenuItem) event.getSource() ; last_selected_menu_item = selected_menu_item.getText() ; if ( cyan_background_menu_item.isSelected() == true ) { getContentPane().setBackground( Color.cyan ) ; } else { getContentPane().setBackground( Color.white ) ; } if ( selected_menu_item == file_exit_menu_item ) { JOptionPane.showMessageDialog( this, "This is a Java applet." + "\nFile -> Exit does not work in applets.", "About", JOptionPane.PLAIN_MESSAGE ) ; } else if ( selected_menu_item == about_menu_item ) { JOptionPane.showMessageDialog( this, "This is a simpe GUI applet" + "\nthat has menus.", "About", JOptionPane.PLAIN_MESSAGE ) ; } repaint() ; } } public void paint( Graphics graphics ) { super.paint( graphics ) ; if ( small_text_menu_item.isSelected() == true ) { graphics.setFont( new Font( "Serif", Font.PLAIN, 10 ) ) ; } if ( medium_text_menu_item.isSelected() == true ) { graphics.setFont( new Font( "Serif", Font.PLAIN, 14 ) ) ; } if ( large_text_menu_item.isSelected() == true ) { graphics.setFont( new Font( "Serif", Font.PLAIN, 18 ) ) ; } graphics.drawString( "Last selected menu item: \"" + last_selected_menu_item + "\"", 100, 200 ) ; } }