An Applet with Buttons
The applet's real estate is the area within the border
Source
Buttons.java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AnAppletWithButtons extends Applet implements ActionListener {
public void init() {
button1 = new Button("Button 1");
add(button1);
button1.addActionListener(this);
button2 = new Button("Button 2");
add(button2);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1)
System.out.println("Button 1 was pressed");
else
System.out.println("Button 2 was pressed");
}
Button button1, button2;
}
Description and Objective
An applet with some buttons
- Shows how to create and position interface components (in particular, buttons) in an applet
- Discusses how to handle events (react to graphical user interface (GUI) components)
Notes
Buttons and GUI Components
- An applet can contain various
graphical user interface (GUI) components
that allow a user to interact with the applet.
- Some components are for user input
- Others display information
- Some components can do both
- A button is primarily used for user input
- It can provide a limited feedback
- The button can being disabled
- A disabled button has a different appearance (grayed out)
- This does provide the user with some inmformation about the state of the applet
Creating a Button
Placing (Laying Out) the Button
- Simply creating the Button object does not cause a button to appear on the applet's surface
- The
Button
object must be explicitly added the applet
- This is achieved using the Applet's
add
method
Reacting to the Button (Event Handling)
- The button must be wired to logic that does something when the button is pressed
- Similar to a light switch-- putting it in the wall is not enogh
- Must be wired to the bulb/outlet
- The user clicking the button is called an
event
- We speak of the click as
generating the event
- In general, the user interacting with the applets interface generates events
- Reacting to the button is referred to as
handling the event
- The applet, is called the
listener
because it wishes to be notified when
an event occurs (i.e., it wishes to listen for the event)
- The listener provides a method that is invoked when the event is generated
- The method is called an
event handler
- The event handler is executed when the event occurs
- Each event has a specific event-handling method that is call when the event is generated
- The method has a specific name/signature
- It is this method that the listener must provide
- We thus speak of 'the listener executing the event-handler when the event is generated'
- In addition to providing the event-handling method, the listener (i.e., the applet) must
explicitly declare that it wishes to be a listener
- This is achieved using the
implements
keyword in the class header
This Applet's Behavior
- The Class Header
- In addition to extending
Applet
, the AnAppletWithButtons
class declares that it intends to be an ActionListener
, i.e., to listen
to button clicks.
- It does this through the
implements
keyword
- Making this declaration obligates the
AnAppletWithButtons
class
to provide the appropriate even-handling method
- The method for an
ActionListener
is addActionPerformed
(see below)
- The
init
Method
- Two buttons (named
button1
and button2
) are created in init
- The buttons are placed on the applet (using the add method)
- In the absence of anything else, adding interface elemnts to the applets places them one
after the other from left-to-right.
- When the right edge of the applet's real estate is reached, the placement continues below,
again starting at the left edge.
- The placement of the elements thus flows from right-to-left, top-to-bottom.
- The applet notifies each button that it wishes to be a listener to the button, i.e., that it wishes to be
notified when the button is clicked
- The applet sends a reference to itself (
this
) to the button's addActionListener
method
- The applet is said to
register
itself as a listener with the button.
- The
actionPerformed
Method
- This is the event-handling method for button clicks
- Having registered the applet as a listener with each button, this method
is invoked whenever either of the two buttons is clicked.
- An
ActionEvent
object is passed a a parameter to this method
- The
ActionEvent
parameter contains information pertaining to
the specific event that occurred.
- In particular, the method,
getSource
can be invoked to determine
which button generated the event (i.e., which button was clicked).
getSource
returns a reference to the interface component that
generated the event
- Knowing which component (button) generated the event allows the
listener to determine what should be done
- Using the
getSource
method to distinguish betwen the two buttons,
the actionPerformed
method simply prints a message stating which
button was clicked.
Things to Do
API work
- The
Button
class
- The
ActionListener class (actually interface
)
- The
ActionEvent class (actually interface
)
- What package is it in?
- Name a couple of other event classes whose names seem to make sense to you
Playing With the Applet
Place the following three applets on the same page:
- Modify
AppletWithButtons
so that buttons' foreground colors are set using RGB values passed in as parameters to the applet.
Each applet should have its own color capability.
- Button inherits behavior from Component which had a method that allows you to specify the foreground color.
- In the API, after the methods defined by the class you can find a list of the methods inherited from the
superclasses (i.e., parent classes) of the class.
- Modify
AppletWithButtons
so that when the first button is pressed, draw a line on the surface of the applet; the second button should cause a oval/circle to
be drawn
- (Optional) Same as #2; make the line a diagonal from upper left to lower right corners; the oval/circle should fit exactly within the applet