95 example of a frame with menus initial frame menu

This preview shows page 95 - 103 out of 164 pages.

95Example of a Frame with MenusInitial FrameMenu with FileSelected.Here we have checkbox and radio button menu itemsMenu withsubmenu Help Topicsselected
96Another Event Handling ExampleLet us now look at another event handling example.In this example we will use event handlers with menus.The event handler will display text in a JTextArea depending on what was selected on the menuWe will also illustrate the implementation of a listener object as a separate class using an adapter class.
97Event Handling with Menusimport java.awt.event.*;import javax.swing.*;class MenuDemo{//declare and instantiate all objectsJPanel p = new JPanel(); JTextArea t = new JTextArea("This is a demo program " + "to illustrate event handling with menus", 5, 30);JFrame f = new JFrame("Example of menu uses");JMenuBar mb = new JMenuBar();JMenu m1 = new JMenu("Help"), m2 = new JMenu("Help Topics");JMenuItem exit = new JMenuItem("Exit");
98Event Handling with Menus (continued)public void init(){// set position and size of framef.setBounds(300,200,500,300);//define and instantiate an array of menu itemsJMenuItem[] helps = {new JMenuItem("Component"), new JMenuItem("Container"),new JMenuItem("Window"), new JMenuItem("Frame")};// add the menu items to second menufor (int i = 0; i < 4; i++)m2.add(helps[i]);//add second menu to first menum1.add(m2);//add separator line to first menum1.addSeparator();// add exit menu item to first menum1.add(exit);
99Event Handling with Menus (continued)//add first menu to the menu barmb.add(m1);// add the menu bar to the framef.setJMenuBar(mb);// make it possible for lines to wrap in text areat.setLineWrap(true);//make wrapping in text area occur at wordst.setWrapStyleWord(true);//do not allow typing in text areat.setEditable(false);// add text area to panelp.add(t);//add panel to frame and make frame visiblef.getContentPane().add(p);f.setVisible(true);
100Event Handling with Menus (continued)//add window listeners to frame f.addWindowListener(new CloseWindowAndExit());//add action listeners to menu itemshelps[0].addActionListener(new ProvideHelp());helps[1].addActionListener(new ProvideHelp());helps[2].addActionListener(new ProvideHelp());helps[3].addActionListener(new ProvideHelp());//disable exit menu item and register a listener with itexit.setEnabled(false);exit.addActionListener(new ProvideHelp());//associate keyboard shortcuts with some menu itemshelps[0].setMnemonic(KeyEvent.VK_P);helps[1].setMnemonic(KeyEvent.VK_C);helps[2].setMnemonic(KeyEvent.VK_W); helps[3].setMnemonic(KeyEvent.VK_F);}
101Event Handling with Menus (continued)public static void main(String[] args){//instantiate frame and invoke its init() methodMenuDemo md = new MenuDemo();md.init();}// implementations of WindowListener methodspublic class CloseWindowAndExit extends WindowAdapter{public void windowClosing(WindowEvent we){System.exit(0);}}
102Event Handling with Menus (continued)// implementation of ActionListener methodclass ProvideHelp implements ActionListener{public void actionPerformed(ActionEvent e){//getSource() returns an Object hence must be cast JMenuItem selected = (JMenuItem)e.getSource();String actionCommand = selected.getActionCommand();if (actionCommand.equals

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture