Learn how to activate a Button control using Java

In this code snippet, you will learn how to activate a Button control using Java

/*<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">applet</span> <span style="color: #ff0000;">code</span> = <span style="color: #0000ff;">"Mybutton.class"</span>
<span style="color: #ff0000;">width</span> = <span style="color: #0000ff;">250</span> <span style="color: #ff0000;">height</span> = <span style="color: #0000ff;">250</span><span style="color: #0000ff;">&gt;</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">applet</span><span style="color: #0000ff;">&gt;</span> */
//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
}

Leave a Reply

Your email address will not be published.