GUI Components: Part 1

Similar documents
PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

BASICS OF GRAPHICAL APPS

Sri Vidya College of Engineering & Technology

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CS111: PROGRAMMING LANGUAGE II

Introduction to the JAVA UI classes Advanced HCI IAT351

Graphical User Interface (GUI)

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

This page intentionally left blank

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Graphical Applications

Recommended GUI Design Standards

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

Graphical User Interface (GUI)

GUI Event Handlers (Part I)

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Solo 4.6 Release Notes

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. Event Architecture. A pipeline: Event Capture

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. 2.4 Event Dispatch 1

Microsoft How to Series

SIMPLE TEXT LAYOUT FOR COREL DRAW. When you start Corel Draw, you will see the following welcome screen.

Word Creating & Using Tables. IT Training & Development (818) Information Technology

CS11 Java. Fall Lecture 4

MyProgram m i ng Lab. get with the programming. Through the power of practice and immediate personalized

Figure 1 Forms category in the Insert panel. You set up a form by inserting it and configuring options through the Properties panel.

Packages: Putting Classes Together

Widget. Widget is a generic name for parts of an interface that have their own behaviour. e.g., buttons, progress bars, sliders, drop-down

Introduction to Microsoft Excel 2010

Labels and Envelopes in Word 2013

Think of layers as a stack of transparencies. Layers can be changed independently of other layers by clicking on its name in the layers palette.

Sample Chapters. To learn more about this book, visit the detail page at: go.microsoft.com/fwlink/?linkid=192147

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Working with Tables in Microsoft Word

Center for Faculty Development and Support Making Documents Accessible

Windows and Events. created originally by Brian Bailey

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

SILVACO. An Intuitive Front-End to Effective and Efficient Schematic Capture Design INSIDE. Introduction. Concepts of Scholar Schematic Capture

WEEK NO. 12 MICROSOFT EXCEL 2007

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

Chapter 4: Single Table Form Lab

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Graphical User Interfaces (GUIs)

The figure below shows the Dreamweaver Interface.

Microsoft Excel Office 2016/2013/2010/2007 Tips and Tricks

Getting Familiar with Microsoft Word 2010 for Windows

OrgPublisher Advanced Box Layout Editor

Introduction to Microsoft Excel 2010

SAS Visual Analytics 8.2: Working with Report Content

Tabs, Tables & Columns

Starting Excel application

CS11 Java. Fall Lecture 3

Event Dispatch. Dispatching events to windows and widgets.

GUI: Racket Graphical Interface Toolkit

Microsoft Office Word 2016 for Mac

WORD XP/2002 USER GUIDE. Task- Formatting a Document in Word 2002

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Beyond 20/20. Browser - English. Version 7.0, SP3

CounselLink Reporting. Designer

Bridgeware Systems War Board Documentation

Access: Printing Data with Reports

Word 3 Microsoft Word 2013

Excel 2013 Intermediate

Microsoft Excel 2010 Basic

Introduction to MS Word XP 2002: An Overview

COGNOS (R) 8 COGNOS CONNECTION USER GUIDE USER GUIDE THE NEXT LEVEL OF PERFORMANCE TM. Cognos Connection User Guide

StarTeam Layout Designer Help

INTRODUCTION... 1 UNDERSTANDING CELLS... 2 CELL CONTENT... 4

Status Bar: Right click on the Status Bar to add or remove features.

Introduction Accessing MICS Compiler Learning MICS Compiler CHAPTER 1: Searching for Data Surveys Indicators...

Microsoft Word 2003 for Windows, Part 2

SETTING UP A. chapter

Graphical User Interfaces. Comp 152

The Racket Graphical Interface Toolkit

SmartView. User Guide - Analysis. Version 2.0

TABLE OF CONTENTS TABLE OF CONTENTS... 1 INTRODUCTION... 2 USING WORD S MENUS... 3 USING WORD S TOOLBARS... 5 TASK PANE... 9

Excel Select a template category in the Office.com Templates section. 5. Click the Download button.

ABOUT THIS COURSE... 3 ABOUT THIS MANUAL... 4 LESSON 1: MANAGING LISTS... 5

PowerPoint Spring 2002

BusinessObjects Frequently Asked Questions

Creating Professional Swing UIs Using the NetBeans GUI Builder

Coach s Office Playbook Tutorial Playbook i

SmartArt Office 2007

NiceForm User Guide. English Edition. Rev Euro Plus d.o.o. & Niceware International LLC All rights reserved.

Computer Nashua Public Library Introduction to Microsoft Word 2010

Lehigh University Library & Technology Services

Corel Ventura 8 Introduction

Access Review. 4. Save the table by clicking the Save icon in the Quick Access Toolbar or by pulling

Full file at Excel Chapter 2 - Formulas, Functions, Formatting, and Web Queries

CS Exam 1 Review Suggestions

5 Setting Preferences 15 Preferences 15 Configure Chart Colors 16

Microsoft Excel Level 1

Tutorials. Lesson 3 Work with Text

Title bar: The top most bar in Word window that usually displays the document and software names.

Chapter 7 Inserting Spreadsheets, Charts, and Other Objects

Educational Fusion. Implementing a Production Quality User Interface With JFC

Transcription:

1 2 11 GUI Components: Part 1 Do you think I can listen all day to such stuff? Lewis Carroll Even a minor event in the life of a child is an event of that child s world and thus a world event. Gaston Bachelard You pays your money and you takes your choice. Punch Guess if you can, choose if you dare. Pierre Corneille 1992-2007 Pearson Education Inc. All rights reserved. OBJECTIVES In this chapter you will learn: The design principles of graphical user interfaces (GUIs). To build GUIs and handle events generated by user interactions with GUIs. To understand the packages containing GUI components, event-handling classes and interfaces. To create and manipulate buttons, labels, lists, text fields and panels. To handle mouse events and keyboard events. To use layout managers to arrange GUI components 3 11.1 Introduction 11.2 Simple GUI-Based Input/Output with 11.3 Overview of Swing Components 11. Displaying Text and Images in a Window 11.5 Text Fields and an Introduction to Event Handling with Nested Classes 11.6 Common GUI Event Types and Listener Interfaces 11.7 How Event Handling Works 11. 11.9 Buttons That Maintain State 11.9.1 11.9.2 11.10 and Using an Anonymous Inner Class for Event Handling 11.11 11.12 Multiple-Selection Lists 11.13 Mouse Event Handling 11.1 Adapter Classes 11.15 Sublcass for Drawing with the Mouse 11.16 Key-Event Handling 11.17 Layout Managers 11.17.1 11.17.2 5 11.1 Introduction Graphical user interface (GUI) Presents a user-friendly mechanism for interacting with an application Often contains title bar, menu bar containing menus, buttons and combo boxes Built from GUI components 6 11.17.3 11.1 Using Panels to Manage More Complex Layouts 11.19 11.20 Wrap-Up 1

