29
1 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pears on Education, Inc. All rights reserved. Chapter 17 Creating User Interfaces

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

Embed Size (px)

Citation preview

Page 1: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

1Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 17Creating User Interfaces

Page 2: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

2Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

MotivationsA graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity and knowledge of how GUI components work. Since the GUI components in Java are very flexible and versatile, you can create a wide assortment of useful user interfaces.

Previous chapters briefly introduced several GUI components. This chapter introduces the frequently used GUI components in detail.

Page 3: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

3Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Objectives To create graphical user interfaces with various user-

interface components (§§17.2–17.8). To create listeners for JCheckBox, JRadioButton, and JTextField (§17.2).

To enter multiple-line texts using JTextArea (§17.3). To select a single item using JComboBox (§17.4). To select a single or multiple items using JList (§17.5). To select a range of values using JScrollBar (§17.6). To select a range of values using JSlider and explore

differences between JScrollBar and JSlider (§17.7). To display multiple windows in an application (§17.8).

Page 4: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

4Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Events for JCheckBox, JRadioButton, and JTextField

RunRun

GUIEventDemoGUIEventDemo

Page 5: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

5Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JTextAreaIf you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text.

javax.swing.JTextArea -columns: int

-rows: int

-tabSize: int

-lineWrap: boolean

-wrapStyleWord: boolean

+JTextArea()

+JTextArea(rows: int, columns: int)

+JTextArea(text: String)

+JTextArea(text: String, rows: int, columns: int)

+append(s: String): void

+insert(s: String, pos: int): void

+replaceRange(s: String, start: int, end: int): void

+getLineCount(): int

The number of columns in this text area.

The number of rows in this text area.

The number of characters used to expand tabs (default: 8).

Indicates whether the line in the text area is automatically wrapped (default: false).

Indicates whether the line is wrapped on words or characters (default: false).

Creates a default empty text area.

Creates an empty text area with the specified number of rows and columns.

Creates a new text area with the specified text displayed.

Creates a new text area with the specified text and number of rows and columns.

Appends the string to text in the text area.

Inserts string s in the specified position in the text area.

Replaces partial text in the range from position start to end with string s.

Returns the actual number of lines contained in the text area.

javax.swing.text.JTextComponent

The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.

Page 6: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

6Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JTextArea Constructors JTextArea(int rows, int columns)

Creates a text area with the specified number of rows and columns.

JTextArea(String s, int rows, int columns)

Creates a text area with the initial text andthe number of rows and columns specified.

Page 7: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

7Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JTextArea PropertiestexteditablecolumnslineWrapwrapStyleWordrowslineCounttabSize

Page 8: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

8Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example: Using Text Areas This example gives a program that displays

an image in a label, a title in a label, and a text in a text area.

DescriptionPanel -jlblImageTitle: JLabel -jtaTextDescription: JTextArea +setImageIcon(icon: ImageIcon): void +setTitle(title: String): void +setTextDescription(text: String): void +getMinimumSize(): Dimension

1 TextAreaDemo

javax.swing.JPanel -char token +getToken +setToken +paintComponet +mouseClicked

javax.swing.JFrame -char token +getToken +setToken +paintComponet +mouseClicked

1

Page 9: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

9Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example, cont.

RunRunTextAreaDemoTextAreaDemoDiscriptionPanelDiscriptionPanel

Page 10: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

10Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JComboBoxA combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value.

javax.swing.JComboBox

+JComboBox()

+JComboBox(items: Object[])

+addItem(item: Object): void

+getItemAt(index: int): Object

+getItemCount(): int

+getSelectedIndex(): int

+setSelectedIndex(index: int): void

+getSelectedItem(): Object

+setSelectedItem(item: Object): void

+removeItem(anObject: Object): void

+removeItemAt(anIndex: int): void

+removeAllItems(): void

Creates a default empty combo box.

Creates a combo box that contains the elements in the specified array.

Adds an item to the combo box.

Returns the item at the specified index.

Returns the number of items in the combo box.

Returns the index of the selected item.

Sets the selected index in the combo box.

Returns the selected item.

Sets the selected item in the combo box.

Removes an item from the item list.

Removes the item at the specified index in the combo box.

Removes all items in the combo box.

javax.swing.JComponent

Page 11: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

11Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JComboBox Methods

To add an item to a JComboBox jcbo, use

jcbo.addItem(Object item)

To get an item from JComboBox jcbo, use

jcbo.getItem()

Page 12: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

12Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Using theitemStateChanged Handler

public void itemStateChanged(ItemEvent e) { // Make sure the source is a combo box if (e.getSource() instanceof JComboBox) String s = (String)e.getItem();}

When a choice is checked or unchecked, itemStateChanged() for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent.

Page 13: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

13Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example: Using Combo Boxes

This example lets users view an image and a description of a country's flag by selecting the country from a combo box.

RunRunComboBoxDemoComboBoxDemo

Page 14: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

14Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JListA list is a component that performs basically the same function as a combo box, but it enables the user to choose a single value or multiple values.

javax.swing.JList

+JList()

+JList(items: Object[])

+getSelectedIndex(): int

+setSelectedIndex(index: int): void

+getSelectedIndices(): int[]

+setSelectedIndices(indices: int[]): void

+getSelectedValue(): Object

+getSelectedValues(): Object[]

+getVisibleRowCount(): int

+setVisibleRowCount(count: int): void

+getSelectionBackground(): Color

+setSelectionBackground(c: Color): void

+getSelectionForeground(): Color

+setSelectionForeground(c: Color): void

+getSelectionMode(): int

+setSelectionMode(selectionMode: int):

Creates a default empty list.

Creates a list that contains the elements in the specified array.

Returns the index of the first selected item.

Selects the cell at the specified index.

Returns an array of all of the selected indices in increasing order.

Selects the cells at the specified indices.

Returns the first selected item in the list.

Returns an array of the values for the selected cells in increasing index order.

Returns the number of visible rows displayed without a scrollbar. (default: 8)

Sets the preferred number of visible rows displayed without a scrollbar.

Returns the background color of the selected cells.

Sets the background color of the selected cells.

Returns the foreground color of the selected cells.

Sets the foreground color of the selected cells.

Returns the selection mode for the list.

Sets the selection mode for the list.

javax.swing.JComponent

Page 15: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

15Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JList Constructors JList()

Creates an empty list.

JList(Object[] stringItems)

Creates a new list initialized with items.

Page 16: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

16Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JList Properties

selectedIndexd

selectedIndices

selectedValue

selectedValues

selectionMode

visibleRowCount

Page 17: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

17Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example: Using Lists This example gives a program that lets users select countries in a list and display the flags of the selected countries in the labels.

RunRunListDemoListDemo

Page 18: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

18Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JScrollBarA scroll bar is a control that enables the user to select from a range of values. The scrollbar appears in two styles: horizontal and vertical.

javax.swing.JScrollBar -orientation: int

-maximum: int

-minimum: int

-visibleAmount: int

-value: int

-blockIncrement: int

-unitIncrement: int

+JScrollBar()

+JScrollBar(orientation: int)

+JScrollBar(orientation: int, value: int, extent: int, min: int, max: int)

Specifies horizontal or vertical style, default is horizontal.

Specifies the maximum value the scroll bar represents when the bubble reaches the right end of the scroll bar for horizontal style or the bottom of the scroll bar for vertical style.

Specifies the minimum value the scroll bar represents when the bubble reaches the left end of the scroll bar for horizontal style or the top of the scroll bar for vertical style.

Specifies the relative width of the scroll bar's bubble. The actual width appearing on the screen is determined by the maximum value and the value of visibleAmount.

Represents the current value of the scroll bar.

Specifies value added (subtracted) when the user activates the block-increment (decrement) area of the scroll bar, as shown in Figure 13.30.

Specifies the value added (subtracted) when the user activates the unit-increment (decrement) area of the scroll bar, as shown in Figure 13.30.

Creates a default vertical scroll bar.

Creates a scroll bar with the specified orientation.

Creates a scrollbar with the specified orientation, value, extent, minimum, and maximum.

javax.swing.JComponent

The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.

Page 19: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

19Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Scroll Bar Properties

Bubble

Unit increment

Block decrement Block increment

Minimal value Maximal value

Unit decrement

Page 20: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

20Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example: Using Scrollbars

This example uses horizontal and vertical scrollbars to control a message displayed on a panel. The horizontal scrollbar is used to move the message to the left or the right, and the vertical scrollbar to move it up and down.

ScrollBarDemoScrollBarDemo RunRun

Page 21: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

21Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JSliderJSlider is similar to JScrollBar, but JSlider has more properties and can appear in many forms.

javax.swing.JSlider -maximum: int

-minimum: int

-value: int

-orientation: int

-paintLabels: boolean

-paintTicks: boolean

-paintTrack: boolean

-majorTickSpacing: int

-minorTickSpacing: int

-inverted: boolean

+JSlider()

+JSlider(min: int, max: int)

+JSlider(min: int, max: int, value: int)

+JSlider(orientation: int)

+JSlider(orientation: int, min: int, max: int, value: int)

The maximum value represented by the slider (default: 100).

The minimum value represented by the slider (default: 0).

The current value represented by the slider.

The orientation of the slider (default: JSlider.HORIZONTAL).

True if the labels are painted at tick marks (default: false).

True if the ticks are painted on the slider (default: false).

True if the track is painted on the slider (default: true).

The number of units between major ticks (default: 0).

The number of units between minor ticks (default: 0).

True to reverse the value-range, and false to put the value range in the normal order (default: false).

Creates a default horizontal slider.

Creates a horizontal slider using the specified min and max.

Creates a horizontal slider using the specified min, max, and value.

Creates a slider with the specified orientation.

Creates a slider with the specified orientation, min, max, and value.

javax.swing.JComponent

The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.

Page 22: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

22Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example: Using Sliders

Rewrite the preceding program using the sliders to control a message displayed on a panel instead of using scroll bars.

SliderDemoSliderDemo RunRun

Page 23: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

23Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Creating Multiple Windows

The following slides show step-by-step how to create an additional window from an application or applet.

Page 24: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

24Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Step 1: Create a subclass of JFrame (called a SubFrame) that tells the new window whatto do. For example, all the GUI application programs extend JFrame and are subclassesof JFrame.

Creating Additional Windows, Step 1

Page 25: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

25Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Creating Additional Windows, Step 2

Step 2: Create an instance of SubFrame in the application or applet.

Example:

SubFrame subFrame = new

SubFrame("SubFrame Title");

Page 26: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

26Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Creating Additional Windows, Step 3

Step 3: Create a JButton for activating the subFrame.

add(new JButton("Activate SubFrame"));

Page 27: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

27Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Creating Additional Windows, Step 4

Step 4: Override the actionPerformed()method as follows:public actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (e.target instanceof Button) { if ("Activate SubFrame".equals(actionCommand)) { subFrame.setVisible(true); } }}

Page 28: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

28Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example: Creating Multiple Windows

This example creates a main window with a text area in the scroll pane, and a button named "Show Histogram." When the user clicks the button, a new window appears that displays a histogram to show the occurrence of the letters in the text area.

Page 29: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces

29Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Example, cont.

RunRunMultipleWindowsDemoMultipleWindowsDemo

HistogramHistogram