0% found this document useful (0 votes)
174 views54 pages

Creating User Interfaces

This document discusses creating user interfaces in Java with Swing components. It covers buttons, labels, text fields, and other common GUI elements. The chapter objectives are to create graphical user interfaces using these components, create listeners for event handling, use borders, and display multiple windows. It provides details on button properties and behaviors, including constructors, icons, alignments, and event handling.

Uploaded by

MohamedFMN
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views54 pages

Creating User Interfaces

This document discusses creating user interfaces in Java with Swing components. It covers buttons, labels, text fields, and other common GUI elements. The chapter objectives are to create graphical user interfaces using these components, create listeners for event handling, use borders, and display multiple windows. It provides details on button properties and behaviors, including constructors, icons, alignments, and event handling.

Uploaded by

MohamedFMN
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 54

Chapter 15

Creating User Interfaces

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
1
Objectives
 To create graphical user interfaces with various user-interface
components: JButton, JCheckBox, JRadioButton, JLabel,
JTextField, JTextArea, JComboBox, JList, JScrollBar, and JSlider
(§15.2 – 15.12).
 To create listeners for various types of events (§15.2 – 15.12).
 To use borders to visually group user-interface components
(§15.2).
 To create image icons using the ImageIcon class (§15.3).
 To display multiple windows in an application (§15.14).

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
2
Components Covered in the Chapter
Introducesthe frequently used GUI components
Uses borders and icons

JButton
Component Container JComponent AbstractButton JCheckBox
JToggleButton
JLabel JRadioButton
JTextArea

JTextComponent
JTextField JPasswordField
JComboBox

JList

JScrollBar

JSlider

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
3
Buttons
A button is a component that triggers an action event
when clicked. Swing provides regular buttons,
toggle buttons, check box buttons, and radio buttons.
The common features of these buttons are
generalized in javax.swing.AbstractButton.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
4
AbstractButton
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.
javax.swing.AbstractButton
-actionCommand: String The action command of this button.
-text: String The button’s text (i.e., the text label on the button).
-icon: javax.swing.Icon The button’s default icon. This icon is also used as the "pressed" and
"disabled" icon if there is no explicitly set pressed icon.
-pressedIcon: javax.swing.Icon The pressed icon (displayed when the button is pressed).
-rolloverIcon: javax.swing.Icon The rollover icon (displayed when the mouse is over the button).
-mnemonic: int The mnemonic key value of this button. You can select the button by
pressing the ALT key and the mnemonic key at the same time.
-horizontalAlignment: int The horizontal alignment of the icon and text (default: CENTER).
-horizontalTextPosition: int The horizontal text position relative to the icon (default: RIGHT).
-verticalAlignment: int The vertical alignment of the icon and text (default: CENTER).
-verticalTextPosition: int The vertical text position relative to the icon (default: CENTER).
-borderPainted: boolean Indicates whether the border of the button is painted. By default, a regular
button’s border is painted, but the borders for a check box and a radio
button is not painted.
-iconTextGap: int The gap between the text and the icon on the button (JDK 1.4).
-selected(): boolean The state of the button. True if the check box or radio button is selected,
false if it's not.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
5
JButton
JButton inherits AbstractButton and provides several
constructors to create buttons.

javax.swing.AbstractButton

javax.swing.JButton
+JButton() Creates a default button with no text and icon.
+JButton(icon: javax.swing.Icon) Creates a button with an icon.
+JButton(text: String) Creates a button with text.
+JButton(text: String, icon: Icon) Creates a button with text and an icon.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
6
JButton Constructors
The following are JButton constructors:
JButton()
JButton(String text)
JButton(String text, Icon icon)
JButton(Icon icon)

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
7
JButton Properties
 text
 icon
 mnemonic
 horizontalAlignment
 verticalAlignment
 horizontalTextPosition
 verticalTextPosition
 iconTextGap
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
8
Default Icons, Pressed Icon, and
Rollover Icon
A regular button has a default icon, pressed icon,
and rollover icon. Normally, you use the default
icon. All other icons are for special effects. A
pressed icon is displayed when a button is pressed
and a rollover icon is displayed when the mouse
is over the button but not pressed.