7 Look-and-Feel Observation 11.1 button menus title bar menu bar combo box Consistent user interfaces enable a user to learn new applications faster. scroll bars Fig. 11.1 Internet Explorer window with GUI components. 11.2 Simple GUI-Based Input/Output with Dialog boxes Used by applications to interact with the user Provided by Java s class Contains input dialogs and message dialogs 9 1 ##% && '( 2 ##% ) 3 % *##% 5 Show input dialog to receive first 6 + integer 7 ",-% %./0 + 9 ## ) % 10 -%)12 -%)12 Show input dialog to receive 11 3 %,56)%5 56)%50* second integer 12 -%12 -%12 13 3 %,56%5 56%50* 1 15 ##"-%" ) 16 &23% 3,)1 17 '23% 3,1 1 19 2&7'* 2&7'*## Show message dialog to output 20 sum to user 21 ## % % 22 % %, 9 95 55 579 79 23 5-)3%5 5-)3%59 9 31:6--60* 2 ;## 25 ;## 10 Input dialog displayed by lines 10 11 11 12 Prompt to the user When the user clicks OK, 3 % returns to the program the 100 typed by the user as a -%. The program must convert the -% to an title bar Input dialog displayed by lines 12 13 Text field in which the user types a value Look-and-Feel Observation 11.2 The prompt in an input dialog typically uses sentence-style capitalization a style that capitalizes only the first letter of the first word in the text unless the word is a proper noun (for example, Deitel). Message dialog displayed by lines 22 23 When the user clicks OK, the message dialog is dismissed (removed from the screen) 2

13 1 Look-and-Feel Observation 11.3 The title bar of a window typically uses book-title capitalization a style that capitalizes the first letter of each significant word in the text and does not end with any punctuation (for example, Capitalization in a Book Title). Message dialog type 6:6--6 3131:6--6 <131:6--6 =>6-31:6--6 Icon Description A dialog that indicates an error to the user. A dialog with an informational message to the user. A dialog warning the user of a potential problem. A dialog that poses a question to the user. This dialog normally requires a response, such as clicking a Yes or a No button. 31:6--6 no icon A dialog that contains a message, but no icon. Fig. 11.3 static constants for message dialogs. 11.3 Overview of Swing Components 15 16 Swing GUI components Declared in package % Most are pure Java components Part of the Java Foundation Classes (JFC) Component Description Displays uneditable text or icons. Enables user to enter data from the keyboard. Can also be used to display editable or uneditable text. Triggers an event when clicked with the mouse. Specifies an option that can be selected or not selected. Provides a drop-down list of items from which the user can make a selection by clicking an item or possibly by typing into the box. Provides a list of items from which the user can make a selection by clicking on any item in the list. Multiple elements can be selected. Provides an area in which components can be placed and organized. Can also be used as a drawing area for graphics. Fig. 11. Some basic GUI components. Swing vs. AWT Abstract Window Toolkit (AWT) Precursor to Swing Declared in package Does not provide consistent, cross-platform look-and-feel 17 Portability Tip 11.1 Swing components are implemented in Java, so they are more portable and flexible than the original Java GUI components from package, which were based on the GUI components of the underlying platform. For this reason, Swing GUI components are generally preferred. 1 3

Lightweight vs. Heavyweight GUI Components Lightweight components Not tied directly to GUI components supported by underlying platform Heavyweight components Tied directly to the local platform AWT components Some Swing components 19 Look-and-Feel Observation 11. The look and feel of a GUI defined with heavyweight GUI components from package may vary across platforms. Because heavyweight components are tied to the localplatform GUI, the look and feel varies from platform to platform. 20 Superclasses of Swing s Lightweight GUI Components Class (package ) Subclass of! Declares many behaviors and attributes common to GUI components Class (package ) Subclass of Organizes s Class (package %) Subclass of Superclass of all lightweight Swing components 21 Software Engineering Observation 11.1 Study the attributes and behaviors of the classes in the class hierarchy of Fig. 11.5. These classes declare the features that are common to most Swing components. 22 23 Superclasses of Swing s Lightweight GUI Components Common lightweight component features Pluggable look-and-feel to customize the appearance of components Shortcut keys (called mnemonics) Common event-handling capabilities Brief description of component s purpose (called tool tips) Support for localization 2 Fig. 11.5 Common superclasses of many of the Swing components.

11. Displaying Text and Images in a Window 25 Labeling GUI Components 26 Class Most windows are an instance or subclass of this class Provides title bar Provides buttons to minimize, maximize and close the application Label Text instructions or information stating the purpose of each component Created with class 27 2 Look-and-Feel Observation 11.5 Text in a normally uses sentence-style capitalization. Specifying the Layout Laying out containers Determines where components are placed in the container Done in Java with layout managers One of which is class Set with the method of class 1 ##% &&?( 2 ## % 3 *##) % % *##" ) 5 % *## % 6 % -% *## -% 7 % 3*##) % % 3 %3*## % 9 10 11 + 12 " &*##! 13 " '*## 1 " @*## 15 16 ## 17,0 1 + 19,,5% 50* 20,,0##) 21 29 27 ## %93 % % 2 3%2 3 %3,%,0 %,5%& %)500* 0 29 '2,5 59%9 9%9 30 -% -% 6 31 ',5 '5 32, '## ' 33 3 @2,0*## % 35 @,5 5 36 @ 3,%## 37 @ AB,-% 6160* 3 @ C,-% 39 @,5 @5 0, @## @ 1 ;## 2 ;## 30 22 ## % % 23 &2,5 5 2 &,5 &5 25, &## & 26 5

