cs12420 - lecture 02 menus

12
CS12420 - Lecture 02 Menus Lynda Thomas [email protected]

Upload: amora

Post on 01-Feb-2016

36 views

Category:

Documents


1 download

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 Presentation

TRANSCRIPT

Page 1: CS12420 - Lecture 02 Menus

CS12420 - Lecture 02Menus

Lynda Thomas

[email protected]

Page 2: CS12420 - Lecture 02 Menus

Menus are like Buttons

• Use a Listener

• Classes involved are:– JMenuBar– JMenu– JMenuItem

• Warning: you may use words slightly differently!

Page 3: CS12420 - Lecture 02 Menus

MenuBar

Menu

MenuItem

Page 4: CS12420 - Lecture 02 Menus

Look in menus directory for FirstMenuFrame.java

Page 5: CS12420 - Lecture 02 Menus
Page 6: CS12420 - Lecture 02 Menus

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...

Page 7: CS12420 - Lecture 02 Menus

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)

Page 8: CS12420 - Lecture 02 Menus

Filling in some blanks..

Page 9: CS12420 - Lecture 02 Menus

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);}

Page 10: CS12420 - Lecture 02 Menus

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’

Page 11: CS12420 - Lecture 02 Menus

More components

Radio buttons and Combo boxes

ComboBoxDemo.java

and

RadioButtonDemo.java

Page 12: CS12420 - Lecture 02 Menus

In this lecture

• We looked at menus

In the next lecture we look at drawing