(A) Default icon (B) Pressed icon (C) Rollover icon


Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
9
Demo

TestButtonIcons

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
10
Horizontal Alignments
Horizontal alignment specifies how the icon and text
are placed horizontally on a button. You can set the
horizontal alignment using one of the five constants:
LEADING, LEFT, CENTER, RIGHT, TRAILING.
At present, LEADING and LEFT are the same and
TRAILING and RIGHT are the same. Future
implementation may distinguish them. The default
horizontal alignment is SwingConstants.TRAILING.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
11
Vertical Alignments
Vertical alignment specifies how the icon and
text are placed vertically on a button. You can
set the vertical alignment using one of the
three constants: TOP, CENTER, BOTTOM.
The default vertical alignment is
SwingConstants.CENTER.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
12
Horizontal Text Positions
Horizontal text position specifies the
horizontal position of the text relative to the
icon. You can set the horizontal text position
using one of the five constants: LEADING,
LEFT, CENTER, RIGHT, TRAILING. The
default horizontal text position is
SwingConstants.RIGHT.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
13
Vertical Text Positions
Vertical text position specifies the vertical
position of the text relative to the icon. You
can set the vertical text position using one of
the three constants: TOP, CENTER. The
default vertical text position is
SwingConstants.CENTER.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
14
Example: Using Buttons
Write a program that displays a
message on a panel and uses ButtonDemo
two buttons, <= and =>, to
move the message on the panel
to the left or right.

MessagePanel

JButton JButton

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
15
JCheckBox
JCheckBox inherits all the properties such as text, icon,
mnemonic, verticalAlignment, horizontalAlignment,
horizontalTextPosition, verticalTextPosition, and selected
from AbstractButton, and provides several constructors to
create check boxes.
javax.swing.AbstractButton

javax.swing.JToggleButton

javax.swing.JCheckBox
+JCheckBox() Creates a default check box button with no text and icon.
+JCheckBox(text: String) Creates a check box with text.
+JCheckBox(text: String, selected: Creates a check box with text and specifies whether the check box is
boolean) initially selected.
+JCheckBox(icon: Icon) Creates a checkbox with an icon.
+JCheckBox(text: String, icon: Icon) Creates a checkbox with text and an icon.
+JCheckBox(text: String, icon: Icon, Creates a check box with text and an icon, and specifies whether the check
selected: boolean) box is initially selected.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
16
Example: Using Check Boxes
Add three check boxes named
Centered, Bold, and Italic
into Example 15.1 to let the
user specify whether the
message is centered, bold, or
italic.
ButtonDemo

CheckBoxDemo

CheckBoxDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
17
JRadioButton
Radio buttons are variations of check boxes. They are
often used in the group, where only one button is
checked at a time.
javax.swing.AbstractButton

javax.swing.JToggleButton

javax.swing.JRadioButton
+JRadioButton() Creates a default radio button with no text and icon.
+JRadioButton(text: String) Creates a radio button with text.
+JRadioButton(text: String, selected: Creates a radio button with text and specifies whether the radio button is
boolean) initially selected.
+JRadioButton(icon: Icon) Creates a radio button with an icon.
+JRadioButton(text: String, icon: Icon) Creates a radio button with text and an icon.
+JRadioButton(text: String, icon: Icon, Creates a radio button with text and an icon, and specifies whether the radio
selected: boolean) button is initially selected.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
18
Grouping Radio Buttons

ButtonGroup btg = new ButtonGroup();


btg.add(jrb1);
btg.add(jrb2);

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
19
Example: Using Radio Buttons
Add three radio buttons
named Red, Green, and
Blue into the preceding
example to let the user
choose the color of the
message. ButtonDemo

