// MenuDemoApplication.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-10-03 File created. // 2006-10-04 Last modification. // See the notes at the end of this file. // This program demonstrates how menus can be built for a Java // application. // The menus are built into a MenuDemoFrame object. // Menus cannot be built into a JPanel-based component. import java.awt.* ; import java.awt.event.*; import javax.swing.* ; class MenuDemoFrame extends JFrame implements ActionListener { static final int WINDOW_WIDTH = 500 ; static final int WINDOW_HEIGHT = 400 ; 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 MenuDemoFrame() { getContentPane().setBackground( Color.white ) ; setSize( WINDOW_WIDTH, WINDOW_HEIGHT ) ; setTitle( "GUI APPLICATION WITH MENUS" ) ; 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 ) ; setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ; } // 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 selection brings a message dialog to the screen. // File -> Exit selection terminates the application. 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 ) { setVisible( false ) ; System.exit( 0 ) ; } else if ( selected_menu_item == about_menu_item ) { JOptionPane.showMessageDialog( this, "This is a simple GUI application" + "\nwith 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 ) ; } } public class MenuDemoApplication { public static void main( String[] not_in_use ) { MenuDemoFrame this_application_window = new MenuDemoFrame() ; this_application_window.setVisible( true ) ; } } /******* The statement at the end of the MenuDemoFrame constructor setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ; makes the program terminate when the window is closed. Another possibility to program this feature would be to make the MenuDemoFrame class implement the WindowListener interface. In addition the constructor should contain the statement addWindowListener( this ) ; and the class should have the methods // A method belonging to the WindowListener interface public void windowClosing( WindowEvent event ) { setVisible( false ) ; System.exit( 0 ) ; } // Other methods required by the WindowListener interface: public void windowActivated( WindowEvent event ) {} public void windowClosed( WindowEvent event ) {} public void windowDeactivated( WindowEvent event ) {} public void windowDeiconified( WindowEvent event ) {} public void windowIconified( WindowEvent event ) { } public void windowOpened( WindowEvent event ) {} *****/