AWT Components: Buttons, Labels, TextFields
java.awt ships a handful of ready-made UI components: Button, Label,
TextField, and Checkbox among them. You construct one, add() it to
a container under whatever layout manager is active, and - for anything clickable - register a listener
to react when the user interacts with it. This lesson adds real buttons to PaintApplet for
the first time.
Constructing and Adding Components
Each component type has a constructor that takes its initial content, and is added like any other child:
Button okButton = new Button("OK");
Label nameLabel = new Label("Name:");
TextField nameField = new TextField(20); // 20 columns wide
Checkbox enabled = new Checkbox("Enabled");
add(nameLabel);
add(nameField);
add(okButton);
add(enabled);
Reacting to Clicks: ActionListener
A Button click is reported through java.awt.event.ActionListener, a
single-abstract-method interface with one method, actionPerformed(ActionEvent e). Register it
with addActionListener(). An anonymous class works, and so does a lambda - ActionListener
having exactly one abstract method makes it a valid lambda target regardless of the applet API's own
deprecated status, which only concerns the Applet/JApplet classes themselves,
not the Java language features you're free to use inside them:
okButton.addActionListener(e -> System.out.println("OK clicked"));
Extending PaintApplet: Clear and Color Buttons
Add a "Clear" button and three color buttons (Red/Green/Blue) below the drawing area. The overall applet
switches to BorderLayout, with a Panel using FlowLayout placed at
BorderLayout.SOUTH to hold the four buttons in a row - exactly the combination promised at
the end of Layout Managers. Only the changed parts of
init() are shown; the mouse/keyboard listeners and audio loading from earlier lessons stay as
they were.
public void init() {
// ...existing title/bgcolor/image/audio handling stays here...
// New: overall layout for the applet
setLayout(new BorderLayout());
// New: a button strip along the bottom edge
Panel controls = new Panel(new FlowLayout());
Button clearButton = new Button("Clear");
clearButton.addActionListener(e -> {
drawnPoints.clear();
repaint();
});
Button redButton = new Button("Red");
redButton.addActionListener(e -> { currentColor = Color.RED; repaint(); });
Button greenButton = new Button("Green");
greenButton.addActionListener(e -> { currentColor = Color.GREEN; repaint(); });
Button blueButton = new Button("Blue");
blueButton.addActionListener(e -> { currentColor = Color.BLUE; repaint(); });
controls.add(clearButton);
controls.add(redButton);
controls.add(greenButton);
controls.add(blueButton);
add(controls, BorderLayout.SOUTH);
// ...existing addMouseMotionListener / addMouseListener / addKeyListener stay here...
}
The Clear button empties drawnPoints and calls repaint(), wiping the freehand
strokes. Each color button just reassigns currentColor and calls repaint(),
exactly like the keyboard shortcuts from Handling Keyboard Events
already did - the buttons are simply a second, clickable way to trigger the same state change.
Mixing Components with Custom Drawing
Note that PaintApplet still overrides paint(Graphics g) directly to draw the
border, shapes, and freehand strokes, and that keeps working fine alongside the new BorderLayout
region and its buttons - AWT happily lets you combine real components with custom painting on a plain
Applet/Component. This is different from the JApplet/Swing
situation covered back in JApplet and Swing: Swing components
manage their own double-buffered painting, so mixing in custom drawing there means overriding
paintComponent() on a JPanel instead of overriding paint() on the
top-level JApplet directly.