Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information

Java Cup Logo

JDC Home Page


Working Applet
Help and Hints
Source Code
Table of Contents
Online Training
shadowSearchFAQFeedback

Button Help

Help is available for each task, or you can go straight to the Button source codeto see a solution.

Task 1

Create an applet called ButtonTest and add a new instance of the Button class to the applet.

import java.awt.*;

public class ButtonTest extends java.applet.Applet {
    public void init() {
        Button b = new Button();
        add(b);
    }
}

Task 2

Define the method action so that when the user presses the button, the label is sent to System.out.


Pressing a Button causes action to be called with an argument of the Button label. All you have to do is print out the label and indicate that you handled the event by returning true.

public class ButtonTest extends java.applet.Applet {
    ...
    public boolean action(Event e, Object label) {
        System.out.println("Pressed " + label);
        return true;
    }
}