creating user interfaces chapter 13 csci 1302. csci 1302 – creating user interfaces2 outline...

21
Creating User Interfaces Chapter 13 CSCI 1302

Upload: martha-howard

Post on 06-Jan-2018

255 views

Category:

Documents


0 download

DESCRIPTION

CSCI 1302 – Creating User Interfaces3 Outline Labels Text Fields Text Areas Combo Boxes Lists Scroll Bars Sliders Creating Multiple Windows

TRANSCRIPT

Page 1: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

Creating User Interfaces

Chapter 13CSCI 1302

Page 2: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 2

Outline• Introduction• Common Features of Swing GUI

Components• Buttons

– Icons– Alignments– Text Positions

• Check Boxes• Radio Buttons

Page 3: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 3

Outline• Labels• Text Fields• Text Areas• Combo Boxes• Lists• Scroll Bars• Sliders• Creating Multiple Windows

Page 4: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 4

Introduction• GUI components make up the user

interfaces that you see and use• Java provides many components that

provide a wide variety of functionality

Page 5: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 5

Common Features• All but a few components are

subclasses of JComponent• They share a lot of the same features

and methods, making it easy to learn and use these items

• See Figure 13.2 for a list of common features

• See TestSwingCommonFeatures.java

Page 6: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 6

Buttons• Triggers an action event when clicked• Includes regular buttons, toggle

buttons, check box buttons, and radio buttons

• Common button features are represented in the parent class javax.swing.AbstractButton

• Regular buttons are JButtons

Page 7: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 7

Icons• Small, fixed-size pictures used to

decorate components• Use the ImageIcon class to create icons

from image files:Icon i = new ImageIcon(“photo.gif”);

• Three button icon states: default, pressed, and rollover

• See TestButtonIcons.java

Page 8: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 8

Alignments• Horizontal alignment specifies

horizontally placement on the button• Use setHorizontalAlignment(int) with

the constants LEADING, LEFT, CENTER, RIGHT, TRAILING

• Vertical alignment specifies vertical placement on the button

• Use setVerticalAlignment(int) with the constants TOP, CENTER, BOTTOM

• See Figures 13.7 and 13.8

Page 9: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 9

Text Positions• Specifies text position relative to the

icon• Use setHorizontalTextPosition(int)

with the constants LEADING, LEFT, CENTER, RIGHT, TRAILING

• Use setVerticalTextPosition(int) with the constants TOP, CENTER, BOTTOM

• See note about SwingConstants • See Figures 13.9 and 13.10 and ButtonDemo.java

Page 10: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 10

Check Boxes• Toggle button is a two-state button like

a light switch• Often used to toggle choices on or off• Many options available through

constructors, see Figure 13.14• Fires ItemEvents• Use isSelected() to query selected

state• See CheckBoxDemo.java

Page 11: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 11

Radio Buttons• Enable the user to choose a single

choice out of a group of choices• Must use a ButtonGroup to create groups

of individual radio buttons• Fires ItemEvent, then an ActionEvent • Use isSelected() to query selected

state• See RadioButtonDemo.java

Page 12: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 12

Labels• Display area for short text, images, or

both• See Figure 13.18 for constructors and

methods• Already used in TVFrame.java

Page 13: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 13

Text Fields• Can be used to enter or display a string• Like other components, has many

properties that can be set• Can be editable or not• See TextFieldDemo.java

Page 14: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 14

Text Areas• Used to enter multiple lines of text as

opposed to using multiple text fields• See Figure 13.22 for constructors and

methods• Does not scroll automatically, but can

be contained in a JScrollPane• See TextAreaDemo.java

Page 15: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 15

Combo Boxes• Known as choice or drop-down lists• A list of items from which the user can

choose• Limit’s range of choices, helps input

validation• See Figure 13.25 for constructors and

methods• Can generate ActionEvent and ItemEvent• See ComboBoxDemo.java

Page 16: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 16

Lists• Performs same functions as a combo

box, but enables the user to select both single and multiple values

• See Figure 13.27 for constructors and methods

• Three selection modes: Single, single-interval, and multiple-interval

• Do not scroll automatically• Fires ListSelectionEvent

Page 17: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 17

Scroll Bars

Bubble

Unit increment

Block decrement Block increment

Minimal value Maximal value

Unit decrement

Page 18: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 18

Scroll Bars• orientation – describes if the scroll bar

is displayed horizontally or vertically• maximum – maximum value • minimum – minimum value• visibleAmount (extent) – Relative width

of the scroll bar’s bubble• value – current value of scroll bar• blockIncrement – used in block areas• unitIncrement – used in unit areas

Page 19: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 19

Scroll Bars• When value is changed, fires an AdjustmentEvent to all registered listeners

• Objects notified of changes to the scroll bar’s value must implement the adjustmentValueChanged method in the AdjustmentListener interface

• See ScrollBarDemo.java

Page 20: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 20

Sliders• Similar to scrollbar but more versatile• See Figure 13.33 for constructors and

methods• User can graphically select a value by

sliding a knob within a bounded interval• Fires a ChangeEvent which is passed to

registered listeners who must implement stateChanged in ChangeListener interface

• See SliderBarDemo.java

Page 21: Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons

CSCI 1302 – Creating User Interfaces 21

Creating Multiple Windows• Subwindows can be opened during the

lifetime of a “main” window• Can create an instance of a new

window and make it visible• See MultipleWindowsDemo.java