Beruflich Dokumente
Kultur Dokumente
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
Motivations
A 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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
Objectives
Component
Container
JComponent
AbstractButton
JCheckBox
JToggleButton
JRadioButton
JLabel
JTextArea
JTextComponent
JTextField
JPasswordField
JComboBox
JList
JScrollBar
JSlider
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
AbstractButton
javax.swing.JComponent
javax.swing.AbstractButton
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-actionCommand: String
-text: String
-icon: javax.swing.Icon
The buttons 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
-rolloverIcon: javax.swing.Icon
The rollover icon (displayed when the mouse is over the button).
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.
-mnemonic: int
-horizontalTextPosition: int
-verticalAlignment: int
-verticalTextPosition: int
-borderPainted: boolean
-iconTextGap: int
-selected(): boolean
The gap between the text and the icon on the button (JDK 1.4).
-horizontalAlignment: int
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
JButton
JButton inherits AbstractButton and provides several
constructors to create buttons.
javax.swing.AbstractButton
javax.swing.JButton
+JButton()
+JButton(icon: javax.swing.Icon)
+JButton(text: String)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
JButton Constructors
The following are JButton constructors:
JButton()
JButton(String text)
JButton(String text, Icon icon)
JButton(Icon icon)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
JButton Properties
text
icon
mnemonic
horizontalAlignment
verticalAlignment
horizontalTextPosition
verticalTextPosition
iconTextGap
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
10
Demo
TestButtonIcons
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
11
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
12
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
13
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
14
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
15
ButtonDemo
Run
MessagePanel
JButton
JButton
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
16
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()
+JCheckBox(text: String)
Creates a check box with text and specifies whether the check box is
initially selected.
+JCheckBox(icon: Icon)
Creates a check box with text and an icon, and specifies whether the check
box is initially selected.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
17
CheckBoxDemo
CheckBoxDemo
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
18
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()
+JRadioButton(text: String)
Creates a radio button with text and specifies whether the radio button is
initially selected.
+JRadioButton(icon: Icon)
Creates a radio button with text and an icon, and specifies whether the radio
button is initially selected.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
19
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
20
ButtonDemo
CheckBoxDemo
RadioButtonDemo
RadioButtonDemo
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
21
JLabel
A label is a display area for a short text, an image, or both.
javax.swing.JComponent
javax.swing.JLabel
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-text: String
-icon: javax.swing.Icon
-horizontalAlignment: int
-horizontalTextPosition: int
-verticalAlignment: int
-verticalTextPosition: int
-iconTextGap: int
The gap between the text and the icon on the label (JDK 1.4).
+JLabel()
+JLabel(icon: javax.swing.Icon)
+JLabel(text: String)
Creates a label with text, an icon, and the specified horizontal alignment.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
22
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
23
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
24
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
25
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).
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.
-text: String
-editable: boolean
javax.swing.JTextField
-columns: int
-horizontalAlignment: int
+JTextField()
+JTextField(column: int)
+JTextField(text: String)
Creates a text field initialized with the specified text and columns.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
26
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
27
JTextField Properties
text
horizontalAlignment
editable
columns
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
28
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
29
ButtonDemo
CheckBoxDemo
TextFieldDemo
RadioButtonDemo
TextFieldDemo
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
30
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
-rows: int
-tabSize: int
-lineWrap: boolean
Indicates whether the line in the text area is automatically wrapped (default:
false).
-wrapStyleWord: boolean
+JTextArea()
Creates an empty text area with the specified number of rows and columns.
+JTextArea(text: String)
+JTextArea(text: String, rows: int, columns: int) Creates a new text area with the specified text and number of rows and columns.
Appends the string to text in the text area.
+append(s: String): void
+insert(s: String, pos: int): void
Replaces partial text in the range from position start to end with string s.
+getLineCount(): int
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
31
JTextArea Constructors
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
32
JTextArea Properties
text
editable
columns
lineWrap
wrapStyleWord
rows
lineCount
tabSize
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
33
JFrame
-char token
+getToken
DescriptionPanel
+setToken
+paintComponet
-jlblImage: JLabel
+mouseClicked
-jtaTextDescription: JTextArea
-char token
1
+getToken
TextAreaDemo
+setToken
+paintComponet
+mouseClicked
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
34
Example, cont.
TextAreaDemo
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
Run
35
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()
+JComboBox(items: Object[])
Creates a combo box that contains the elements in the specified array.
+getItemCount(): int
+getSelectedIndex(): int
+getSelectedItem(): Object
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
36
JComboBox Methods
To add an item to a JComboBox jcbo, use
jcbo.addItem(Object item)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
37
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
38
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
39
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()
+JList(items: Object[])
+getSelectedIndex(): int
+getSelectedIndices(): int[]
Returns an array of the values for the selected cells in increasing index order.
+getVisibleRowCount(): int
+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, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
40
JList Constructors
JList()
JList(Object[] stringItems)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
41
JList Properties
selectedIndexd
selectedIndices
selectedValue
selectedValues
selectionMode
visibleRowCount
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
42
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
43
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
-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
-blockIncrement: int
Specifies value added (subtracted) when the user activates the blockincrement (decrement) area of the scroll bar, as shown in Figure
13.30.
-unitIncrement: int
Specifies the value added (subtracted) when the user activates the unitincrement (decrement) area of the scroll bar, as shown in Figure
13.30.
+JScrollBar()
+JScrollBar(orientation: int)
44
Maximal value
Block decrement
Block increment
Bubble
Unit decrement
Unit increment
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
45
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
46
JSlider
JSlider is similar to JScrollBar, but JSlider has more
properties and can appear in many forms.
javax.swing.JComponent
javax.swing.JSlider
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-maximum: int
-minimum: int
-value: int
-orientation: int
-paintLabels: boolean
-paintTicks: boolean
-paintTrack: boolean
-majorTickSpacing: int
-minorTickSpacing: int
-inverted: boolean
True to reverse the value-range, and false to put the value range in the
normal order (default: false).
+JSlider()
Creates a horizontal slider using the specified min, max, and value.
+JSlider(orientation: int)
Creates a slider with the specified orientation, min, max, and value.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
47
SliderDemo
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
48
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
49
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
50
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
51
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
52
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
53
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
54
Example, cont.
MultipleWindowsDemo
Run
Histogram
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All ri
ghts reserved. 0132130807
55