Download presentation
Presentation is loading. Please wait.
Published byArleen Davis Modified over 9 years ago
1
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition
2
Java Programming: From Problem Analysis to Program Design, Second Edition2 Chapter Objectives Learn about applets. Explore the class Graphics. Learn about the class Font. Explore the class Color. Learn to use additional layout managers. Become familiar with more GUI components. Learn how to create menu-based programs. Explore how to handle key and mouse events.
3
Java Programming: From Problem Analysis to Program Design, Second Edition3 Inheritance Hierarchy of GUI Classes
4
Java Programming: From Problem Analysis to Program Design, Second Edition4 Constructors and Methods of the class Component
5
Java Programming: From Problem Analysis to Program Design, Second Edition5 Constructors and Methods of the class Component
6
Java Programming: From Problem Analysis to Program Design, Second Edition6 Constructors and Methods of the class Component
7
Java Programming: From Problem Analysis to Program Design, Second Edition7 Constructors and Methods of the class Component
8
Java Programming: From Problem Analysis to Program Design, Second Edition8 Applets A Java program that is embedded within a Web page and executed by a Web browser. Create an applet by extending the class JApplet. class JApplet is contained in package javax.swing.
9
Java Programming: From Problem Analysis to Program Design, Second Edition9 Applets
10
Java Programming: From Problem Analysis to Program Design, Second Edition10 Applets
11
Java Programming: From Problem Analysis to Program Design, Second Edition11 Applets No main method. Methods init, start, and paint guaranteed to be invoked in sequence. To develop an applet: Override any/all of the methods above.
12
Java Programming: From Problem Analysis to Program Design, Second Edition12 Applet Methods init method: Initializes variables. Gets data from user. Places various GUI components. paint method: Performs output.
13
Java Programming: From Problem Analysis to Program Design, Second Edition13 Skeleton of a Java Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { }
14
Java Programming: From Problem Analysis to Program Design, Second Edition14 Applet Displaying Welcome Message //Welcome Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { public void paint(Graphics g) { super.paint(g); //Line 1 g.drawString( " Welcome to Java Programming ", 30, 30); //Line 2 }
15
Java Programming: From Problem Analysis to Program Design, Second Edition15 HTML to Run Applet
16
Java Programming: From Problem Analysis to Program Design, Second Edition16 class Font Shows text in different fonts. Contained in package java.awt. Available fonts: Serif/Sans Serif Monospaced Dialog/DialogInput Arguments for constructor: String specifying the font face name. int value specifying font style. int value specifying font size. Expressed in points (72 points = 1 inch).
17
Java Programming: From Problem Analysis to Program Design, Second Edition17 class Font
18
Java Programming: From Problem Analysis to Program Design, Second Edition18 class Color Shows text in different colors. Changes background color of component. Contained in package java.awt.
19
Java Programming: From Problem Analysis to Program Design, Second Edition19 class Color
20
Java Programming: From Problem Analysis to Program Design, Second Edition20 class Color
21
Java Programming: From Problem Analysis to Program Design, Second Edition21 class Color
22
Java Programming: From Problem Analysis to Program Design, Second Edition22 class Graphics Provides methods for drawing items such as lines, ovals, and rectangles on the screen. Contains methods to set the properties of graphic elements including clipping areas, fonts, and colors. Contained in the package java.awt.
23
Java Programming: From Problem Analysis to Program Design, Second Edition23 class Graphics
24
Java Programming: From Problem Analysis to Program Design, Second Edition24 Constructors and Methods for the class Graphics
25
Java Programming: From Problem Analysis to Program Design, Second Edition25 Constructors and Methods for the class Graphics
26
Java Programming: From Problem Analysis to Program Design, Second Edition26 Constructors and Methods for the class Graphics
27
Java Programming: From Problem Analysis to Program Design, Second Edition27 Constructors and Methods for the class Graphics
28
Java Programming: From Problem Analysis to Program Design, Second Edition28 Differences Between Applets and GUI Applications Applets Derived from JApplet. No main method. Uses init method. Displayed by HTML. Sets title in HTML. Size set in HTML. Applet closes when HTML doc closes. GUI applications Class extends JFrame. Invokes main method. Uses constructors. Uses method setVisible. Uses setTitle method. Uses method setSize. Closes with Exit button.
29
Java Programming: From Problem Analysis to Program Design, Second Edition29 Converting a GUI Application to an Applet Change JFrame to JApplet. Change constructor to method init. Remove method calls such as setVisible, setTitle, setSize. Remove the method main. If applicable, remove Exit button and all code associated with it (for example, action listener).
30
Java Programming: From Problem Analysis to Program Design, Second Edition30 Additional GUI Components JTextArea JCheckBox JRadioButton JComboBox JList
31
Java Programming: From Problem Analysis to Program Design, Second Edition31 JTextArea Can collect multiple lines of input from user. Can display multiple lines of output. Pressing Enter key separates lines of text. Each line ends with newline character ( \n ). Derived from class JTextComponent.
32
Java Programming: From Problem Analysis to Program Design, Second Edition32 JTextArea
33
Java Programming: From Problem Analysis to Program Design, Second Edition33 JTextArea
34
Java Programming: From Problem Analysis to Program Design, Second Edition34 JTextArea Example
35
Java Programming: From Problem Analysis to Program Design, Second Edition35 JCheckBox User selects from predefined values. Example of a toggle button. Clicking JCheckBox generates item event. Use interface ItemListener and its abstract method itemStateChanged to handle event.
36
Java Programming: From Problem Analysis to Program Design, Second Edition36 JCheckBox
37
Java Programming: From Problem Analysis to Program Design, Second Edition37 Constructors and Methods of class JCheckBox
38
Java Programming: From Problem Analysis to Program Design, Second Edition38 Constructors and Methods of class JCheckBox
39
Java Programming: From Problem Analysis to Program Design, Second Edition39 JRadioButton Created same way as check boxes. Placed in content pane of applet. Forces user to select only one radion button at a time. You create a button group to group radio buttons. Generates an ItemEvent. interface ItemListener and method itemStateChanged used to handle events.
40
Java Programming: From Problem Analysis to Program Design, Second Edition40 JRadioButton
41
Java Programming: From Problem Analysis to Program Design, Second Edition41 JRadioButton
42
Java Programming: From Problem Analysis to Program Design, Second Edition42 JComboBox Commonly known as a drop-down list. Used to select an item from a list of possibilities. Generates an ItemEvent. Event monitored by ItemListener. ItemListener invokes method itemStateChanged.
43
Java Programming: From Problem Analysis to Program Design, Second Edition43 JComboBox
44
Java Programming: From Problem Analysis to Program Design, Second Edition44 JComboBox
45
Java Programming: From Problem Analysis to Program Design, Second Edition45 JList
46
Java Programming: From Problem Analysis to Program Design, Second Edition46 JList
47
Java Programming: From Problem Analysis to Program Design, Second Edition47 Layout Managers FlowLayout Default layout manager. Places components from left to right until no more items can be placed. Can align each line left, center, or right. Default alignment: LEFT. GridLayout Similar to FlowLayout. All rows (columns) have same number of components. All components have the same size.
48
Java Programming: From Problem Analysis to Program Design, Second Edition48 Layout Managers BorderLayout Items placed into one of five specific regions: NORTH/SOUTH EAST/WEST CENTER NORTH and SOUTH components extend horizontally (completely span one edge to the other). EAST and WEST components extend vertically between components in NORTH and SOUTH regions. CENTER component expands to occupy any unused regions.
49
Java Programming: From Problem Analysis to Program Design, Second Edition49 Menus Allow for various functions without cluttering GUI with too many components. Can be attached to objects such as JFrame and JApplet ( setJMenuBar method). To set a menu bar: private JMenuBar menuMB = new JMenuBar(); setJMenuBar(menuMB); Add menus to menu bar; add menu items to menu. Order of menus added = order of appearance.
50
Java Programming: From Problem Analysis to Program Design, Second Edition50 Keyboard and Mouse Events
51
Java Programming: From Problem Analysis to Program Design, Second Edition51 Chapter Summary Creating applets class Font class Graphics class Color Differences between applets and GUI applications Converting GUI applications to applets
52
Java Programming: From Problem Analysis to Program Design, Second Edition52 Chapter Summary GUI components: JTextArea JCheckBox JRadioButton Layout managers Menus Keyboard and mouse events
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.