cs12420 - lecture 02 menus
DESCRIPTION
CS12420 - Lecture 02 Menus. Lynda Thomas [email protected]. 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. - PowerPoint PPT PresentationTRANSCRIPT
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
• 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...
JFrame
SimpleFrame
SecondMenuFrame
1: new SecondMenuFrame()
JPanel
ColorPanel
2: new JMenu*
3: new ColorPanel
MenuListener4: new MenuListener(this)
smFrame
5: e.g. green.addActionListener(menuList)
6: select green:actionPerformed called
7: setPanelColor(Color.green)
Filling in some blanks..
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);}
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’
More components
Radio buttons and Combo boxes
ComboBoxDemo.java
and
RadioButtonDemo.java
In this lecture
• We looked at menus
In the next lecture we look at drawing