21
Graphical User Graphical User Interface Interface Components Components Chapter 14 Chapter 14

Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

Embed Size (px)

Citation preview

Page 1: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

Graphical User Graphical User Interface Interface ComponentsComponents

Chapter 14Chapter 14

Page 2: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

22

What You Will LearnWhat You Will Learn

Text AreasText Areas SlidersSliders MenusMenus

– With framesWith frames– Pop up menusPop up menus

Look and feel optionsLook and feel options

Page 3: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

33

JTextAreaJTextArea

Area for manipulating multiple lines Area for manipulating multiple lines of textof text

Extends Extends JTextComponentJTextComponent– Inherits from that classInherits from that class

View View Figure 14.1Figure 14.1 NoteNote

– Creation of boxCreation of box– Creation of two Creation of two JTextArea JTextArea objectsobjects– Copy of marked text to other text areaCopy of marked text to other text area

Page 4: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

44

Subclass of Subclass of JPanelJPanel

A A JPanel JPanel can can – Aggregate a set of GUI componentsAggregate a set of GUI components– Create dedicated drawing areasCreate dedicated drawing areas– Create areas that receive mouse eventsCreate areas that receive mouse events

Subclass of a Subclass of a JPanel JPanel should should – Override method Override method paintComponentpaintComponent– Call the superclass version as first Call the superclass version as first

statementstatement Note the customized JPanel subclass, Note the customized JPanel subclass,

Figure 14.2Figure 14.2

Page 5: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

55

Demonstration of Demonstration of Customized Customized JPanelJPanel

Now view Now view Figure 14.3Figure 14.3 NoteNote

– Declaration, instantiation of the Declaration, instantiation of the CustomPanelCustomPanel

– Method draw used in Method draw used in actionPerformed actionPerformed methodsmethods

Page 6: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

66

Another Customized Another Customized JPanel JPanel ClassClass

Possible for the (sub)class to handle its Possible for the (sub)class to handle its own eventsown events

Note Note SelfContainedPanelSelfContainedPanel class, Figure class, Figure 14.414.4

This class hasThis class has– Mouse activity handlersMouse activity handlers– Preferred dimension specifiedPreferred dimension specified– Call to Call to paintComponent paintComponent methodmethod

Figure 14.5 Figure 14.5 uses this customized uses this customized subclasssubclass

Page 7: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

77

JSliderJSliderss

Enable users to select from range of Enable users to select from range of integer valuesinteger values

FeaturesFeatures– Tick marks (major and minor)Tick marks (major and minor)– Snap-to ticksSnap-to ticks– Orientation (horizontal and vertical)Orientation (horizontal and vertical)

thumbtick mark

Page 8: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

88

JSliderJSliderss

JPanel JPanel subclass for drawing circles, subclass for drawing circles, Figure 14.7Figure 14.7

JSlider JSlider used to control size of a used to control size of a circle graphic, circle graphic, Figure 14.8Figure 14.8

Note Note – Slider declaration, implementationSlider declaration, implementation– Registration of listener for the sliderRegistration of listener for the slider

Page 9: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

99

Using Menus with Using Menus with FramesFrames

Allows for performing actions with Allows for performing actions with cluttering GUIcluttering GUI

Contained by menu bar – Contained by menu bar – JMenuBarJMenuBar Comprised of menu items – Comprised of menu items – MenuItemMenuItem Note Note Figure 14.9Figure 14.9

– Declares menu itemsDeclares menu items– Populates the menusPopulates the menus– Specifies actions for menu choices Specifies actions for menu choices

Page 10: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1010

JPopupMenuJPopupMenu

Menus that are context-sensitiveMenus that are context-sensitive– Right click and popup menu appearsRight click and popup menu appears

In Swing, use JPopupMenuIn Swing, use JPopupMenu– Provide options specific to componentProvide options specific to component– Popup trigger event occurs when object Popup trigger event occurs when object

right mouse clickedright mouse clicked Demonstration of JPopupMenu shown Demonstration of JPopupMenu shown

in in Figure 14.10Figure 14.10

Page 11: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1111

Pluggable Look-and-Pluggable Look-and-FeelFeel

