TwoButton.java



/*TwoButton.java
This applet demonstrates how to use two buttons in an applet.

< applet >
code=TwoButton
width=200
height=250 >
< /applet >



*/
import java.applet.*;
import java.awt.*;

public class TwoButton extends Applet
{
	TextField num1Fld=new TextField(5);
	TextField num2Fld=new TextField(5);
	TextField num3Fld=new TextField(5);
	Button Calculate1=new Button("Calculate1");
	Button Calculate2=new Button("Calculate2");
	TextArea result1=new TextArea(2, 20);
	TextArea result2=new TextArea(2, 20);

	public void init()
	{
		add(new Label("Enter a number:"));
		add(num1Fld);
		add(new Label("Enter another:"));
		add(num2Fld);
		add(Calculate1);
		add(result1);
		add(new Label("Enter a third:"));
		add(num3Fld);
		add(Calculate2);
		add(result2);
	}

	public boolean action(Event clickedButton, Object findResult)
	{
		int num1, num2, num3;
		if (clickedButton.target==Calculate1)
		{
			num1=Integer.parseInt(num1Fld.getText());
			num2=Integer.parseInt(num2Fld.getText());
			result1.replaceText(""+num1+" + "+num2+" = "+(num1+num2)+"\n",0,0);
		}
		if (clickedButton.target==Calculate2)
		{
			num1=Integer.parseInt(num1Fld.getText());
			num2=Integer.parseInt(num2Fld.getText());
			num3=Integer.parseInt(num3Fld.getText());
			result2.replaceText(""+num1+" + "+num2+" + "+num3+" = "+(num1+num2+num3)+"\n",0,0);
		}
		return true;
	}
}

Output for TwoButton.java