Algorithm Steps:
Step 1: Write a html applet tag with code property set to the class name and comment it
Step 2: Import necessary packages and classes
Step 3: Define a class that extends applet and implements action listener
Step 4: Declare a string msg and three buttons, yes, no, and maybe
Step 5: In the init() method, do the following:
i) Create the buttons
ii) Add the buttons to the applet
iii) Add action listener to the buttons
Step 6: In the actionPerformed() method, do the following:
i) Get the action command of the event happened
ii) If it is equal to yes, set the msg string that the user pressed yes button
iii) Repeat i) and ii) for the other buttons with appropriate msg
iv) Repaint the applet
Step 7: In the paint() method, display the msg string
Java Program To demonstrate Awt Buttons
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ButtonApplet extends Applet implements ActionListener
{
String msg = "";
Button ok, cancel, reset;
public void init()
{
ok = new Button("ok");
cancel = new Button("cancel");
reset = new Button("reset");
add(ok);
add(cancel);
add(reset);
ok.addActionListener(this);
cancel.addActionListener(this);
reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
if(str.equals("ok"))
msg = "You pressed ok";
else if(str.equals("cancel"))
msg = "You pressed cancel";
else
msg = "You pressed reset";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,5,125); } }
//button.html
SAMPLE OUTPUT SCREEN:
|