Recall that Java is designed to run on Recall that Java is designed to run on variety of platformsvariety of platforms– GUI components on a Mac, look different GUI components on a Mac, look different

from those on a PC or on a UNIX platformfrom those on a PC or on a UNIX platform Look-and-Feel capabilityLook-and-Feel capability

– Java program on a particular platform Java program on a particular platform looks like other programs on the same looks like other programs on the same platformplatform

– Also possible to specify the look-and-feel Also possible to specify the look-and-feel of a Swing GUIof a Swing GUI

Page 12: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1212

Pluggable Look-and-Pluggable Look-and-FeelFeel

Figure 14.11 demonstrates how to Figure 14.11 demonstrates how to change the appearancechange the appearance

Note use of Note use of UIManager UIManager methodsmethods

Page 13: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1313

JDesktopPane JDesktopPane and and JInternalFrameJInternalFrame

Multiple document interfaceMultiple document interface– Main (parent) windowMain (parent) window– Child windowsChild windows

View Figure 14.12 which View Figure 14.12 which demonstrates thisdemonstrates this

NoteNote– Declaration, instantiation of Declaration, instantiation of

JDesktopPane objectJDesktopPane object– Choosing and painting of the iconsChoosing and painting of the icons

Page 14: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1414

JTabbedPaneJTabbedPane

Arranges GUI components into layersArranges GUI components into layers– One layer visible at a time One layer visible at a time – Access each layer via a tabAccess each layer via a tab

View tab demo View tab demo program, program, Figure 14.13Figure 14.13

NoteNote– declaration of declaration of

tabbed panes tabbed panes

Page 15: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1515

Layout ManagersLayout Managers

BoxLayout BoxLayout aarranges GUI rranges GUI componentscomponents– Horizontally along x-axisHorizontally along x-axis– Vertically along y-axisVertically along y-axis

Page 16: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1616

BoxLayout BoxLayout

Demonstrated in Figure 14.15Demonstrated in Figure 14.15– Also uses the tabsAlso uses the tabs– Offers several different options for Offers several different options for

laying out three laying out three JButtonJButtonss NoteNote

– Creation of boxesCreation of boxes– Adding of buttonsAdding of buttons

Page 17: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1717

GridBagLayoutGridBagLayout Layout Layout ManagerManager

Flexible Flexible GridBagLayoutGridBagLayout– Components can vary in sizeComponents can vary in size– Components can occupy multiple rows Components can occupy multiple rows

and columnsand columns– Components can be added in any orderComponents can be added in any order

Uses Uses GridBagConstraintsGridBagConstraints– Specifies how component is placed in Specifies how component is placed in GridBagLayoutGridBagLayout

Page 18: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1818

GridBagLayoutGridBagLayout Layout Layout ManagerManager

GridBagConstraints GridBagConstraints fieldsfields

Page 19: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

1919

GridBagLayoutGridBagLayout Layout Layout ManagerManager

Designing a GUI to use the Designing a GUI to use the GridBagLayoutGridBagLayout

Program for this layout demonstrated Program for this layout demonstrated in Figure 14.19in Figure 14.19

Row

Column0 1 2

0

1

2

3

Page 20: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

2020

GridBagLayoutGridBagLayout Layout Layout ManagerManager

Variation of Variation of GridBagLayoutGridBagLayout does not use does not use gridx gridx and and gridygridy– Use constants Use constants RELATIVERELATIVE and and REMAINDERREMAINDER

RELATIVERELATIVE– Specifies next-to-last component placement in Specifies next-to-last component placement in

row or columnrow or column– Component placed next to one previously addedComponent placed next to one previously added

REMAINDERREMAINDER– Specifies component as last component in row Specifies component as last component in row

or columnor column

Page 21: Graphical User Interface Components Chapter 14. 2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look

2121

GridBagLayoutGridBagLayout Layout Layout ManagerManager

Use of constraints demonstrated inUse of constraints demonstrated inFigure 14.20Figure 14.20

NoteNote– Setting constraint weightsSetting constraint weights– Setting grid widthSetting grid width– Adding buttonsAdding buttons– The The addComponent addComponent methodmethod