How to use Button in java applet with the use of ActionListener?

Hello Friends, 

Today I will explain how can we use Button in java applet.
In this  program I used simple Button name but you can write or put different Buttons in your java applet program.
In this program i used ActionListener event hendling class.

    import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*


*/

public class nine extends Applet implements ActionListener
{
String msg="";
Button yes,no,maybe;
public void init()
{
yes=new Button("Yes");
no=new Button("No");
maybe=new Button("MayBe");

add(yes);
add(no);
add(maybe);

yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();

if(str.equals("Yes"))
msg="You press Yes....";
else if(str.equals("No"))
msg="You press No....";
else
msg="You press Maybe....";

repaint();
}

public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *