0% found this document useful (0 votes)
34 views39 pages

Java How To Program, 9/e: Reserved

Uploaded by

Amaly Al
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views39 pages

Java How To Program, 9/e: Reserved

Uploaded by

Amaly Al
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Java How to Program, 9/e

© Copyright 1992-2012 by Pearson Education, Inc. All Rights


Reserved.
 A graphical user interface (GUI) presents a user-
friendly mechanism for interacting with an application.
◦ Pronounced “GOO-ee”
◦ Gives an application a distinctive “look” and “feel.”
◦ Learn new applications more quickly and use them more
productively.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Built from GUI components.
◦ Sometimes called controls or widgets—short for window
gadgets.
 User interacts via the mouse, the keyboard or another
form of input, such as voice recognition.
 IDEs
◦ Provide GUI design tools to specify a component’s exact size
and location in a visual manner by using the mouse.
◦ Generates the GUI code for you.
◦ Greatly simplifies creating GUIs, but each IDE has different
capabilities and generates different code.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 Most applications use windows or dialog boxes (also called
dialogs) to interact with the user.

 JOptionPane (package javax.swing) provides


prebuilt dialog boxes for input and output
◦ Displayed via static JOptionPane methods.

 Figure 14.2 uses two input dialogs to obtain integers from


the user and a message dialog to display the sum of the
integers the user enters.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 JOptionPane static method showInputDialog
displays an input dialog, using the method’s String argument
as a prompt.

◦ JOptionPane dialog are dialog—the user cannot interact with the rest
of the application while dialog is displayed.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Converting Strings to int Values
◦ Integer class’s static method parseInt converts its String argument to
an int value.

 Message Dialogs
◦ JOptionPane static method showMessageDialog displays a message
dialog.
◦ The first argument helps determine where to position the dialog.
 If null, the dialog box is displayed at the center of your screen.

◦ The second argument is the message to display.


◦ The third argument is the String that should appear in the title bar at the top of the
dialog.
◦ The fourth argument is the type of message dialog to display.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 Swing GUI components located in package
javax.swing.

 Abstract Window Toolkit (AWT) in package java.awt is


another set of GUI components in Java.

◦ Java application with AWT GUI  the application’s GUI components


display differently on each platform.

 The appearance and the way in which the user interacts with the
application are known as that application’s look-and-feel.

 Swing GUI components allow you to specify a uniform look-


and-feel for your application across all platforms.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 Most Swing components are not tied to actual GUI components
of the underlying platform.
◦ Known as lightweight components.

 AWT components are tied to the local platform and are called
heavyweight components, because they rely on the local
platform’s windowing system to determine their functionality
and their look-and-feel.

 Several Swing components are heavyweight components.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Class Component (package java.awt) declares
many of the attributes and behaviors common to the
GUI components in packages java.awt and
javax.swing.

 Most GUI components extend class Component


directly or indirectly.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Class Container (package java.awt) is a subclass of
Component.
 Components are attached to Containers so that they can be
organized and displayed on the screen.
 Any object that is a Container can be used to organize other
Components in a GUI.
 Because a Container is a Component, you can place
Containers in other Containers to help organize a GUI.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Class JComponent (package javax.swing) is a
subclass of Container.
 JComponent is the superclass of all lightweight Swing
components, all of which are also Containers.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Some common lightweight component features supported
by JComponent include:
 pluggable look-and-feel

 Shortcut keys (called mnemonics)

 Common event-handling capabilities for components that initiate

the same actions in an application.

 tool tips

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Most windows that can contain Swing GUI components are
instances of class JFrame or a subclass of JFrame.
 JFrame is an indirect subclass of class java.awt.Window
 Provides the basic attributes and behaviors of a window
◦ a title bar at the top
◦ buttons to minimize, maximize and close the window

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 A label is created with class JLabel—a subclass of
JComponent.
◦ Displays read-only text, an image, or both text and an image.

 JFrame’s constructor uses its String argument as the


text in the window’s title bar.
 Must attach each GUI component to a container, such as a
JFrame.
 You typically must decide where to position each GUI
component.
◦ Known as specifying the layout of the GUI components.
◦ Java provides several layout managers that can help you position
components.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 FlowLayout
◦ GUI components are placed on a container from left to right in the
order in which the program attaches them to the container.
◦ When there is no more room to fit components left to right,
components continue to display left to right on the next line.

 Method setLayout is inherited from class Container.


◦ argument must be an object of a class that implements the
LayoutManager interface (e.g., FlowLayout).

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Method setToolTipText specifies the tool tip that is
displayed when the user positions the mouse cursor over a
JComponent (such as a JLabel).
 You attach a component to a container using the add
method, which is inherited indirectly from class
Container.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 An icon is normally specified with an Icon argument to a
constructor or to the component’s setIcon method.

 An Icon is an object of any class that implements interface


Icon (package javax.swing).

 ImageIcon (package javax.swing) supports several image


formats, including Graphics Interchange Format (GIF), Portable
Network Graphics (PNG) and Joint Photographic Experts Group
(JPEG).

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 A JLabel can display an Icon.
 JLabel constructor can receive text and an Icon.
◦ The last constructor argument indicates the justification of the label’s
contents.
◦ Interface SwingConstants (package javax.swing) declares a set
of common integer constants (such as SwingConstants.LEFT) that
are used with many Swing components.
◦ By default, the text appears to the right of the image when a label
contains both text and an image.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 Class JLabel provides methods to change a label’s appearance
after it has been instantiated.
 Method setText sets the text displayed on the label.
 Method getText retrieves  the current text displayed on a label.
 Method setIcon specifies the Icon to display on a label.
 Method getIcon retrieves  the current Icon displayed on a
label.
 Methods setHorizontalTextPosition and
setVerticalTextPosition specify the text position in the
label.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 By default, closing a window simply hides the window.
 Calling method setDefaultCloseOperation (inherited
from class JFrame) with the argument
JFrame.EXIT_ON_CLOSE indicates that the program should
terminate when the window is closed by the user.
 Method setSize specifies the width and height of the window
in pixels.
 Method setVisible with the argument true displays the
window on the screen.
© Copyright 1992-2012 by Pearson
Education, Inc. All Rights Reserved.
 GUIs are event driven.
 When the user interacts with a GUI component, the
interaction—known as an event—drives the program to
perform a task.
 The code that performs a task in response to an event is
called an event handler, and the overall process of
responding to events is known as event handling.

© Copyright 1992-2012 by Pearson


Education, Inc. All Rights Reserved.
 JTextFields and JPasswordFields (package
javax.swing).
 JTextField extends class JTextComponent (package
javax.swing.text), which provides many features common
to Swing’s text-based components.
 Class JPasswordField extends JTextField and adds
methods that are specific to processing passwords.
 JPasswordField shows that characters are being typed as the
user enters them, but hides the actual characters with an echo
© Copyright 1992-2012 by Pearson
character. Education, Inc. All Rights Reserved.

You might also like