1 ##% && D( 2 ##% 3 % * 31 Creating and Attaching & 32 5 6 + 7 " ",-% %./0 + 9 2,0*## 10 ), 6E3:1:-60* 11 -B,'DF 'DF9 9&GH &GH ##) B 12 C, ## ) 13 ;## ## 1 ;## Method of class Specifies the tool tip Method of class Adds a component to a container 33 3 Common Programming Error 11.1 If you do not explicitly add a GUI component to a container, the GUI component will not be displayed when the container appears on the screen. Look-and-Feel Observation 11.6 Use tool tips to add descriptive text to your GUI components. This text helps the user determine the GUI component s purpose in the user interface. 35 36 Creating and Attaching ' Creating and Attaching @ Interface 3 Other methods Can be added to a with the 3 method % and Implemented by class 3 %3 Interface -% Declares a set of common integer constants such as those used to set the alignment of components Can be used with methods AB % and C % For setting and retrieving the text of a label %3 and 3 For setting and retrieving the icon displayed in the label %AB and AB For setting and retrieving the horizontal position of the text displayed in the label 6

37 Creating and Displaying a Window 3 Other methods Constant Horizontal-position constants -% 6 -% 616 -% 3A Vertical-position constants -% -% 616 -% Description Place text on the left. Place text in the center. Place text on the right. Place text at the top. Place text in the center. Place text at the bottom. ) Dictates how the application reacts when the user clicks the close button -B Specifies the width and height of the window C Determines whether the window is displayed () or not () ) Fig. 11. Some basic GUI components. 11.5 Text Fields and an Introduction to Event Handling with Nested Classes 39 11.5 Text Fields and an Introduction to Event Handling with Nested Classes 0 GUIs are event-driven A user interaction creates an event Common events are clicking a button, typing in a text field, selecting an item from a menu, closing and window and moving the mouse The event causes a call to a method called an event handler Class Superclass of Superclass of Adds echo character to hide text input in component Allows user to enter text in the component when component has the application s focus 1 ##% && I( 2 ## % 3 * " * 5 " 6"* 6 % * 7 % * * % * 9 % * (1 of 3) 10 11 12 + 13 " &* &*##)B 1 " '* '*##) ##) 15 " @* @*##) B 16 " *## ) 17 1 ## 19,0 20 + 21,,5% 50* 22,,0##) 23 Create a new 2 ##)&H 25 &2 &2 &H &H0* 26,&## & 27 1 2 ##)) 29 '2 '2 565 5650* 30,'## ' 31 32 ##)) '& Create a new 33 @2 @2 5> )59 9'& '&0* 3 @ 6,) ) ## % (2 of 3) 35,@## @ Make this uneditable 36 37 ## )) 3 2,5A5 5A50* 39, ## 0 Create a new 1 ##%" 2 A 2 A,0* 3 &, Create event handler ', 5 @, 6, 7 ;## ## 9 ##" )" % 50 " A Create event handler class by 51 + implementing the 52 ##)" 53 " " ),6""0 interface 5 + Register event handler 55 -%%255 55* *## % Declare ) 56 method 2 7

57 ##6& 5 ) ) " %-,022&0 0 59 %2-% ),5&(J5 5&(J59 60 " %,00* Test if the source of the event is the first text field 61 Get text from text field 62 ##6' 63 ) ) " %-,022'0 0 Test if the source of the event! is " the 6 %2-% ),5'(J5 5'(J59 second text field 65 " %,00* (3 of 3) 66 Get text from text field 67 ##6@ @ 6 ) ) " %-,022@0 0 Test if the source of the event is the 69 %2-% ),5@(J5 5@(J59 9 third text field 70 " %,00* 71 72 ##6 Get text from text field 73 ) ) " %-,022 0 0 Test if the source of the event is the 7 %2-% ),5 (J59 9 password field 75 -%, -%, %,000* 0 76 77 ## Get password from password field 7 % %, 9% 9% 79 ;## ) 0 ;##" A 1 ;## 3 1 ##% && &H( 2 ##% 3 % * 5 6 + 7 " ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,@'F @'F9 9&HH &HH ##) B 12 C, ## ) 13 ;## 1 ;## 5 Steps Required to Set Up Event Handling for a GUI Component Several coding steps are required for an application to respond to events Create a class for the event handler Implement an appropriate event-listener interface Register the event handler 6 Using a Nested Class to Implement an Event Handler Top-level classes Not declared within another class Nested classes Declared within another class Non- nested classes are called inner classes Frequently used for event handling 7 Software Engineering Observation 11.2 An inner class is allowed to directly access its top-level class s variables and methods, even if they are ".

Using a Nested Class to Implement an Event Handler 9 Registering the Event Handler for Each Text Field 50 Ks and s Registering an event handler Pressing enter within either of these fields causes an 6" Processed by objects that implement the interface Call method to register an object listens for events on the object 51 52 Software Engineering Observation 11.3 The event listener for an event must implement the appropriate event-listener interface. Common Programming Error 11.2 Forgetting to register an event-handler object for a particular GUI component s event type causes events of that type to be ignored. Details of Class A s ) Method 53 11.6 Common GUI Event Types and Listener Interfaces 5 Event source Event types Component from which event originates Can be determined using method %- Text from a can be acquired using % Text from a can be acquired using % All are subclasses of <6" Some declared in package " Those specific to Swing components declared in % " 9

11.6 Common GUI Event Types and Listener Interfaces 55 56 Delegation event model Event source is the component with which user interacts Event object is created and contains information about the event that happened Event listener is notified when an event happens Fig. 11.11 Some event classes of package ". 57 11.7 How Event Handling Works 5 Remaining questions How did the event handler get registered? How does the GUI component know to call ) rather than some other eventhandling method? Fig. 11.12 Some common event-listener interfaces of package ". Registering Events 59 60 Every has instance variable Object of type 6" Maintains references to all its registered listeners Fig. 11.13 Event registration for &. 10

Event-Handler Invocation 61 11. 62 Events are dispatched to only the event listeners that match the event type Events have a unique event ID specifying the event type K6"s are handled by s and s KL6"s are handled by Ls Button Component user clicks to trigger a specific action Can be command button, check box, toggle button or radio button Button types are subclasses of class Look-and-Feel Observation 11.7 Buttons typically use book-title capitalization. 63 11. Command button Generates an 6" when it is clicked Created with class Text on the face of the button is called button label 6 65 66 Look-and-Feel Observation 11. Having more than one with the same label makes the s ambiguous to the user. Provide a unique label for each button. Fig. 11.1 Swing button hierarchy. 11

