Buttons are simple objects. Most of the time all you need to do is
add them to the layout and add an ActionListener
. The
following methods are also available:
public void addNotify()
public String getLabel()
public void setLabel(String label)
public void setActionCommand(String command)
public String getActionCommand()
public void addActionListener(ActionListener l)
public void removeActionListener(ActionListener l)
addNotify()
creates the Button's peer object, that is
the native widget that has the appearance of a Windows button or a
Mac button or a Motif button or whatever. It is extremely rare to
call this directly.
The getLabel()
and setLabel(String s)
methods allow you to retrieve and change the text of the button
while the applet is running. Given a Button b
, here is
how they might be used
String s = b.getLabel();
b.setLabel("Here's the new label");
Note that despite the suggestive name, getLabel()
returns a String
, not a Label
.
The setActionCommand()
and
getActionCommand()
methods modify the command string sent
along with the ActionEvent
. By default this is the
label of the button, but it can be changed. For instance, you could
use this as a way to pass the number of times the
BeepApplet
should beep
Finally addActionListener()
registers another
object as one which should receive ActionEvent
s fired
by the Button
. removeActionListener()
deregisters the ActionListener
object
so it will no longer receive action events fired by this
Button.