Java Reference
In-Depth Information
constants
TOP
,
CENTER
, or
BOTTOM
, as shown in Figure 12.21. The default vertical text posi-
tion is
AbstractButton.CENTER
.
Text positioned top
Text centered
vertically
Text positioned bottom
F
IGURE
12.21
You can specify the vertical position of the text relative to the icon.
12.28
How do you create a button with the text OK? How do you change text on a button?
How do you set an icon, a pressed icon, and a rollover icon in a button?
12.29
Given a
JButton
object
jbtOK
, write statements to set the button's foreground to
red
, background to
yellow
, mnemonic to
K
, tool tip text to
Click OK to pro-
ceed
, horizontal alignment to
RIGHT
, vertical alignment to
BOTTOM
, horizontal text
position to
LEFT
, vertical text position to
TOP
, and icon text gap to
5
.
12.30
List at least five properties defined in the
AbstractButton
class.
✓
✓
Check
Point
Key
Point
toggle button
A
toggle button
is a two-state button like a light switch.
JToggleButton
inherits
AbstractButton
and
implements a toggle button. Often
JToggleButton
's subclasses
JCheckBox
and
JRadioButton
are used to enable the user to toggle a choice on or off. This
section introduces
JCheckBox
.
JRadioButton
will be introduced in the next section.
JCheckBox
inherits all the properties from
AbstractButton
, such as
text
,
icon
,
mnemonic
,
verticalAlignment
,
horizontalAlignment
,
horizontalTextPosition
,
verticalTextPosition
, and
selected
, and provides several constructors to create check
boxes, as shown in Figure 12.22.
javax.swing.AbstractButton
javax.swing.JToggleButton
javax.swing.JCheckBox
+JCheckBox()
+JCheckBox(text: String)
+JCheckBox(text: String, selected:
boolean)
+JCheckBox(icon: Icon)
+JCheckBox(text: String, icon: Icon)
+JCheckBox(text: String, icon: Icon,
selected: boolean)
Creates a default check box without any text or icon.
Creates a check box with text.
Creates a check box with text and specifies whether the check box is
initially selected.
Creates a check box with an icon.
Creates a check box with text and an icon.
Creates a check box with text and an icon, and specifies whether the check
box is initially selected.
F
IGURE
12.22
JCheckBox
defines a check box button.