Java Reference
In-Depth Information
j)
A dialog capable of displaying a message to the user is displayed with method
of class
.
k)
Both
JTextField
s and
JTextArea
s directly extend class
.
12.2
Determine whether each statement is
true
or
false
. If
false
, explain why.
a)
BorderLayout
is the default layout manager for a
JFrame
's content pane.
b)
When the mouse cursor is moved into the bounds of a GUI component, method
mouseOver
is called.
c)
A
JPanel
cannot be added to another
JPanel
.
d)
In a
BorderLayout
, two buttons added to the
NORTH
region will be placed side by side.
e)
A maximum of five components can be added to a
BorderLayout
.
f)
Inner classes are not allowed to access the members of the enclosing class.
g)
A
JTextArea
's text is always read-only.
h)
Class
JTextArea
is a direct subclass of class
Component
.
12.3
Find the error(s) in each of the following statements, and explain how to correct it (them):
a)
buttonName = JButton(
"Caption"
);
b)
JLabel aLabel, JLabel;
c)
txtField =
new
JTextField(
50
,
"Default Text"
);
d)
setLayout(
new
BorderLayout());
button1 =
new
JButton(
"North Star"
);
button2 =
new
JButton(
"South Pole"
);
add(button1);
add(button2);
Answers to Self-Review Exercises
12.1
a)
mouseMoved
. b) uneditable (read-only). c) layout manager. d)
Container
. e) graphical
user interface. f)
setLayout
. g)
mousePressed
,
mouseReleased
. h)
JOptionPane
. i)
showInputDi-
alog
,
JOptionPane
. j)
showMessageDialog
,
JOptionPane
. k)
JTextComponent
.
12.2
a) True.
b) False. Method
mouseEntered
is called.
c) False. A
JPanel
can be added to another
JPanel
, because
JPanel
is an indirect subclass
of
Component
. So, a
JPanel
is a
Component
. Any
Component
can be added to a
Container
.
d) False. Only the last button added will be displayed. Remember that only one compo-
nent should be added to each region in a
BorderLayout
.
e)
True. [
Note:
Panels containing multiple components can be added to each region.]
f)
False. Inner classes have access to all members of the enclosing class declaration.
g)
False.
JTextArea
s are editable by default.
h)
False.
JTextArea
derives from class
JTextComponent
.
12.3
a)
new
is needed to create an object.
b)
JLabel
is a class name and cannot be used as a variable name.
c) The arguments passed to the constructor are reversed. The
String
must be passed first.
d)
BorderLayout
has been set, and components are being added without specifying the re-
gion, so both are added to the center region. Proper
add
statements might be
add(button1,
BorderLayout.NORTH
);
add(button2,
BorderLayout.SOUTH
);
Exercises
12.4
Fill in the blanks in each of the following statements:
a)
The
JTextField
class directly extends class
.