Learn how to activate a Button control using Java
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.javaimport java.awt.*;import java.awt.event.*;public class Mybutton extendsApplet 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 methodpublic 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}