1 / 12

CS12420 - Lecture 02 Menus

CS12420 - Lecture 02 Menus. Lynda Thomas ltt@aber.ac.uk. Menus are like Buttons. Use a Listener Classes involved are: JMenuBar JMenu JMenuItem Warning: you may use words slightly differently!. MenuBar. Menu. MenuItem. Look in menus directory for FirstMenuFrame.java. Alternatively.

amora
Download Presentation

CS12420 - Lecture 02 Menus

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS12420 - Lecture 02Menus Lynda Thomas ltt@aber.ac.uk

  2. Menus are like Buttons • Use a Listener • Classes involved are: • JMenuBar • JMenu • JMenuItem • Warning: you may use words slightly differently!

  3. MenuBar Menu MenuItem

  4. Look in menus directory for FirstMenuFrame.java

  5. Alternatively • You could use a separate MenuListener class • See SecondMenuFrame.java and MenuListener.java • Let’s step through code with debugger and see animation of what is happening...

  6. 1: new SecondMenuFrame() JFrame 4: new MenuListener(this) MenuListener SimpleFrame smFrame SecondMenuFrame 7: setPanelColor(Color.green) 3: new ColorPanel JMenu Colours JMenu Options JMenuItem Green JMenuBar JMenuItem JMenuItem JMenuItem JMenuItem 2: new JMenu* JPanel ColorPanel 6: select green: actionPerformed called 5: e.g. green.addActionListener(menuList)

  7. Filling in some blanks..

  8. Layout Managers • BorderLayout is default for JFrame • FlowLayout is default for JPanel • GridLayout: public TextPanel() { setLayout(new GridLayout(2,2,5,5)); //2 rows, 2 cols, spaces add (new JLabel("Amount: ")); inputField=new JTextField(8); add (inputField); add (new JLabel("Balance: ")); balanceField=new JTextField(8); add (balanceField); }

  9. Possible mistakes: • Misspelling javax.swing in import • Forgetting setVisible(true) (done for you in SimpleFrame) • Forgetting the JFrame.EXIT_ON_CLOSE • If buttons etc. don’t work: • Have you got something Listening to the button? • Is there an appropriate action in actionPerformed? • Try just printing ‘they hit button X’

  10. More components Radio buttons and Combo boxes ComboBoxDemo.java and RadioButtonDemo.java

  11. In this lecture • We looked at menus In the next lecture we look at drawing

More Related