1 ##% && &F( 2 ## % 3 * " * 5 " 6"* 6 % * 7 % * % 3* 9 % 3 %3* 10 % * 11 12 13 + 1 " *##! 15 " ) *## ## 16 17 ## 1,0 19 + 20,,5%5 5%50* Create new 21,,0##) 22 23 2 5 5 5 ## Create two 3 %3s 2, ## 25 Create new 26 3%&2 3%&2 3 %3,%,0 %,5%& %)500* 0 27 3%'2 3%'2 3 %3,%,0 %,5%' %)500* 0 Set rollover icon for 2 ) ) 2 5 59%& 9%&## % 29 ) ) "3,%'##" % 30,) ## ) Declare two instance variables 67 31 32 ## A )" % 33 A 2 A,0* 3 ) ), 35, Create handler for buttons 36 ;## 37 3 ## )" % 39 " A 0 + 1 ## " 2 " " ),6""0 6""0 Inner class implements 3 + % %, 9-% ), 5 5M(J5 5M(J59 9" %,000* 0 6 ;## ) 7 ;##" A A ;## Register event handlers Access outer class s instance using reference Get text of pressed 6 1 ##% && &?( 2 ##% 3 % * 69 70 5 6 + 7 " ",-% %./0 + 9 2,0*## 10 ), 6E3:1:-60* 11 -B,'DF 'DF9 9&&H &&H ##) B 12 C, ## ) 13 ;## ## 1 ;## 11. Ks can have a rollover icon Appears when mouse is positioned over a button Added to a with method "3 71 Look-and-Feel Observation 11.9 Because class supports displaying text and images on a button, all subclasses of also support displaying text and images. 72 12

73 7 Look-and-Feel Observation 11.10 Using rollover icons for s provides users with visual feedback indicating that when they click the mouse while the cursor is positioned over the button, an action will occur. Software Engineering Observation 11. When used in an inner class, keyword refers to the current inner-class object being manipulated. An inner-class method can use its outer-class object s by preceding with the outer-class name and a dot, as in. 11.9 Buttons That Maintain State 75 11.9.1 76 State buttons Swing contains three types of state buttons %%, and and are subclasses of %% K Contains a check box label that appears to right of check box by default Generates an 36" when it is clicked K 36"s are handled by an 3 Passed to method - % Method - returns whether check box is selected () or not () ) 1 ##% && &D( 2 ## % 3 * * 5 " 3* 6 " 36"* 7 % * % * 9 % * 10 11 12 + 13 " * *## %%) 1 " * *### ### 15 " *### 16 17 ## 1,0 19 + 20,,55 550* (1 of 3) Declare two instance variables 21,,0,0##) 22 Set font of text field 23 ## ) 2 2 5< ) %59 9'H 'H0* 25, 5-)5 5-)59 9 319 9&N &N00* 0 26,## 27 77 2 2 2 55 55 ## 29 2 53 5 ## 30,## ## 31, ## Create two es 32 33 ##%) ##%) 3 A 2 A,0* 35 3, Create event handler 36 3, (2 of 3) 37 ;## Register event handler with 3 es 39 ##" )3" % 0 " A 3 3 1 + Inner class implements 2 " " 2 31* *## ##) 3 3 " " 3 2 31* *## ) 5 ##" ##" 6 " "- %,36""0 7 + ##" " 9 ) ) " %-,0220 0 50 " 2 - % method is called when a is clicked 51 -,0O O ( ( 31* 52 Test whether is selected 7 13

53 ## " 5 ) ) " %-,022 0 0 55 " 3 2 Test source of the event 56 -,0O O 33( ( 31* 79 1 ##% && &G( 2 ##% 3 % * 0 57 5 ##)) - method returns 59, whether is selected 60 5-)5 5-)59" 7" 3 9&N &N00* 0 61 ;##- % (3 of 3) 5 6 + 7 ",-% %./0 + 9 2,0* 62 ;##" A 10 ), 6E3:1:-60* 63 ;## 11 -B,'DF 'DF9 9&HH &HH ##) B 12 C, ## ) 13 ;## 1 ;## 11.9.2 K Has two states selected and unselected Normally appear in a group in which only one radio button can be selected at once Group maintained by a object Declares method to add a to group Usually represents mutually exclusive options 1 Common Programming Error 11.3 Adding a object (or an object of any other class that does not derive from ) to a container results in a compilation error. 2 1 ##% && &I( 2 ## % % 3 * * 5 " 3* 6 " 36"* 7 % * % * 9 % * 10 % * 11 12 3 (1 of 3) 13 + Declare four s 1 " * *## ) % 15 " *##)) and a to manage 16 " * *##)) them 17 " *##)) 1 " 3 *##)) 19 " *## 20 " *## ## 21 " *## 22 " 3 * *## 23 " *##% 2 25 ## 26,0 27 + 2,,5 5 50* 29,,0##) 30 31 2 5< ) %59 9'F 'F0* 32,## 33 3 ## 35 2,5 59 9 0* 36 2,55 559 9) ) 0* 37 2,53 59 9) ) 0* 3 3 2,5#3 59 9) ) 0* 39, ## (2 of 3) 0, ## 1, ## Create the four s 2,3 ## 3 ## % 5 2,0*,0*## 6, ## % Create the 7, ## %, ## % 9,3 ## 50 Add each to the 51 ## )! 52 2 2 5-)5 5-)59 9 319 9&N &N0* 53 2 5-)5 5-)59 9 9 9&N &N0* 5 2 5-)5 5-)59 9 339 9&N &N0* 55 3 2 5-)5 5-)59 9 7 7 339 9&N &N0* 56, ## ) 57 1

5 ##%") 59 3, 60 A, 0 61 3, 62 A,0 63 3, 6 A, 0 65 3 3, 66 A,3 00* 0 67 ;## 6 69 ##" " 70 " A 3 3 Register an event handler with each 71 + 72 " )* )*##) Event handler inner class 73 implements 3 7 A,)0 75 + 76 )2)*##)) 77 ;## A 7 79 ## " 0 " "- %,36""0 1 + 2,)##)) 3 ;##- % ;##" A 5 ;## 5 (3 of 3) When radio button is selected, the text field s font will be set to the value passed to the constructor 1 ##% && 'H( 2 ##% 3 % * 5 6 + 7 " ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,@HH @HH9 9&HH &HH ##) B 12 C, ## ) 13 ;## 1 ;## 6 11.10 and Using an Anonymous Inner Class for Event Handling Combo box Also called a drop-down list Implemented by class Each item in the list has an index sets the maximum number of rows shown at once provides a scrollbar and up and down arrows to traverse list 7 Look-and-Feel Observation 11.11 Set the maximum row count for a to a number of rows that prevents the list from expanding outside the bounds of the window in which it is used. This configuration will ensure that the list displays correctly when it is expanded by the user. Using an Anonymous Inner Class for Event Handling Anonymous inner class Special form of inner class Declared without a name Typically appears inside a method call Has limited access to local variables 9 1 ##% && '&( 2 ##>% % 3 * " 3* 5 " 36"* 6 % * 7 % * % * 9 % 3* 10 % 3 %3* 11 12 13 + 1 " %*## ) 15 " " *## 16 Declare instance 17 " -%./2 variable 1 +5%& %)59 95%' %)59 95 "% %)59 95% %)5;* ;* 19 " 3./2+ 3./2+ 20 3 %3,%,0 %,.H/009 /009 21 3 %3,%,0 %,.&/009 /009 22 3 %3,%,0 %,.'/009 /009 23 3 %3,%,0 %,.@/00;* /00;* 2 90 25 ## 26,0 27 + 2,,5%5 5%50* 0* 29,,0##) 30 15

31 %2 ## ## 32 %,@ ## 33 Create and set maximum row count 3 % 3, 35 3, 3,0 0## Create anonymous inner class as 36 + 37 ## " the event handler 3 " "- %,36""0 39 + Declare method 0 ## ## - % 1 ) )" %- %,02236" -6660 0 2 3,.. 3 % %-3,0/ Test state change of ; ;##- % 5 ; ;## 6 0*## 3 Method %-3 7 locates selected item, %## 9 2,.H/ /## ) 50, ## 51 ;## 52 ;## 91 1 ##% && ''( 2 ##% 3 % * 5 6 + 7 ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,@FH @FH9 9&FH &FH ##) B 12 C, ## ) 13 ;## 1 ;## 92 Scrollbar to scroll through the items in the list scroll arrows scroll box 93 9 Software Engineering Observation 11.5 Software Engineering Observation 11.6 An anonymous inner class declared in a method can access the instance variables and methods of the top-level class object that declared it, as well as the method s ) local variables, but cannot access the method s non-) variables. Like any other class, when an anonymous inner class implements an interface, the class must implement every method in the interface. 11.11 List Displays a series of items from which the user may select one or more items Implemented by class Allows for single-selection lists or multiple-selection lists A -6" occurs when an item is selected Handled by a - and passed to method " % 95 1 ##% && '@( 2 ##-%) 3 * * 5 % * 6 % * 7 % - * % " -* 9 % " -6"* 10 % -* 11 12 13 + 1 " * *## 15 " ) - -%1./2+5 59 955 559 95 59 9 16 5 59 95 59 955 559 95% 59 95 % 59 5 17 5 %59 955 559 955 559 95<5 5<59 95M5 5M5;* ;* Declare instance variable 1 " )./2+./2+ L9 9 >69 9 M19 9 19 L:M9 9 M9 9 6619 9 3A:M9 9 20 619 9 169 9 31L9 9 69 9 <A369 9 21 M6<;* ;* 22 23 ## - % 2,0 25 + 26,,55 550* 27,,0##) 2 96 16

29 2 2 1 ## 1 30 C,F ## )" 31 Create 32 ## 33 -,- -316:-66310* 3 35 ## - %) Set selection mode of 36, -,0 37 3 -, 39 -,0 -,0## Add to - and 0 + add to application 1 ## " 2 " "" %,-6""0 3 + %,0 %,, 5. %-3,0/0* / 6 ;##" % 7 ;## Get index of selected item ## - 9 ;## 50 ;## 97 1 ##% && 'N( 2 ##-%) 3 % * 5 6 + 7 " ",-% %./0 + 9 2,0*## 10 ), 6E3:1:-60* 11 -B,@FH @FH9 9&FH &FH ##) B 12 C, ## ) 13 ;## 1 ;## 9 11.12 Multiple-Selection Lists Multiple-selection list Enables users to select many items Single interval selection allows only a continuous range of items Multiple interval selection allows any set of elements to be selected 99 1 ##% && 'F(- 2 ##%) 3 * " * 5 " 6"* 6 % * 7 % * % * 9 % - * 10 % -* 11 12-13 + 1 " * *## 15 " * *## 16 " * *## 17 " ) -%1./2+5 59 955 559 95 59 9 1 5 59 95 59 955 559 95% 5% 59 95 % 59 5 95 %59 9 19 55 559 955 559 95<5 5<59 95M5 5M5;* ;* 20 100 - (1 of 3) 21 ##- 22 -,0 23 + 2,,5-5 5-50* 25,,0##) 26 27 2 1 ## ) 2 C,F ##)" 29 -, 30 - >36:316C:-6631 :-66310* 31, -,0## ## Use a multiple interval selection 32 list 33 2 5PPP5 5PPP5 ## - 3, 35 36,0,0## ## 37 + 3 ## " 39 " " ),6""0 0 + 1 ## " 2, %-C,0 3 ;## ) ;## 5 ## ## 6 (2 of 3) Use methods and %-C to copy values from one to the other 101 7,## 9 2,0*,0*## 50 C,F ##F Set cell width for presentation 51 <,&HH &HH ## ## 52 A%,&F &F ##% ##% 53 -, Set cell height for presentation - 5 - -316:316C:-66310* 55, -,0## (3 of 3) 56 ;##- Set selection model to single 57 ;## - interval selection 102 17

1 ##% && '?(- 2 ##%- 3 % * 5 - - 6 + 7 " ",-% %./0 + 9 - - - 2 10 -,0* 11 - ), 12 6E3:1:-60* 13 - -B,@FH @FH9 9&NH &NH ##) B 1 - C, ## ) 15 ;## 16 ;## - - 103 11.13 Mouse Event Handling Mouse events Create a 6" object Handled by s and s 3 combines the two interfaces Interface < declares method <" to handle <6"s 10 105 106 MouseListener and MouseMotionListener interface methods Methods of interface "6""0 Called when a mouse button is pressed while the mouse cursor is on a component. "6""0 "0 Called when a mouse button is pressed and released while the mouse cursor remains stationary on a component. This event is always preceded by a call to. ",6""0 Called when a mouse button is released after being pressed. This event is always preceded by a call to and one or more calls to %%. "6,6""0 Called when the mouse cursor enters the bounds of a component. MouseListener and MouseMotionListener interface methods "6,6""0 Called when the mouse cursor leaves the bounds of a component. Methods of interface " %%,6""0 Called when the mouse button is pressed while the mouse cursor is on a component and the mouse is moved while the mouse button remains pressed. This event is always preceded by a call to. All drag events are sent to the component on which the user began to drag the mouse. "",6""0 Called when the mouse is moved when the mouse cursor is on a component. All move events are sent to the component over which the mouse is currently positioned. Fig. 11.27 and interface methods. (Part 1 of 2.) Fig. 11.27 and interface methods. (Part 2 of 2.) Look-and-Feel Observation 11.12 Method calls to %% and are sent to the for the on which a mouse drag operation started. Similarly, the mousereleased method call at the end of a drag operation is sent to the for the on which the drag operation started. 107 1 ##% && 'G( 2 ## %" 3 * * 5 " * 6 " * 7 " 6"* 6"* % * 9 % * 10 % * 11 12 13 + 1 " *## " 15 " *## ") 16 17 ## >3 1 ##%" 19,0 20 + 21,,5 %6"5 50* 22 23 2,0*## 2 %, <A36 ## % 25, 9 616 ## Create and add to 26 application 27 2,5 50* 0* 2, 9 ->A0* ## ## 29 (1 of ) Create to capture mouse events Set background to white 10 1

30 ## %) " 31 A 2 A,0* 32, 33, Create event handler for mouse events 3 ;## 35 Register event handler 36 " A 9 9 37 3 + 39 ##" 0 ## " ) (2 of ) 1 " "6""0 6""0 2 + Declare method 3,-% ),5.J9J/59 9 " " %E,09" %M,000* 0 5 ;## 6 Implement mouse listener interfaces Find location of mouse click 109 60 61 ## " 62 " "6,6""0 6,6""0 63 + Declare 6 method 6,-% ),5.J9J/59 9 65 " %E,09" %M,000* 0 66 %, 6610* 67 ;##6 Set background of 6 (3 of ) 69 ## " 70 " "6,6""0 6,6""0 71 + Declare 6 method 72,5 5 50* 73 %, <A360* 7 ;##6 Set background of 75 110 7 ## " " "6""0 6""0 9 + Declare method 50,-% ),5.J9J/5 /59 9 51 " %E,09" %M,000* 0 52 ;## 53 5 ## " ) %%% 55 " ",6""0 Declare 56 + 57,-% ),5.J9J/59 method 9 5 " %E,09 9" %M,000* 0 59 ;## 76 ##" 77 ## " % 7 " " %%,6""0 79 + Declare %% method 0,-% ),5 %%.J9J/59 1 " %E,09" %M,000* 0 2 ;## %% 3 ## "" ( of ) 5 " "",6""0 ",6""0 6 + Declare " method 7,-% ),5".J9J/59 9 " %E,09" %M,000* 0 9 ;##" 90 ;## A 91 ;## 111 1 ##% && 'I( 2 ##% 3 % * 5 6 + 7 " ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,@HH @HH9 9&HH &HH ##) B 12 C, ## ) 13 ;## 1 ;## 112 113 11.1 Adapter Classes 11 Adapter class Implements event listener interface Provides default implementation for all event-handling methods 19

Software Engineering Observation 11.7 When a class implements an interface, the class has an is a relationship with that interface. All direct and indirect subclasses of that class inherit this interface. Thus, an object of a class that extends an event-adapter class is an object of the corresponding event-listener type (e.g., an object of a subclass of is a ). 115 Extending K Adapter class for and interfaces Extending class allows you to override only the methods you wish to use 116 117 11 Common Programming Error 11. If you extend an adapter class and misspell the name of the method you are overriding, your method simply becomes another method in the class. This is a logic error that is difficult to detect, since the program will call the empty version of the method inherited from the adapter class. Event-adapter class in " Implements interface L L < < Fig. 11.30 Event-adapter classes and the interfaces they implement in package ". 1 ##% && @&( 2 ## % %% 3 * * 5 " * 6 " 6"* * 7 % * % * 9 10 11 + 12 " -% *##-%% 13 " *## ) 1 15 ## -% % 16,0 17 + 1,,5 50* 19 119 25 ## " 26 " A 27 + 2 ## " 29 " "6""0 6""0 30 + 31 2" %E,0*##%) 32 2" %M,0*##%) 33 3 2-% ),5J,05 5J,059 9 35 " %,00* Get number of times mouse button 36 was clicked 37 ) ) ",00 0##% 3 725%5 5%5* Test for right mouse button 39 ) ) ",00 0## 0 7255 55* Test for middle mouse button 1 ##) ##) 2 725)5 5)5* 3 120 20 2,55 550* 21, 9 ->A0* 22, A,0## 23 ;## ## Register event handler 2, ## % 5 ;## 6 ;##" A 7 ;## 20

1 ##% && @'( 2 ##% 3 % * 121 122 5 6 + 7 " ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,NHH NHH9 9&FH &FH ##) B 12 C, ## ) 13 ;## 1 ;## 11.15 Subclass for Drawing with the Mouse 123 12 Overriding class Provides a dedicated drawing area 36" method Description,0,0 Returns when the user clicks the right mouse button on a mouse with two or three buttons. To simulate a right-mousebutton click on a one-button mouse, the user can hold down the Meta key on the keyboard and click the mouse button. Returns when the user clicks the middle mouse button on a mouse with three buttons. To simulate a middle-mousebutton click on a one- or two-button mouse, the user can press the Alt key on the keyboard and click the only- or left-mouse button, respectively. Fig. 11.33 36" methods that help distinguish among left-, center- and right-mouse-button clicks. 125 126 Method Method Draws on a Swing component Overriding method allows you to create custom drawings Must call superclass method first when overridden Look-and-Feel Observation 11.13 Most Swing GUI components can be transparent or opaque. If a Swing GUI component is opaque, its background will be cleared when its method is called. Only opaque components can display a customized background color. objects are opaque by default. 21

127 12 Error-Prevention Tip 11.1 In a subclass s method, the first statement should always be a call to the superclass s method to ensure that an object of the subclass displays correctly. Common Programming Error 11.5 If an overridden method does not call the superclass s version, the subclass component may not display properly. If an overridden method calls the superclass s version after other drawing is performed, the drawing will be erased. Defining the Custom Drawing Area Customized subclass of Provides custom drawing area Class is used to draw on Swing components Class represents an x-y coordinate 129 1 ##% && @N( 2 ##>% 3 * * 5 " 6"* 6 " * 7 % * 9 10 + 11 " 2 2H* *##) 12 13 ## )&HHHH ) 1 "./2./2..&HHHH &HHHH/* /* 15 Create array of s 16 ##>3 %" 17,0 1 + 19 ## ) " 20, 21 130 22,0## 23 + 2 ## % Anonymous inner class for event handling 25 " " %%,6""0 26 + Override %% method 27 ) )Q %0 2 + 29../2" /2" %,0*##) 30 77* 77*##) 31,0*## Get location of mouse cursor 32 ; ;##) ##) 33 ;## ## %% 3 ;## Repaint the 35 ## 36 ;## 37 3 ## " NR RN% ) 39 " ", %0 0 + 1,%## % 2 3 ## ) ) 2 2H*Q*770 *Q*770 5 % )",./ 9./ 9 9N9 9N0* 6 ;## ## 7 ;## Get the x and y-coordinates of the 131 Look-and-Feel Observation 11.1 Calling for a Swing GUI component indicates that the component should be refreshed on the screen as soon as possible. The background of the GUI component is cleared only if the component is opaque. method S can be passed a argument indicating whether the component is opaque ( ) or transparent ()) ). 132 22

Look-and-Feel Observation 11.15 Drawing on any GUI component is performed with coordinates that are measured from the upper-left corner (0, 0) of that GUI component, not the upper-left corner of the screen. 133 1 ##% && @F( 2 ##% 3 * % * 5 % * 6 7 + 9 ",-% %./0 10 + 11 ## ## 12 2,5 % 50* 13 1 2,0*## 15, 9 616 ## 16 Create instance of custom drawing 17 ## ->A) panel 1,,5 % 509 09 13 19 ->A0* 20 21 ), 6E3:1:-60* 22 -B,NHH NHH9 9'HH 'HH ##) B 23 C, ## ) 2 ;## ## 25 ;## 135 11.16 Key-Event Handling 136 KL interface For handling L6"s Declares methods, and 1 ##% && @?(L 2 ## %" 3 * " L* 5 " L6"* 6 % * 7 % * * L 137 30 ## ) 31 " "L6""0 L6""0 32 + 33 &2-% ),5L(J5 5L(J59 9 Declare method 3 " %L," %L,000* ## ## 35 ' @," ## Get code of pressed key L 36 ;## 37 13 9 L L L (1 of 3) 10 + 11 " -%&2 -%&255 55* *##)) 12 " -%'2 -%'255 55* *##) Implement L 13 " -%@2 -%@255 55* *##) interface 1 " *## 15 16 ##L 17 L,0 1 + Set background color 19,,5 %L6"50* 20 21 2,&H &H9 9&F &F ## ## 22,5 50* 5 23 6,) ) ## Register application as event 2, L ## handler for itself 25, ## 26 3 ## ) (2 of 3) 39 " ",L6""0 0 + Declare method 1 &2-% ),5L (J59 2 " %L," %L,00 ## 3 ' @,"## Get code of released key ;## 5 6 ## ) Declare method 7 " "L6""0 L6""0 + 9 &2-% ),5L(J5 5L(J59 9" %L,00* 50 ' @,"## 51 ;## 52 Get character typed 27 L, ## ) " 2 ;##L 29 23

53 ## ) 5 " " "' @,L6""0 55 + 139 1 ##% && @D(L 2 ##%L 3 % * 10 56 '2-% ),5J 59 9 57 " L,0O O55 55( (55 5500* 0 Test if it was an action key 5 L 59 -% -%2" %L)," %),0 60 Determine any modifiers pressed 61 @2-% ),5)(J5 5)(J59 9 (3 of 3) 5 L L 6 + 7 " ",-% %./0 + 9 L 2 L,0* L 62 S,55 550O 0O55 55(0 (0##) 10 ), 6E3:1:-60* 63 11 -B,@FH @FH9 9&HH &HH ##) B 6,-% ),5J 5JTJ JTJ JT5 59 9 12 C, 0* ## ## ) 65 &9'9@0##) ##) 13 ;## ## 66 ;##' @ 1 ;## L 67 ;## L 11 11.17 Layout Managers 12 L Layout managers Provided to arrange GUI components in a container Provide basic layout capabilities Implement the interface % 13 1 Look-and-Feel Observation 11.16 Most Java programming environments provide GUI design tools that help a programmer graphically design a GUI; the design tools then write the Java code to create the GUI. Such tools often provide greater control over the size, position and alignment of GUI components than do the built-in layout managers. Look-and-Feel Observation 11.17 It is possible to set a s layout to, which indicates that no layout manager should be used. In a without a layout manager, the programmer must position and size the components in the given container and take care that, on resize events, all components are repositioned as necessary. A component s resize events can be processed by a. 2

11.17.1 15 16 K Simplest layout manager Components are placed left to right in the order they are added Components can be left aligned, centered or right aligned Layout manager Description Default for %. Places components sequentially (left to right) in the order they were added. It is also possible to specify the order of the components by using the method, which takes a and an integer index position as arguments. Default for s (and other windows). Arranges the components into five areas: 1A, ->A, 6-, <6- and 616. Arranges the components into rows and columns. Fig. 11.3 Layout managers. 1 ##% && @I( 2 ## % % 3 * * 5 " * 6 " 6"* 7 % * % * 9 10 11 + 12 " )* )*## %) 13 " * *## % 1 " %* %*## %% 15 " *##! 16 " *## 17 1 ##>3 % 19,0 20 + 21,,5 50* 22 Create 23 2,0*## 2 2%,0*##% 25, ##) 26 Set layout of application (1 of 3) 17 27 ##) % 2 )2 5)5 5)5 ## ) 29,)## )) 30 ), Add ; 31 will handle placement 32,0,0## 33 + 3 ##)" 35 " " ),6""0 (2 of 3) 36 + 37 %, 60* 3 Set alignment to left 39 ## % 0, 1 ;## ) 2 ;## Adjust layout 3 ## 5 ## % 6 2 55 55 ## 7,## ), Add ; 9 50,0,0## will handle placement 51 + 52 ##" 53 " " ),6""0 Set alignment to center 5 + 55 %, 6160* 56 1 57 ## % 5, 59 ;## ) 60 ;## 61 ## 62 63 ##% % 6 %2, 5%5 5%5 ## % Adjust layout 65,%## %) (3 of 3) 66 %, Add ; 67 6,0,0## will handle placement 69 + 70 ##%" 71 " " ),6""0 72 + 73 %, 3A0* 7 Set alignment to right 75 ## % 76, 77 ;## ) 7 ;## Adjust layout 79 ## 0 ;## ## 1 ;## 19 1 ##% && NH( 2 ##% 3 % * 5 6 + 7 " ",-% %./0 + 9 ) 2,0* 10 ) ), 6E3:1:-60* 11 ) -B,@HH @HH9 9DF DF ##) B 12 ) C, ## ) 13 ;## 1 ;## ## 150 25

151 11.17.2 152 K Arranges components into five regions north, south, east, west and center Implements interface %' Provides horizontal gap spacing and vertical gap spacing 153 15 Look-and-Feel Observation 11.1 Each container can have only one layout manager. Separate containers in the same application can use different layout managers. Look-and-Feel Observation 11.19 If no region is specified when adding a to a, the layout manager assumes that the should be added to region 616. Common Programming Error 11.6 When more than one component is added to a region in a, only the last component added to that region will be displayed. There is no error that indicates this problem. 155 1 ##% && N&( 2 ## % 3 * " * 5 " 6"* 6 % * 7 % * 9 10 + 11 "./*./*## ) 12 " ) -%./2+5A15 5A159 95A-5 5A-59 9 13 5A6 59 95A 5A<5 <59 95A5 5A5;* ;* 1 " *##! 15 Declare instance 16 ##>3 " % variable 17,0 1 + 19,,5 50* 20 21 2,F9 9F ##F% Set layout 22, ##) 23 2. %/*##B) 2 25 ## %) 26 ) ) 2 2H*Q %*770 27 + Register event handler 2./2./ 29./, 0* 30 ;##) Create 156 26

31 32,.H/9 /9 1A ## 33,.&/9 /9 ->A ## 3,.'/9 /9 6- ## 35,..@/9 /9 <6-0*## 36,.N/9 /9 616 ## 37 ;## Add buttons to application using 3 39 ## " layout manager constants 0 " " ),6""0 6""0 1 + 2 ##" % 3 ) ) (0 (0 Make button invisible + 5 ) )" %-,0220 6 C,) ) ## ## Make button visible 7 C, 0* ## ## 9 ;##) 50 157 1 ##% && N'( 2 ##% 3 % * 5 6 + 7 " ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,@HH @HH9 9'HH 'HH ##) B 12 C, ## ) 13 ;## ## 1 ;## horizontal gap vertical gap 15 51,%,0## 52 ;## ) Update layout 53 ;## 159 11.17.3 160 K Divides container into a grid Every component has the same width and height 1 ##% && N@( 2 ## % 3 * * 5 " * 6 " 6"* 7 % * % * 9 10 11 + 12 "./*./*## ) 13 " ) -%./2 1 +55 559 955 559 955 559 95)5 5)59 95)"5 5)"59 955 55;* ;* Declare two 15 " %%2 %%2 * *##%% instance variables 16 " *##) 17 " % &*##)% 1 " % '*##% 19 20 ##R % 21,0 Create 22 + 23,,5 50* 2 % &2,'9 9@9 9F9 9F ##'@*% )F 25 % '2,@9 9' ##@ ##@'*% 26 2%,0*##% 27,% &## 2 2. %/*## ) Set layout 29 161 30 ) ) 2 2H*Q %*770 31 + 32./2./ 33./, ##% 3,./ *## 35 ;##) Add button to 36 ;## 37 3 ## "%%% 39 " " ),6""0 0 + Use second layout 1 ) )%%0 %%0 2,% '## Use first layout 3,% &## ) 5 6 %%2U%%*##%%" 7 ",0*## ##R ;## ) Update layout 9 ;## 162 27

1 ##% && NN( 2 ##% 3 % * 5 6 + 7 ",-% %./0 + 9 % 2,0* 10 % ), 6E3:1:-60* 11 % -B,@HH @HH9 9'HH 'HH ##) B 163 11.1 Using Panels to Manage More Complex Layouts Complex GUIs often require multiple panels to arrange their components properly 16 12 % C, ## ) 13 ;## 1 ;## ## 1 ##% && NF( 165 2 ##>% 3 * * 5 % * 6 % * 7 % * 9 10 + 11 " *## 12 "./*./*## ) Declare a to hold buttons 13 1 ##R % 15,0 16 + Create 17,,5 50* 1 2..F/* /*## 19 2,0*## 20,,&9 %0 21 Set layout 22 ## 23 ) ) 2 2H*Q %*770 2 + 25./2 55 5577 77&00* 0 26,./## 27 ;##) ##) 2 Add button to panel 29, 9 ->A ## 30 ;## Add panel to application 31 ;## 166 1 ##% && N?( 2 ##% 3 % * 5 6 + 7 " ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,NFH NFH9 9'HH 'HH ##) B 12 C, ## ) 13 ;## 1 ;## 167 11.19 K Provides an area for manipulating multiple lines of text K container Subclass of Uses a layout manager 16 2

Look-and-Feel Observation 11.20 To provide line-wrapping functionality for a, invoke method R < with a argument. 169 1 ##% && ND( 2 ##%) 3 " * " 6"* 5 % * 6 % * 7 % * * % * 9 % - * 10 11 12 + 13 " &*## % 1 " '*## ##%% 15 " * *## %) 16 Declare instance variables 170 17 ##R % 1,0 19 + 20,,5 5 21 2 AB,0*## 22 -%25 %T5 57 7 23 5 %T) ) T5 57 7 Create text area and add to box 2 5 5 % T "T5 5* 25 Create a container 26 &2,9 9&H &H9 9&F &F ## & 27, -, &0## 2 29 2 5PPP5 5PPP5 ## 30,## 31, 32 33,0,0## 3 + + 35 ## ') & 36 " " ),6""0 37 + 3 ', & %-,0 39 ;## ## ) 0 ;## 1 ## 2 3 '2,&H &H9 9&F &F ## ' 6,) ) ## % % 5, -, '0## 6 7,## ) ;## 9 ;## Add button to box Copy selected text from one text area to the other Create second text area and add it to box 171 1 ##% && NG( 2 ##%) 3 % * 5 6 + 7 " ",-% %./0 + 9 2,0* 10 ), 6E3:1:-60* 11 -B,N'F N'F9 9'HH 'HH ##) B 12 C, ## ) 13 ;## ## 1 ;## 172 173 - Scrollbar Policies 17 K- has scrollbar policies Horizontal policies Always (A3V1:-:<M-) As needed (A3V1:-:-:1666) Never (A3V1:-:16C6) Vertical policies Always (C63:-:<M-) As needed (C63:-:-:1666) Never (C63:-:16C6) 29