CH14 - Chapter 14 Java: an Introduction to Computer Science...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 14 ● Menus ● Making GUIs Pretty (and More Functional) ● Box Containers and Box Layout Managers ● More on Events and Listeners ● Another Look at the Swing Class Hierarchy More Swing Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch Menus ● Three Swing classes used to put a menu in a program: » JMenuBar » JMenu » JMenuItem ● Menu items behave in the same way as buttons AbstractButton JButton JMenu JMenuItem Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m); . . . JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Create a menu Create a menu item A menu item uses an action listener the same way a button does. Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m); . . . JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Each menu item is added to the menu. The menu is added to the menu bar. One way to add a menu bar to a JFrame Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch Nested Menus ● JMenu is a descendant of JMenuItem ● Every JMenu object is also a JMenuItem ● A JMenu can be a menu item in another menu ● This allows nested menus ● Clicking on a nested menu shows the items in the nested menu and allows them to be selected. AbstractButton JButton JMenu JMenuItem Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch Making GUIs Pretty (and More Functional) ● Adding Icons ● The JScrollPane Class for Scroll Bars ● Adding Borders ● Changing the Look and Feel Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch Using Icons ● Icons are (small) pictures ● Icons may be added to labels, buttons, and menu items. ● The ImageIcon class can be used to convert a picture to an icon: ImageIcon SmileyFaceIcon = new ImageIcon(“smiley.gif”); ● The setIcon method can be used to add an icon to a component: JLabel helloLabel = new JLabel(“Hello”); ImageIcon dukeWavingIcon = new ImageIcon(“duke_waving.gif”); helloLabel.setIcon(dukeWavingIcon); Chapter 14 Java: an Introduction to Computer Science & Programming - Walter Savitch The JScrollPane Class for Scroll Bars ● A view port is used when not all information can be displayed on screen at once....
View Full Document

This note was uploaded on 04/17/2008 for the course COMP SCI 201 taught by Professor Durga during the Spring '08 term at Wentworth Institute of Technology.

Page1 / 29

CH14 - Chapter 14 Java: an Introduction to Computer Science...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online