Java Reference
In-Depth Information
11 {
12 JFrame frame =
new
JFrame();
13 JButton button =
new
JButton(
ÑClick me!Ñ
);
14 frame.add(button);
15
16 ActionListener listener =
new
ClickListener();
17 button.addActionListener(listener);
18
19 frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
21 frame.setVisible(
true
);
22 }
23
24
private static final int
FRAME_WIDTH =
100
;
25
private static final int
FRAME_HEIGHT =
60
;
26 }
S
ELF
C
HECK
13.
Which objects are the event source and the event listener in the
ButtonViewer
program?
14.
Why is it legal to assign a
ClickListener
object to a variable of
type
ActionListener
?
410
411
C
OMMON
E
RROR
9.3: Modifying the Signature in the
Implementing Method
When you implement an interface, you must define each method exactly as it is
specified in the interface. Accidentally making small changes to the parameter or
return types is a C
OMMON
E
RROR
. Here is the classic example,
class MyListener implements ActionListener
{
public void actionPerformed()
// Oops . . . forgot ActionEvent parameter
{
. . .
}
}