CheckBoxDemo

RadioButtonDemo RadioButtonDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
20
JLabel
A label is a display area for a short text, an image, or both.
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.
javax.swing.JLabel
-text: String The label’s text.
-icon: javax.swing.Icon The label’s image icon.
-horizontalAlignment: int The horizontal alignment of the text and icon on the label.
-horizontalTextPosition: int The horizontal text position relative to the icon on the label.
-verticalAlignment: int The vertical alignment of the text and icon on the label.
-verticalTextPosition: int The vertical text position relative to the icon on the label.
-iconTextGap: int The gap between the text and the icon on the label (JDK 1.4).
+JLabel() Creates a default label with no text and icon.
+JLabel(icon: javax.swing.Icon) Creates a label with an icon.
+JLabel(icon: Icon, hAlignment: int) Creates a label with an icon and the specified horizontal alignment.
+JLabel(text: String) Creates a label with text.
+JLabel(text: String, icon: Icon, Creates a label with text, an icon, and the specified horizontal alignment.
hAlignment: int)
+JLabel(text: String, hAlignment: int) Creates a label with text and the specified horizontal alignment.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
21
JLabel Constructors
The constructors for labels are as follows:
JLabel()

JLabel(String text, int horizontalAlignment)

JLabel(String text)

JLabel(Icon icon)

JLabel(Icon icon, int horizontalAlignment)

JLabel(String text, Icon icon, int


horizontalAlignment)

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
22
JLabel Properties
JLabel inherits all the properties from
JComponent and has many properties
similar to the ones in JButton, such as
text, icon, horizontalAlignment,
verticalAlignment,
horizontalTextPosition,
verticalTextPosition, and iconTextGap.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
23
Using Labels
// Create an image icon from image file
ImageIcon icon = new ImageIcon("image/grapes.gif");
 
// Create a label with text, an icon,
// with centered horizontal alignment
JLabel jlbl = new JLabel("Grapes", icon,
SwingConstants.CENTER);
 
// Set label's text alignment and gap between text and icon
jlbl.setHorizontalTextPosition(SwingConstants.CENTER)
;
jlbl.setVerticalTextPosition(SwingConstants.BOTTOM);
jlbl.setIconTextGap(5);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
24
JTextField
A text field is an input area where the user can type in
characters. Text fields are useful in that they enable the user to
enter in variable data (such as a name or a description).

The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
javax.swing.text.JTextComponent
-text: String The text contained in this text component.
-editable: boolean Indicates whether this text component is editable (default: true).

javax.swing.JTextField
-columns: int The number of columns in this text field.
-horizontalAlignment: int The horizontal alignment of this text field (default: LEFT).
+JTextField() Creates a default empty text field with number of columns set to 0.
+JTextField(column: int) Creates an empty text field with specified number of columns.
+JTextField(text: String) Creates a text field initialized with the specified text.
+JTextField(text: String, columns: int) Creates a text field initialized with the specified text and columns.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
25
JTextField Constructors
 JTextField(int columns)
Creates an empty text field with the specified
number of columns.
 JTextField(String text)
Creates a text field initialized with the specified text.
 JTextField(String text, int columns)
Creates a text field initialized with the
specified text and the column size.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
26
JTextField Properties
 text
 horizontalAlignment
 editable
 columns

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
27
JTextField Methods
 getText()
Returns the string from the text field.
 setText(String text)
Puts the given string in the text field.
 setEditable(boolean editable)
Enables or disables the text field to be edited. By default,
editable is true.
 setColumns(int)
Sets the number of columns in this text field.
The length of the text field is changeable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
28
Example: Using Text Fields
Add a text field to the
preceding example to
let the user set a new
message.

JFrame ButtonDemo CheckBoxDemo RadioButtonDemo TextFieldDemo

TextFieldDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
29
JTextArea
If 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.text.JTextComponent The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
javax.swing.JTextArea
-columns: int The number of columns in this text area.
-rows: int The number of rows in this text area.
-tabSize: int The number of characters used to expand tabs (default: 8).
-lineWrap: boolean Indicates whether the line in the text area is automatically wrapped (default:
false).
-wrapStyleWord: boolean Indicates whether the line is wrapped on words or characters (default: false).
+JTextArea() Creates a default empty text area.
+JTextArea(rows: int, columns: int) Creates an empty text area with the specified number of rows and columns.
+JTextArea(text: String) Creates a new text area with the specified text displayed.
+JTextArea(text: String, rows: int, columns: int) Creates a new text area with the specified text and number of rows and columns.
+append(s: String): void Appends the string to text in the text area.
+insert(s: String, pos: int): void Inserts string s in the specified position in the text area.
+replaceRange(s: String, start: int, end: int): Replaces partial text in the range from position start to end with string s.
void
+getLineCount(): int Returns the actual number of lines contained in the text area.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
30
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 and
the number of rows and columns specified.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
31
JTextArea Properties
 text
 editable
 columns
 lineWrap
 wrapStyleWord
 rows
 lineCount
 tabSize
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
32
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.

JPanel JFrame
-char token -char token

+getToken 1 1 +getToken
DescriptionPanel TextAreaDemo
+setToken +setToken
+paintComponet +paintComponet
-jlblImage: JLabel
+mouseClicked +mouseClicked
-jtaTextDescription: JTextArea

+setImageIcon(icon: ImageIcon): void


+setTitle(title: String): void
+setTextDescription(text: String): void
+getMinimumSize(): Dimension

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
33
Example, cont.

TextAreaDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
34
JComboBox
A 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.JComponent

javax.swing.JComboBox
+JComboBox() Creates a default empty combo box.
+JComboBox(items: Object[]) Creates a combo box that contains the elements in the specified array.
+addItem(item: Object): void Adds an item to the combo box.
+getItemAt(index: int): Object Returns the item at the specified index.
+getItemCount(): int Returns the number of items in the combo box.
+getSelectedIndex(): int Returns the index of the selected item.
+setSelectedIndex(index: int): void Sets the selected index in the combo box.
+getSelectedItem(): Object Returns the selected item.
+setSelectedItem(item: Object): void Sets the selected item in the combo box.
+removeItem(anObject: Object): void Removes an item from the item list.
+removeItemAt(anIndex: int): void Removes the item at the specified index in the combo box.
+removeAllItems(): void Removes all items in the combo box.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
35
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()

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
36
Using the
itemStateChanged Handler
When a choice is checked or unchecked,
itemStateChanged() for ItemEvent is
invoked as well as the actionPerformed()
handler for ActionEvent.
public void itemStateChanged(ItemEvent e) {
// Make sure the source is a combo box
if (e.getSource() instanceof JComboBox)
String s = (String)e.getItem();
}

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
37
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.
ComboBoxDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
38
JList
A 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.JComponent

javax.swing.JList
+JList() Creates a default empty list.
+JList(items: Object[]) Creates a list that contains the elements in the specified array.
+getSelectedIndex(): int Returns the index of the first selected item.
+setSelectedIndex(index: int): void Selects the cell at the specified index.
+getSelectedIndices(): int[] Returns an array of all of the selected indices in increasing order.
+setSelectedIndices(indices: int[]): void Selects the cells at the specified indices.
+getSelectedValue(): Object Returns the first selected item in the list.
+getSelectedValues(): Object[] Returns an array of the values for the selected cells in increasing index order.
+getVisibleRowCount(): int Returns the number of visible rows displayed without a scrollbar. (default: 8)
+setVisibleRowCount(count: int): void Sets the preferred number of visible rows displayed without a scrollbar.
+getSelectionBackground(): Color Returns the background color of the selected cells.
+setSelectionBackground(c: Color): void Sets the background color of the selected cells.
+getSelectionForeground(): Color Returns the foreground color of the selected cells.
+setSelectionForeground(c: Color): void Sets the foreground color of the selected cells.
+getSelectionMode(): int Returns the selection mode for the list.
+setSelectionMode(selectionMode: int): Sets the selection mode for the list.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
39
JList Constructors
 JList()

