Learn how to activate a Button control using Java


0

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
}

Like it? Share with your friends!

0

What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win
Anand Narayanaswamy
Anand Narayanaswamy is the editor-in-chief of Learnxpress. He was a Microsoft Most Valuable Professional (MVP) for a period of 9 years. He is a ASPInsider based in Trivandrum, Kerala State, India. Anand is the author of Community Server Quickly published by Packt Publishing.

0 Comments

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