JavaHTP6e_11.ppt

Embed Size (px)

Citation preview

  • 8/8/2019 JavaHTP6e_11.ppt

    1/174

    1

    © 2005 Pearson Education, Inc. All rights reserved.

    11

    11

    GUI Components:

    Part

  • 8/8/2019 JavaHTP6e_11.ppt

    2/174

    2

    © 2005 Pearson Education Inc. All rights reserved.

     

     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

  • 8/8/2019 JavaHTP6e_11.ppt

    3/174

    3

    © 2005 Pearson Education, Inc. All rights reserved.

    OBJECTIVES

    In this chapter "ou #ill learn: $he design principles o% graphical user inter%aces

    &GUIs'.

    $o (uild GUIs and handle events generated ("user interactions #ith GUIs.

    $o understand the pac)ages containing GUIcomponents, event*handling classes andinter%aces.

    $o create and manipulate (uttons, la(els, lists,

    te+t %ields and panels.

    $o handle mouse events and )e"(oard events.

    $o use la"out managers to arrange GUIcomponents

  • 8/8/2019 JavaHTP6e_11.ppt

    4/174

    4

    © 2005 Pearson Education, Inc. All rights reserved.

    11.1  Introduction

    11.2  Simple GUI-Based Input/Output it! JOptionPane

    11."  O#er#ie o$ Sin% Components

    11.&  'ispla(in% Te)t and Ima%es in a *indo

    11.+  Te)t ,ields and an Introduction to E#ent andlin% it!ested Classes

    11.  Common GUI E#ent T(pes and 0istener Inter$aces

    11.  o E#ent andlin% *ors11.3  JButton 

    11.4  Buttons T!at 5aintain State

    11.4.1  JCheckBox 

    11.4.2 JRadioButton

    11.16  JComboBox and Usin% an 7non(mous Inner Class $or E#entandlin%

  • 8/8/2019 JavaHTP6e_11.ppt

    5/174

    5

    © 2005 Pearson Education, Inc. All rights reserved.

    11.11  JList

    11.12  5ultiple-Selection 0ists

    11.1"  5ouse E#ent andlin%11.1&  7dapter Classes

    11.1+  JPanel Su8lcass $or 'rain% it! t!e 5ouse

    11.1  9e(-E#ent andlin%

    11.1  0a(out 5ana%ers11.1.1  FlowLayout 

    11.1.2  BorderLayout 

    11.1."  GridLayout

    11.13  Usin% :anels to 5ana%e 5ore Comple) 0a(outs

    11.14  JTextrea 

    11.26  *rap-Up

  • 8/8/2019 JavaHTP6e_11.ppt

    6/174

    6

    © 2005 Pearson Education, Inc. All rights reserved.

    11.1 Introduction

    • Graphical user interface (GUI)  Presents a user-friendly mechanism for interacting with an

    application

      Often contains title ar! menu ar containing menus!

    uttons and como o"es  Built from GUI components

  • 8/8/2019 JavaHTP6e_11.ppt

    7/174

    7

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.1

    Consistent user interfaces enale a user to learnnew applications faster#

  • 8/8/2019 JavaHTP6e_11.ppt

    8/174

    8

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.1 ; Internet E)plorer indo it! GUI components.

    (utton menus title (ar menu (ar com(o (o+

    scroll

    (ars

  • 8/8/2019 JavaHTP6e_11.ppt

    9/174

    9

    © 2005 Pearson Education, Inc. All rights reserved.

    11.2 Simple GUI-Based Input/Output it!

    JOptionPane

    • $ialog o"es  Used y applications to interact with the user

      Pro%ided y &a%a's JOptionPane class

    • Contains input dialogs and message dialogs

  • 8/8/2019 JavaHTP6e_11.ppt

    10/174

    10

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    ddition!"a#a

    (1 of 2)

    Show input dialog to receive first

    integer 

    Show input dialog to receive

    second integer 

    Show message dialog to output sum

    to user 

  • 8/8/2019 JavaHTP6e_11.ppt

    11/174

    11

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    ddition!"a#a

    (2 of 2)

    Input dialog displa"ed (" lines 0

    Input dialog displa"ed (" lines 2 

    /essage dialog displa"ed (" lines 22 2

    $e+t %ield in #hich

    the user t"pes a

    value

    Prompt to the user 

    hen the user clic)s O9, show$nput%ialo& 

    returns to the program

    the 166 t"ped (" theuser as a 'trin&. $heprogram must convert

    the 'trin& to an int

    title (ar 

    hen the user clic)s O9, the message dialog is dismissed

    &removed %rom the

    screen'

  • 8/8/2019 JavaHTP6e_11.ppt

    12/174

    12

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.2

    he prompt in an input dialog typically usessentence-style capitaliation a style that

    capitali*es only the first letter of the first wordin the te"t unless the word is a proper noun (fore"ample! $eitel)#

  • 8/8/2019 JavaHTP6e_11.ppt

    13/174

    13

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11."

    he title ar of a window typically uses !ook-titlecapitaliation a style that capitali*es the firstletter of each significant word in the te"t anddoes not end with any punctuation (for e"ample!Capitali*ation in a Boo+ itle)# 

  • 8/8/2019 JavaHTP6e_11.ppt

    14/174

    14

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11." ; JOptionPane static constants $or messa%e dialo%s.

    15

  • 8/8/2019 JavaHTP6e_11.ppt

    15/174

    15

    © 2005 Pearson Education, Inc. All rights reserved.

    11." O#er#ie o$ Sin% Components

    • ,wing GUI components  $eclared in pac+age javax.swing

      ost are pure &a%a components

      Part of the &a%a .oundation Classes (&.C)

    16

  • 8/8/2019 JavaHTP6e_11.ppt

    16/174

    16

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.& ; Some 8asic GUI components.

    17

  • 8/8/2019 JavaHTP6e_11.ppt

    17/174

    17

    © 2005 Pearson Education, Inc. All rights reserved.

    Sin% #s. 7*T

    • /stract 0indow ool+it (/0)  Precursor to ,wing

      $eclared in pac+age java.awt

      $oes not pro%ide consistent! cross-platform loo+-and-feel

    18

  • 8/8/2019 JavaHTP6e_11.ppt

    18/174

    18

    © 2005 Pearson Education, Inc. All rights reserved.

    :orta8ilit( Tip 11.1

    ,wing components are implemented in &a%a! sothey are more portale and fle"ile than theoriginal &a%a GUI components from pac+age

    "a#a!awt! which were ased on the GUIcomponents of the underlying platform# .or thisreason! ,wing GUI components are generallypreferred# 

    19

  • 8/8/2019 JavaHTP6e_11.ppt

    19/174

    19

    © 2005 Pearson Education, Inc. All rights reserved.

    0i%!tei%!t #s. ea#(ei%!t GUI

    Components

    • Lightweight components  1ot tied directly to GUI components supported y

    underlying platform

    • 2ea%yweight components

      ied directly to the local platform

      /0 components

      ,ome ,wing components

    20

  • 8/8/2019 JavaHTP6e_11.ppt

    20/174

    20

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.&

    he loo+ and feel of a GUI defined withhea%yweight GUI components from pac+age"a#a!awt may %ary across platforms# Becausehea%yweight components are tied to the local-platform GUI! the loo+ and feel %aries fromplatform to platform# 

    21

  • 8/8/2019 JavaHTP6e_11.ppt

    21/174

    21

    © 2005 Pearson Education, Inc. All rights reserved.

    Superclasses o$ Sin%

  • 8/8/2019 JavaHTP6e_11.ppt

    22/174

    22

    © 2005 Pearson Education, Inc. All rights reserved.

    So$tare En%ineerin% O8ser#ation 11.1

    ,tudy the attriutes and eha%iors of the classesin the class hierarchy of .ig# 33#4# hese classesdeclare the features that are common to most

    ,wing components#

    23

  • 8/8/2019 JavaHTP6e_11.ppt

    23/174

    23

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.+ ; Common superclasses o$ man( o$ t!e Sin% components.

    24

  • 8/8/2019 JavaHTP6e_11.ppt

    24/174

    24

    © 2005 Pearson Education, Inc. All rights reserved.

    Superclasses o$ Sin%

  • 8/8/2019 JavaHTP6e_11.ppt

    25/174

    25

    © 2005 Pearson Education, Inc. All rights reserved.

    11.& 'ispla(in% Te)t and Ima%es in a

    *indo

    • Class JFrame  ost windows are an instance or suclass of this class

      Pro%ides title ar

      Pro%ides uttons to minimi*e! ma"imi*e and close the

    application

    26

  • 8/8/2019 JavaHTP6e_11.ppt

    26/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0a8elin% GUI Components

    • Lael  e"t instructions or information stating the purpose of

    each component

      Created with class JLabel

    27

  • 8/8/2019 JavaHTP6e_11.ppt

    27/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.+

    e"t in a JLabel normally uses sentence-stylecapitali*ation#

    28

  • 8/8/2019 JavaHTP6e_11.ppt

    28/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Speci$(in% t!e 0a(out

    • Laying out containers  $etermines where components are placed in the container

      $one in &a%a with layout managers

    • One of which is class FlowLayout

      ,et with the setLayout method of class JFrame

    29

  • 8/8/2019 JavaHTP6e_11.ppt

    29/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    LabelFrame!"a#a

    (1 of 2)

    30

  • 8/8/2019 JavaHTP6e_11.ppt

    30/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    LabelFrame!"a#a

    (2 of 2)

    31

  • 8/8/2019 JavaHTP6e_11.ppt

    31/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    LabelTest!"a#a

    32

  • 8/8/2019 JavaHTP6e_11.ppt

    32/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Creatin% and 7ttac!in% label1

    • ethod setToolTipText of classJComponent

      ,pecifies the tool tip

    • ethod add of class Container

      /dds a component to a container

    33

  • 8/8/2019 JavaHTP6e_11.ppt

    33/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Common :ro%rammin% Error 11.1

    If you do not e"plicitly add a GUI componentto a container! the GUI component will not edisplayed when the container appears on the

    screen#

    34

  • 8/8/2019 JavaHTP6e_11.ppt

    34/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.

    Use tool tips to add descripti%e te"t to your GUIcomponents# his te"t helps the user determinethe GUI component's purpose in the user

    interface#

    35

  • 8/8/2019 JavaHTP6e_11.ppt

    35/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Creatin% and 7ttac!in% label

    • Interface !con  Can e added to a JLabel with the set!con method

      Implemented y class !mage!con

    • Interface "wingConstants

      $eclares a set of common integer constants such as those

    used to set the alignment of components

      Can e used with methods set#ori$ontal%lignment and set&ertical%lignment

    36

  • 8/8/2019 JavaHTP6e_11.ppt

    36/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Creatin% and 7ttac!in% label'

    • Other JLabel methods  getText and setText

    • .or setting and retrie%ing the te"t of a lael

      get!con and set!con

    • .or setting and retrie%ing the icon displayed in the lael

      get#ori$ontalTextPosition andset#ori$ontalTextPosition

    • .or setting and retrie%ing the hori*ontal position of the te"t

    displayed in the lael

    37

  • 8/8/2019 JavaHTP6e_11.ppt

    37/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.3 ; Some 8asic GUI components.

    38

    C ti d 'i l i L b lF

  • 8/8/2019 JavaHTP6e_11.ppt

    38/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Creatin% and 'ispla(in% a LabelFrame *indo

    • Other JFrame methods  set(e)aultCloseOperation

    • $ictates how the application reacts when the user clic+s the

    close utton

      set"i$e• ,pecifies the width and height of the window

      set&isible

    • $etermines whether the window is displayed (true) or not

    ()alse)

    39

    11 + T t ,i ld d I t d ti t

  • 8/8/2019 JavaHTP6e_11.ppt

    39/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.+ Te)t ,ields and an Introduction to

    E#ent andlin% it! ested Classes

    • GUIs are e%ent-dri%en  / user interaction creates an e%ent

    • Common e%ents are clic+ing a utton! typing in a te"t field!

    selecting an item from a menu! closing and window and

    mo%ing the mouse

      he e%ent causes a call to a method called an e%ent handler

    40

    11 + T t ,i ld d I t d ti t

  • 8/8/2019 JavaHTP6e_11.ppt

    40/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.+ Te)t ,ields and an Introduction to

    E#ent andlin% it! ested Classes

    • Class JTextComponent  ,uperclass of  JTextField

    • ,uperclass of  JPasswordField

      /dds echo character to hide te"t input in component

      /llows user to enter te"t in the component whencomponent has the application's focus

    41

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    41/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    TextFieldFrame!"a#a

    (1 of 3)

    Create a new JTextField

    42

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    42/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    TextFieldFrame!"a#a

    (2 of 3)

    Create a new JTextField

    Create a new uneditableJTextField

    Create a new JPasswordField

    Create event handler 

    Register event handler 

    Create event handler class by

    implementing the

    %ctionListener interface

    eclare actionPer)ormed method

    43

    -utline

    !est if the source of the event is the

  • 8/8/2019 JavaHTP6e_11.ppt

    43/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    TextFieldFrame!"a#a

    (3 of 3)

    !est if the source of the event is the

    first te"t field

    #et te"t from te"t field

    #et te"t from te"t field

    #et te"t from te"t field

    #et password from password field

    !est if the source of the event is the

    second te"t field

    !est if the source of the event is the

    third te"t field

    !est if the source of the event is the

     password field

    44

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    44/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    TextFieldTest!"a#a

    (1 of 2)

    45

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    45/174

    © 2005 Pearson Education,Inc. All rights reserved.

    -utline

    TextFieldTest!"a#a

    (2 of 2)

    46

    Steps =e>uired to Set Up E#ent andlin%

  • 8/8/2019 JavaHTP6e_11.ppt

    46/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Steps =e>uired to Set Up E#ent andlin%

    $or a GUI Component

    • ,e%eral coding steps are re5uired for anapplication to respond to e%ents

      Create a class for the e%ent handler

      Implement an appropriate e%ent-listener interface

      6egister the e%ent handler

    47

    Usin% a ested Class to Implement an

  • 8/8/2019 JavaHTP6e_11.ppt

    47/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Usin% a ested Class to Implement an

    E#ent andler 

    • op-le%el classes  1ot declared within another class

    • 1ested classes

      $eclared within another class

      1on-static nested classes are called inner classes

    • .re5uently used for e%ent handling

    48

  • 8/8/2019 JavaHTP6e_11.ppt

    48/174

    © 2005 Pearson Education, Inc. All rights reserved.

    So$tare En%ineerin% O8ser#ation 11.2

    /n inner class is allowed to directly access itstop-le%el class's %ariales and methods! e%en ifthey are pri#ate#

    49

    Usin% a ested Class to Implement an

  • 8/8/2019 JavaHTP6e_11.ppt

    49/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Usin% a ested Class to Implement an

    E#ent andler 

    *JTextFields and JPasswordFields  Pressing enter within either of these fields causes an

    %ction+vent

    • Processed y o7ects that implement the %ctionListener interface

    50

    =e%isterin% t!e E#ent andler $or Eac!

  • 8/8/2019 JavaHTP6e_11.ppt

    50/174

    © 2005 Pearson Education, Inc. All rights reserved.

    =e%isterin% t!e E#ent andler $or Eac!

    Te)t ,ield

    • 6egistering an e%ent handler  Call method add%ctionListener to register an

    %ctionListener o7ect

      %ctionListener listens for e%ents on the o7ect

    51

  • 8/8/2019 JavaHTP6e_11.ppt

    51/174

    © 2005 Pearson Education, Inc. All rights reserved.

    So$tare En%ineerin% O8ser#ation 11."

    he e%ent listener for an e%ent must implementthe appropriate e%ent-listener interface#

    52

  • 8/8/2019 JavaHTP6e_11.ppt

    52/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Common :ro%rammin% Error 11.2

    .orgetting to register an e%ent-handler o7ect fora particular GUI component's e%ent type causese%ents of that type to e ignored#

    53

    'etails o$ Class TextField#andler

  • 8/8/2019 JavaHTP6e_11.ppt

    53/174

    © 2005 Pearson Education, Inc. All rights reserved.

    'etails o$ Class TextField#andler sactionPer)ormed 5et!od

    • 8%ent source  Component from which e%ent originates

      Can e determined using method get"ource

      e"t from a JTextField can e ac5uired using

    get%ctionCommand  e"t from a JPasswordField can e ac5uired using

    getPassword

    54

    11 Common GUI E#ent T(pes and

  • 8/8/2019 JavaHTP6e_11.ppt

    54/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11. Common GUI E#ent T(pes and

    0istener Inter$aces

    • 8%ent types  /ll are suclasses of %,T+vent

      ,ome declared in pac+age java.awt.event

      hose specific to ,wing components declared in

    javax.swing.event

    55

    11 Common GUI E#ent T(pes and

  • 8/8/2019 JavaHTP6e_11.ppt

    55/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11. Common GUI E#ent T(pes and

    0istener Inter$aces

    • $elegation e%ent model  8%ent source is the component with which user interacts

      8%ent o7ect is created and contains information aout the

    e%ent that happened

      8%ent listener is notified when an e%ent happens

    56

  • 8/8/2019 JavaHTP6e_11.ppt

    56/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.11 ; Some e#ent classes o$ paca%e "a#a!awt!e#ent.

    57

  • 8/8/2019 JavaHTP6e_11.ppt

    57/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.12 ; Some common e#ent-listener inter$aces o$ paca%e "a#a!awt!e#ent.

    58

  • 8/8/2019 JavaHTP6e_11.ppt

    58/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11. o E#ent andlin% *ors

    • 6emaining 5uestions  2ow did the e%ent handler get registered9

      2ow does the GUI component +now to call

    actionPer)ormed rather than some other e%ent-

    handling method9

    59

    = i t i E t

  • 8/8/2019 JavaHTP6e_11.ppt

    59/174

    © 2005 Pearson Education, Inc. All rights reserved.

    =e%isterin% E#ents

    • 8%ery JComponent has instance %arialelistenerList

      O7ect of type +ventListenerList

      aintains references to all its registered listeners

    60

  • 8/8/2019 JavaHTP6e_11.ppt

    60/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.1" ; E#ent re%istration $or JTextField textField( .

    61

    E t dl I ti

  • 8/8/2019 JavaHTP6e_11.ppt

    61/174

    © 2005 Pearson Education, Inc. All rights reserved.

    E#ent-andler In#ocation

    • 8%ents are dispatched to only the e%ent listenersthat match the e%ent type

      8%ents ha%e a uni5ue e%ent I$ specifying the e%ent type

    *-ouse+vents are handled y

    -ouseListeners and-ouse-otionsListeners

    *ey+vents are handled y eyListeners

    62

    11 3 J/ tt

  • 8/8/2019 JavaHTP6e_11.ppt

    62/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.3 J/utton

    • Button  Component user clic+s to trigger a specific action

      Can e command utton! chec+ o"! toggle utton or radio

    utton

      Button types are suclasses of class %bstract/utton

    63

  • 8/8/2019 JavaHTP6e_11.ppt

    63/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.

    Buttons typically use oo+-title capitali*ation#

    64

    11 3 J/ tt

  • 8/8/2019 JavaHTP6e_11.ppt

    64/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.3 J/utton

    • Command utton  Generates an %ction+vent when it is clic+ed

      Created with class J/utton

      e"t on the face of the utton is called utton lael

    65

  • 8/8/2019 JavaHTP6e_11.ppt

    65/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.3

    2a%ing more than one JButton with the samelael ma+es the JButtons amiguous to the user#Pro%ide a uni5ue lael for each utton#

    66

  • 8/8/2019 JavaHTP6e_11.ppt

    66/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.1& ; Sin% 8utton !ierarc!(.

    67

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    67/174

    ©

     2005 Pearson Education,Inc. All rights reserved.

    ButtonFrame!"a#a

    (1 of 2)

    eclare two J/utton instancevariables

    Create new J/utton

    Create two !mage!cons

    Create new J/utton

    Set rollover icon for J/utton

    68

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    68/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ButtonFrame!"a#a

    (2 of 2)

    Create handler for buttons

    Register event handler 

    $nner class implements

    %ctionListener

    %ccess outer class&s instance usingt0is reference

    #et te"t of J/utton pressed

    69

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    69/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ButtonTest!"a#a

    (1 of 2)

  • 8/8/2019 JavaHTP6e_11.ppt

    70/174

    71

    11 3 J/utton

  • 8/8/2019 JavaHTP6e_11.ppt

    71/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.3 J/utton

    *J/uttons can ha%e a rollo%er icon  /ppears when mouse is positioned o%er a utton

      /dded to a J/utton with method setollover!con

    72

  • 8/8/2019 JavaHTP6e_11.ppt

    72/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.4

    Because class bstractButton supportsdisplaying te"t and images on a utton! allsuclasses of bstractButton also support

    displaying te"t and images#

    73

  • 8/8/2019 JavaHTP6e_11.ppt

    73/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.16

    Using rollo%er icons for JButtons pro%idesusers with %isual feedac+ indicating that whenthey clic+ the mouse while the cursor is

    positioned o%er the utton! an action will occur#

    74

  • 8/8/2019 JavaHTP6e_11.ppt

    74/174

    © 2005 Pearson Education, Inc. All rights reserved.

    So$tare En%ineerin% O8ser#ation 11.&

    0hen used in an inner class! +eyword this refers to the current inner-class o7ect eingmanipulated# /n inner-class method can use its

    outer-class o7ect's this y preceding this with the outer-class name and a dot! as inButtonFrame!this#

    75

    11 4 Buttons T!at 5aintain State

  • 8/8/2019 JavaHTP6e_11.ppt

    75/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.4 Buttons T!at 5aintain State

    • ,tate uttons  ,wing contains three types of state uttons

      JToggle/utton! JC0ec2/ox and Jadio/utton

      JC0ec2/ox and Jadio/utton are suclasses of

    JToggle/utton

    76

    11 4 1 JC0ec2/ox

  • 8/8/2019 JavaHTP6e_11.ppt

    76/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.4.1 JC0ec2/ox

    *JC0ec2/ox  Contains a chec+ o" lael that appears to right of chec+

    o" y default

      Generates an !tem+vent when it is clic+ed

    * !tem+vents are handled y an !temListener• Passed to method item"tateC0anged

      ethod is"elected returns whether chec+ o" isselected (true) or not ()alse)

    77

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    77/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    CheckBoxFrame!"a#a

    (1 of 3)eclare two JC0ec2/ox instance

    variables

    Set font of te"t field

    78

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    78/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    CheckBoxFrame!"a#a

    (2 of 3)

    Create two JC0ec2/oxes

    Create event handler 

    Register event handler with

    JC0ec2/oxes

    $nner class implements

    !temListener

    item"tateC0anged method iscalled when a JC0ec2/ox is

    clic'ed

    !est whether JC0ec2/ox isselected

    79

    -utline

    !est source of the event

  • 8/8/2019 JavaHTP6e_11.ppt

    79/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    CheckBoxFrame!"a#a

    (3 of 3)

    !est source of the event

    is"elected method returnswhether JC0ec2/ox is selected

    80

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    80/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    CheckBoxTest!"a#a

    81

    11 4 2 Jadio/utton

  • 8/8/2019 JavaHTP6e_11.ppt

    81/174

    © 2005 Pearson Education, Inc. All rights reserved.

    11.4.2 Jadio/utton

    *Jadio/utton  2as two states : selected and unselected

      1ormally appear in a group in which only one radio utton

    can e selected at once

    • Group maintained y a /utton3roup o7ect  $eclares method add to add a Jadio/utton to

    group

      Usually represents mutually e"clusi%e options

    82

  • 8/8/2019 JavaHTP6e_11.ppt

    82/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Common :ro%rammin% Error 11."

    /dding a ButtonGroup o7ect (or an o7ect of anyother class that does not deri%e from Component)to a container results in a compilation error#

    83

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    83/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    RadioButtonFrame!"a#a

    (1 of 3)

    eclare four Jadio/uttonsand a /utton3roup to manage

    them

    84

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    84/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    RadioButtonFrame!"a#a

    (2 of 3)

    Create the four Jadio/uttons

    Create the /utton3roup

    %dd each Jadio/utton to the/utton3roup

    85

    -utlineRegister an event handler with each

  • 8/8/2019 JavaHTP6e_11.ppt

    85/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    RadioButtonFrame!"a#a

    (3 of 3)

    Register an event handler with each

    Jadio/utton

    vent handler inner class

    implements !temListener

    hen radio button is selected* the

    te"t field&s font will be set to the

    value passed to the constructor 

    86

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    86/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    RadioButtonTest!"a#a

    87

    11.16 JCombo/ox and Usin% an 7non(mousI Cl $ E t dli

  • 8/8/2019 JavaHTP6e_11.ppt

    87/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Inner Class $or E#ent andlin%

    • Como o"  /lso called a drop-down list

      Implemented y class JCombo/ox

      8ach item in the list has an inde"

      set-aximumowCount sets the ma"imum numer ofrows shown at once

      JCombo/ox pro%ides a scrollar and up and down arrowsto tra%erse list

    88

  • 8/8/2019 JavaHTP6e_11.ppt

    88/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.11

    ,et the ma"imum row count for a JComboBox to anumer of rows that pre%ents the list frome"panding outside the ounds of the window in

    which it is used# his configuration will ensurethat the list displays correctly when it is e"pandedy the user#

    89

    Usin% an 7non(mous Inner Class $or

    E t dli

  • 8/8/2019 JavaHTP6e_11.ppt

    89/174

    © 2005 Pearson Education, Inc. All rights reserved.

    E#ent andlin%

    • /nonymous inner class  ,pecial form of inner class

      $eclared without a name

      ypically appears inside a method call

      2as limited access to local %ariales

    90

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    90/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ComboBoxFrame!"a#a

    (1 of 2)

    eclare JCombo/ox instancevariable

    91

    -utline

    Create JCombo/ox and setma"imum row count

  • 8/8/2019 JavaHTP6e_11.ppt

    91/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ComboBoxrame!"a#a

    (; of ;)

    Create anonymous inner class as

    the event handler 

    eclare methoditem"tateC0anged

    +ethod get"elected!ndex locates selected item

    !est state change of JCombo/ox

    92

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    92/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ComboBoxTest!"a#a

    1croll(ar to scroll through the

     items in the list

    scroll arro#s scroll (o+

    93

  • 8/8/2019 JavaHTP6e_11.ppt

    93/174

    © 2005 Pearson Education, Inc. All rights reserved.

    So$tare En%ineerin% O8ser#ation 11.+

    /n anonymous inner class declared in a methodcan access the instance %ariales and methods ofthe top-le%el class o7ect that declared it! as well

    as the method's )inal local %ariales! ut cannotaccess the method's non-)inal %ariales#

    94

  • 8/8/2019 JavaHTP6e_11.ppt

    94/174

    © 2005 Pearson Education, Inc. All rights reserved.

    So$tare En%ineerin% O8ser#ation 11.

    Li+e any other class! when an anonymous innerclass implements an interface! the class mustimplement e%ery method in the interface#

    95

    11.11 JList

  • 8/8/2019 JavaHTP6e_11.ppt

    95/174

    © 2005 Pearson Education, Inc. All rights reserved.

    • List  $isplays a series of items from which the user may select

    one or more items

      Implemented y class JList

      /llows for single-selection lists or multiple-selection lists  / List'election*#ent occurs when an item is selected

    • 2andled y a List'electionListener and passed tomethod #alueChan&ed

    96

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    96/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ListFrame!"a#a

    (3 of ;)

    eclare JList instance variable

    97

    -utline

    Create JList

  • 8/8/2019 JavaHTP6e_11.ppt

    97/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ListFrame!"a#a

    (; of ;)

    Set selection mode of JList

    %dd JList to "crollPane andadd to application

    #et inde" of selected item

    98

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    98/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    ListTest!"a#a

    99

    11.12 5ultiple-Selection 0ists

  • 8/8/2019 JavaHTP6e_11.ppt

    99/174

    © 2005 Pearson Education, Inc. All rights reserved.

    p

    •ultiple-selection list  8nales users to select many items

      ,ingle inter%al selection allows only a continuous range of

    items

      ultiple inter%al selection allows any set of elements to eselected

    100

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    100/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ultiple'electionFrame!"a#a

    (3 of

  • 8/8/2019 JavaHTP6e_11.ppt

    101/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ultiple'electionFrame!"a#a

    (; of

  • 8/8/2019 JavaHTP6e_11.ppt

    102/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ultiple'electionFrame!"a#a

    (< of

  • 8/8/2019 JavaHTP6e_11.ppt

    103/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ultiple'electionTest!"a#a

    104

    11.1" 5ouse E#ent andlin%

  • 8/8/2019 JavaHTP6e_11.ppt

    104/174

    © 2005 Pearson Education, Inc. All rights reserved.

    •ouse e%ents  Create a +ouse*#ent o7ect

      2andled y +ouseListeners and+ouse+otionListeners

      +ouse$nputListener comines the two interfaces  Interface +ouse,heelListener declares method

    mouse,heel+o#ed to handle +ouse,heel*#ents

    105

  • 8/8/2019 JavaHTP6e_11.ppt

    105/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.2 ; +ouseListener and +ouse+otionListener inter$acemet!ods. ?:art 1 o$ 2.@

    106

  • 8/8/2019 JavaHTP6e_11.ppt

    106/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11.2 ; +ouseListener and +ouse+otionListener inter$acemet!ods. ?:art 2 o$ 2.@

    107

  • 8/8/2019 JavaHTP6e_11.ppt

    107/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.12

    ethod calls to mouse%ra&&ed and mouseReleased are sent to the +ouse+otionListener for the

    Component on which a mouse drag operationstarted# ,imilarly! the mouseReleased method callat the end of a drag operation is sent to the+ouseListener for the Component on which the

    drag operation started#

    108

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    108/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouseTrackerFrame!"a#a

    (3 of =)

    Create JPanel to capture mouseevents

    Set bac'ground to white

    Create JLabel and add toapplication

    109

    -utline

    Create event handler for mouse

    events

  • 8/8/2019 JavaHTP6e_11.ppt

    109/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouseTrackerFrame!"a#a

    (; of =)

    Register event handler 

    $mplement mouse listener interfaces

    -ind location of mouse clic' 

    eclare mouseClic2ed method

    eclare mousePressed method

    eclare mouseeleased method

    110

    -utline

    eclare mouse+ntered method

  • 8/8/2019 JavaHTP6e_11.ppt

    110/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouseTrackerFrame!"a#a

    (< of =)

    Set bac'ground of JPanel

    eclare mouse+xited method

    Set bac'ground of JPanel

    111

    -utline

    eclare mouse(ragged method

  • 8/8/2019 JavaHTP6e_11.ppt

    111/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouseTrackerFrame!"a#a

    (= of =)

    eclare mouse-oved method

    112

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    112/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouseTrackerFrame!"a#a

    (3 of ;)

    113

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    113/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouseTrackerFrame!"a#a

    (; of ;)

    114

    11.1& 7dapter Classes

  • 8/8/2019 JavaHTP6e_11.ppt

    114/174

    © 2005 Pearson Education, Inc. All rights reserved.

    • /dapter class

      Implements e%ent listener interface

      Pro%ides default implementation for all e%ent-handling

    methods

    115

    So$tare En%ineerin% O8ser#ation 11

  • 8/8/2019 JavaHTP6e_11.ppt

    115/174

    © 2005 Pearson Education, Inc. All rights reserved.

    So$tare En%ineerin% O8ser#ation 11.

    0hen a class implements an interface! the classhas an >is a? relationship with that interface# /lldirect and indirect suclasses of that class inheritthis interface# hus! an o7ect of a class thate"tends an e%ent-adapter class is an o7ect of thecorresponding e%ent-listener type (e#g#! an o7ectof a suclass of +ousedapter is a +ouseListener)#

    116

    E)tendin% +ousedapter

  • 8/8/2019 JavaHTP6e_11.ppt

    116/174

    © 2005 Pearson Education, Inc. All rights reserved.

    *+ousedapter  /dapter class for +ouseListener and

    +ouse+otionListener interfaces

      8"tending class allows you to o%erride only the methods

    you wish to use

    117

    C : i E 11 &

  • 8/8/2019 JavaHTP6e_11.ppt

    117/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Common :ro%rammin% Error 11.&

    If you e"tend an adapter class and misspell thename of the method you are o%erriding! yourmethod simply ecomes another method in the

    class# his is a logic error that is difficult to detect!since the program will call the empty %ersion ofthe method inherited from the adapter class#

    118

  • 8/8/2019 JavaHTP6e_11.ppt

    118/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11."6 ; E#ent-adapter classes and t!e inter$aces t!e( implement inpaca%e "a#a!awt!e#ent.

    119

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    119/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouse%etailsFrame!"a#a

    (3 of ;)

    Register event handler 

    120

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    120/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouse%etailsFrame!"a#a

    (; of ;)

    #et number of times mouse button

    was clic'ed

    !est for right mouse button

    !est for middle mouse button

    121

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    121/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouse%etails!"a#a

    (3 of ;)

    122

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    122/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    +ouse%etails!"a#a

    (; of ;)

    123

    11.1+ JPanel Su8class $or 'rain% it!t!e 5ouse

  • 8/8/2019 JavaHTP6e_11.ppt

    123/174

    © 2005 Pearson Education, Inc. All rights reserved.

    • O%erriding class JPanel  Pro%ides a dedicated drawing area

    124

  • 8/8/2019 JavaHTP6e_11.ppt

    124/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11."" ; $nput*#ent met!ods t!at !elp distin%uis! amon% le$t-Acenter- and ri%!t-mouse-8utton clics.

    125

    5et!od paintComponent

  • 8/8/2019 JavaHTP6e_11.ppt

    125/174

    © 2005 Pearson Education, Inc. All rights reserved.

    • ethod paintComponent  $raws on a ,wing component

      O%erriding method allows you to create custom drawings

      ust call superclass method first when o%erridden

    126

    0oo and ,eel O8ser#ation 11 1"

  • 8/8/2019 JavaHTP6e_11.ppt

    126/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.1"

    ost ,wing GUI components can etransparent or opa5ue# If a ,wing GUIcomponent is opa5ue! its ac+ground will ecleared when its paintComponent method iscalled# Only opa5ue components can displaya customi*ed ac+ground color# JPanel o7ects are opa5ue y default#

    127

    Error :re#ention Tip 11 1

  • 8/8/2019 JavaHTP6e_11.ppt

    127/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Error-:re#ention Tip 11.1

    In a JComponent suclass's paintComponent method! the first statement should always e acall to the superclass's paintComponent method

    to ensure that an o7ect of the suclass displayscorrectly#

    128

    Common :ro%rammin% Error 11 +

  • 8/8/2019 JavaHTP6e_11.ppt

    128/174

    © 2005 Pearson Education, Inc. All rights reserved.

    Common :ro%rammin% Error 11.+

    If an o%erridden paintComponent method doesnot call the superclass's %ersion! the suclasscomponent may not display properly# If an

    o%erridden paintComponent method calls thesuperclass's %ersion after other drawing isperformed! the drawing will e erased#

    129

    'e$inin% t!e Custom 'rain% 7rea

  • 8/8/2019 JavaHTP6e_11.ppt

    129/174

    © 2005 Pearson Education, Inc. All rights reserved.

    • Customi*ed suclass of JPanel  Pro%ides custom drawing area

      Class Graphics is used to draw on ,wing components

      Class Point represents an " - y coordinate

    130

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    130/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    PaintPanel!"a#a

    (3 of ;)

    Create array of Points

    131

    -utline

    %nonymous inner class for eventhandling

    .verride mouse(ragged method

  • 8/8/2019 JavaHTP6e_11.ppt

    131/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    PaintPanel!"a#a

    (; of ;)

    .verride mouse(ragged method

    #et location of mouse cursor 

    Repaint the JFrame

    #et the  x and  y/coordinates of the

    Point

    132

    0oo and ,eel O8ser#ation 11 1&

  • 8/8/2019 JavaHTP6e_11.ppt

    132/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.1&

    Calling repaint for a ,wing GUI componentindicates that the component should erefreshed on the screen as soon as possile# he

    ac+ground of the GUI component is clearedonly if the component is opa5ue# JComponentmethod setOpa-ue can e passed a boolean argument indicating whether the component is

    opa5ue (true) or transparent ()alse)#

    133

    0oo and ,eel O8ser#ation 11 1+

  • 8/8/2019 JavaHTP6e_11.ppt

    133/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo-and-,eel O8ser#ation 11.1+

    $rawing on any GUI component is performedwith coordinates that are measured from theupper-left corner (@! @) of that GUI component!

    not the upper-left corner of the screen#

    134

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    134/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    Painter!"a#a

    (3 of ;)

    Create instance of custom drawing

     panel

    135

    -utline

  • 8/8/2019 JavaHTP6e_11.ppt

    135/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    Painter!"a#a

    (; of ;)

    136

    11.1 9e(-E#ent andlin%

  • 8/8/2019 JavaHTP6e_11.ppt

    136/174

    © 2005 Pearson Education, Inc. All rights reserved.

    *.eyListener interface  .or handling .ey*#ents

      $eclares methods keyPressed! keyReleased andkeyTyped

    137

    -utline

    . % F

  • 8/8/2019 JavaHTP6e_11.ppt

    137/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    .ey%emoFrame!"a#a

    (3 of

  • 8/8/2019 JavaHTP6e_11.ppt

    138/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    .ey%emoFrame!"a#a

    (; of

  • 8/8/2019 JavaHTP6e_11.ppt

    139/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    .ey%emoFrame!"a#a

    (< of

  • 8/8/2019 JavaHTP6e_11.ppt

    140/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    .ey%emo!"a#a

    (3 of ;)

    141

    -utline

    .ey%emo "a#a

  • 8/8/2019 JavaHTP6e_11.ppt

    141/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    .ey%emo!"a#a

    (3 of ;)

    142

    11.1 0a(out 5ana%ers

  • 8/8/2019 JavaHTP6e_11.ppt

    142/174

    © 2005 Pearson Education, Inc. All rights reserved.

    • Layout managers

      Pro%ided to arrange GUI components in a container

      Pro%ide asic layout capailities

      Implement the interface Layout+ana&er

    143

    0oo-and-,eel O8ser#ation 11 1

  • 8/8/2019 JavaHTP6e_11.ppt

    143/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo and ,eel O8ser#ation 11.1

    ost &a%a programming en%ironmentspro%ide GUI design tools that help aprogrammer graphically design a GUIA thedesign tools then write the &a%a code to create

    the GUI# ,uch tools often pro%ide greatercontrol o%er the si*e! position and alignmentof GUI components than do the uilt-in layoutmanagers#

    144

    0oo-and-,eel O8ser#ation 11 1

  • 8/8/2019 JavaHTP6e_11.ppt

    144/174

    © 2005 Pearson Education, Inc. All rights reserved.

    0oo and ,eel O8ser#ation 11.1

    It is possile to set a Container's layout to null!which indicates that no layout manager shoulde used# In a Container without a layoutmanager! the programmer must position and

    si*e the components in the gi%en container andta+e care that! on resi*e e%ents! all componentsare repositioned as necessary# / component'sresi*e e%ents can e processed y aComponentListener#

    145

    11.1.1 FlowLayout

  • 8/8/2019 JavaHTP6e_11.ppt

    145/174

    © 2005 Pearson Education, Inc. All rights reserved.

    *FlowLayout  ,implest layout manager

      Components are placed left to right in order they are

    added

      Components can e left aligned! centered or right aligned

    146

  • 8/8/2019 JavaHTP6e_11.ppt

    146/174

    © 2005 Pearson Education, Inc. All rights reserved.

    ,i%. 11."3 ; 0a(out mana%ers.

    147

    -utline

    FlowLayoutFrame

  • 8/8/2019 JavaHTP6e_11.ppt

    147/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    FlowLayoutFrame!"a#a

    (3 of

  • 8/8/2019 JavaHTP6e_11.ppt

    148/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    FlowLayoutFrame!"a#a

    (; of

  • 8/8/2019 JavaHTP6e_11.ppt

    149/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    FlowLayoutFrame!"a#a

    (< of

  • 8/8/2019 JavaHTP6e_11.ppt

    150/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    FlowLayout%emo!"a#a

    (3 of ;)

    151

    -utline

    FlowLayout%emo

  • 8/8/2019 JavaHTP6e_11.ppt

    151/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    FlowLayout%emo!"a#a

    (; of ;)

    152

    11.1.2 BorderLayout

  • 8/8/2019 JavaHTP6e_11.ppt

    152/174

    © 2005 Pearson Education, Inc. All rights reserved.

    *BorderLayout  /rranges components into fi%e regions : north! south! east!

    west and center

      Implements interface Layout+ana&er/

      Pro%ides hori*ontal gap spacing and %ertical gap spacing

    153

    0oo-and-,eel O8ser#ation 11.13

  • 8/8/2019 JavaHTP6e_11.ppt

    153/174

    © 2005 Pearson Education, Inc. All rights reserved.

    oo a d ee O8se at o 3

    8ach container can ha%e only one layout manager#,eparate containers in the same application canuse different layout managers#

    154

    0oo-and-,eel O8ser#ation 11.14

  • 8/8/2019 JavaHTP6e_11.ppt

    154/174

    © 2005 Pearson Education, Inc. All rights reserved.

    If no region is specified when adding a Component to a BorderLayout! the layout manager assumesthat the Component should e added to region

    BorderLayout!C*0T*R#

    155

    Common :ro%rammin% Error 11.

  • 8/8/2019 JavaHTP6e_11.ppt

    155/174

    © 2005 Pearson Education, Inc. All rights reserved.

    % %

    0hen more than one component is added to aregion in a BorderLayout! only the last componentadded to that region will e displayed# here is noerror that indicates this prolem#

    156

    -utline

    BorderLayout

  • 8/8/2019 JavaHTP6e_11.ppt

    156/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    Frame!"a#a

    (3 of ;)

    eclare /orderLayout instancevariable

    Create /orderLayout

    Set layout

    Register event handler 

    157

    -utline

    BorderLayout

  • 8/8/2019 JavaHTP6e_11.ppt

    157/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    Frame!"a#a

    (; of ;)

    %dd buttons to application using

    layout manager constants

    +a'e button invisible

    +a'e button visible

    ,pdate layout

    158

    -utline

    BorderLayout

  • 8/8/2019 JavaHTP6e_11.ppt

    158/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    %emo!"a#a

    (3 of ;)

    horiontal gap vertical gap

    159

    -utline

    BorderLayout

  • 8/8/2019 JavaHTP6e_11.ppt

    159/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    %emo!"a#a

    (; of ;)

    160

    11.1." GridLayout

  • 8/8/2019 JavaHTP6e_11.ppt

    160/174

    © 2005 Pearson Education, Inc. All rights reserved.

    *GridLayout  $i%ides container into a grid

      8%ery component has the same width and height

    161

    -utline

    GridLayout"

  • 8/8/2019 JavaHTP6e_11.ppt

    161/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    Frame!"a#a

    (3 of ;)

    eclare two 3ridLayout instance variables

    Create 3ridLayout

    Set layout

    162

    -utline

    GridLayoutF "

    %dd button to JFrame

  • 8/8/2019 JavaHTP6e_11.ppt

    162/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    Frame!"a#a

    (; of ;)

    %dd button to JFrame

    ,se second layout

    ,se first layout

    ,pdate layout

    163

    -utline

    GridLayout%emo"

  • 8/8/2019 JavaHTP6e_11.ppt

    163/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    !"a#a

    164

    11.13 Usin% :anels to 5ana%e 5oreComple) 0a(outs

  • 8/8/2019 JavaHTP6e_11.ppt

    164/174

    © 2005 Pearson Education, Inc. All rights reserved.

    • Comple" GUIs often re5uire multiple panels to

    arrange their components properly

    165

    -utline

    PanelFrame!"a#a

  • 8/8/2019 JavaHTP6e_11.ppt

    165/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    (3 of ;)

    eclare a JPanel to hold buttons

    Create JPanel

    Set layout

    166

    -utline

    PanelFrame!"a#a

    %dd button to panel

  • 8/8/2019 JavaHTP6e_11.ppt

    166/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    (; of ;)

    %dd panel to application

    167

    -utline

    Panel%emo!"a#a

  • 8/8/2019 JavaHTP6e_11.ppt

    167/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    168

    11.14 JTextrea

  • 8/8/2019 JavaHTP6e_11.ppt

    168/174

    © 2005 Pearson Education, Inc. All rights reserved.

    *JTextrea  Pro%ides an area for manipulating multiple lines of te"t

    *Box container  ,uclass of Container

      Uses a BoxLayout layout manager

    169

    0oo-and-,eel O8ser#ation 11.26

  • 8/8/2019 JavaHTP6e_11.ppt

    169/174

    © 2005 Pearson Education, Inc. All rights reserved.

    o pro%ide line-wrapping functionality for aJTextrea! in%o+e JTextrea method setLine1,rap with a true argument#

    170

    -utline

    TextreaFrame"a#a

  • 8/8/2019 JavaHTP6e_11.ppt

    170/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    !"a#a

    (3 of ;)eclare JText%rea instance

    variables

    Create a /ox container 

    Create te"t area and add to bo"

    171

    -utline

    TextreaFrame"a#a

    %dd button to bo"

  • 8/8/2019 JavaHTP6e_11.ppt

    171/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    !"a#a

    (; of ;)

    Copy selected te"t from one te"t

    area to the other 

    Create second te"t area and add it

    to bo"

    172

    -utline

    Textrea%emo"a#a

  • 8/8/2019 JavaHTP6e_11.ppt

    172/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    !"a#a

    (3 of ;)

    173

    -utline

    Textrea%emo!"a#a

  • 8/8/2019 JavaHTP6e_11.ppt

    173/174

    © 2005 Pearson Education,

    Inc. All rights reserved.

    !"a#a

    (; of ;)

    174

    J"crollPane Scroll8ar :olicies

    J" llP

  • 8/8/2019 JavaHTP6e_11.ppt

    174/174

    *J"crollPane has scrollar policies

      2ori*ontal policies

    • /lways (#O!4O5T%L6"COLL/%6%L,%7")

    • /s needed (#O!4O5T%L6"COLL/%6%"65++(+()

    • 1e%er (#O!4O5T%L6"COLL/%65+&+)

      ertical policies

    • /lways (&+T!C%L6"COLL/%6%L,%7")

    • /s needed (&+T!C%L6"COLL/%6%"65++(+()

    • 1e%er (&+T!C%L6"COLL/%65+&+)