Creates an empty list.

 JList(Object[] stringItems)
Creates a new list initialized with items.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
40
JList Properties
 selectedIndexd
 selectedIndices
 selectedValue
 selectedValues
 selectionMode
 visibleRowCount
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
41
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.

ListDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
42
JScrollBar
A 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.JComponent
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
javax.swing.JScrollBar
-orientation: int Specifies horizontal or vertical style, default is horizontal.
-maximum: int 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.
-minimum: int 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.
-visibleAmount: int 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.
-value: int Represents the current value of the scroll bar.
-blockIncrement: int Specifies value added (subtracted) when the user activates the block-
increment (decrement) area of the scroll bar, as shown in Figure
13.30.
-unitIncrement: int Specifies the value added (subtracted) when the user activates the unit-
increment (decrement) area of the scroll bar, as shown in Figure
13.30.

+JScrollBar() Creates a default vertical scroll bar.


+JScrollBar(orientation: int) Creates a scroll bar with the specified orientation.
+JScrollBar(orientation: int, value: Creates a scrollbar with the specified orientation, value, extent,
int, extent: int, min: int, max: int) minimum, and maximum.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
43
Scroll Bar Properties
Minimal value Maximal value

Block decrement Block increment

Bubble
Unit decrement Unit increment

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
44
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.
ScrollBarDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
45
JSlider
JSlider is similar to JScrollBar, but JSlider has more
properties and can appear in many forms.
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.
javax.swing.JSlider
-maximum: int The maximum value represented by the slider (default: 100).
-minimum: int The minimum value represented by the slider (default: 0).
-value: int The current value represented by the slider.
-orientation: int The orientation of the slider (default: JSlider.HORIZONTAL).
-paintLabels: boolean True if the labels are painted at tick marks (default: false).
-paintTicks: boolean True if the ticks are painted on the slider (default: false).
-paintTrack: boolean True if the track is painted on the slider (default: true).
-majorTickSpacing: int The number of units between major ticks (default: 0).
-minorTickSpacing: int The number of units between minor ticks (default: 0).
-inverted: boolean True to reverse the value-range, and false to put the value range in the
normal order (default: false).
+JSlider() Creates a default horizontal slider.
+JSlider(min: int, max: int) Creates a horizontal slider using the specified min and max.
+JSlider(min: int, max: int, value: int) Creates a horizontal slider using the specified min, max, and value.
+JSlider(orientation: int) Creates a slider with the specified orientation.
+JSlider(orientation: int, min: int, max: Creates a slider with the specified orientation, min, max, and value.
int, value: int)

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
46
Example: Using Sliders
Rewrite the preceding
program using the sliders
to control a message
displayed on a panel
instead of using scroll
bars.

SliderDemo

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
47
Creating Multiple Windows
The following slides show step-by-step how to
create an additional window from an application
or applet.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
48
Creating Additional Windows, Step 1

Step 1: Create a subclass of JFrame (called a


SubFrame) that tells the new window what
to do. For example, all the GUI application
programs extend JFrame and are subclasses
of JFrame.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
49
Creating Additional Windows, Step 2

Step 2: Create an instance of SubFrame in the


application or applet.

Example:
SubFrame subFrame = new
SubFrame("SubFrame Title");

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
50
Creating Additional Windows, Step 3

Step 3: Create a JButton for activating the


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

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
51
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);
}
}
}

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
52
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.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
53
Example, cont.

MultipleWindowsDemo

Histogram

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
54

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy