Java Reference
In-Depth Information
Buttons
We have seen the Button and JButton class used in many of the GUI examples
of Chapter 12, “An Introduction to GUI Programming,” and also the examples
earlier in this chapter. Now, let's look at buttons in detail. The java.awt.Button
class represents an AWT button, and the javax.swing.JButton class represents
a Swing button.
AWT Buttons
The java.awt.Button class has two constructors:
public Button(). Creates a Button with no label.
public Button(String label).
Creates a Button with the specified label.
The methods in the Button class related to events include:
public void addActionListener(ActionListener a). Registers the given
listener with the button to receive the ActionEvent generated when the
button is clicked.
public void setActionCommand(String c). Sets the action command for
the button. A component that generates an ActionEvent can assign a
command to the event, which by default is the label on the button. This
method is used to assign the action command to something other than
the button's label.
The java.awt.event.ActionEvent class represents an action event. Action
events are typically generated by a user making a selection, such as
clicking a button or selecting a menu item. The methods in the
ActionEvent class include:
public String getActionCommand().
Returns the action command
of the source of the component.
public int getModifiers(). Returns an int containing information
about modifier keys that were pressed when the event occurs.
Modifier keys include the Shift, Ctrl, and Alt keys. Sometimes an
event is handled slightly differently when a modifier key is
pressed.
In addition, because ActionEvent extends java.util.EventObject, you can
also obtain a reference to the source of the event using the getSource()
method.
Search WWH ::




Custom Search