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

Notes

Buttons and GUI Components Creating a Button Placing (Laying Out) the Button Reacting to the Button (Event Handling)

This Applet's Behavior

Things to Do

API work Playing With the Applet Place the following three applets on the same page:
  1. 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.
  2. 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
  3. (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