In this code snippet, you will learn how to activate a Button control using Java
/*<applet code = "Mybutton.class"
width = 250 height = 250></applet> */
//Compilation: javac Mybutton.java
//Execution: appletviewer Mybutton.java
import java.awt.*;
import java.awt.event.*;
public class Mybutton extends
Applet implements ActionListener
{
String msg = "":
Button byes, bno;
public void init()
{
byes = new Button("Yes");
add(byes);
byes.addActionListener(this);
bno = new Button("No");
add(bno);
bno.addActionListener(this);
}
//implementing the interface method
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
if (str.equals("Yes"))
{
msg = "You pressed Yes";
else if (str.equals("No"))
{
msg = "You pressed No";
}
}
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
//end of class
}