JAVA Program using Applet to Display Buttons and When Clicked on Button Show Message

JAVA Program using Applet to Display Buttons and When Clicked on Button Show Message:


/*
<applet code="ButtonDemo" width=250 height=150>
</applet>
*/


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

public class ButtonDemo extends Applet implements ActionListener
{
    String msg = "";
    Button yes, no, maybe;

    public void init()
    {
        yes = new Button("Yes");
        no = new Button("No");
        maybe = new Button("Undecided");
        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 pressed Yes.";
        }
        else if(str.equals("No"))
        {
            msg = "You pressed No.";
        }
        else
        {
            msg = "You pressed Undecided.";
        }
        repaint();
    }

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

OUTPUT:

JAVA Program using Applet to Display Buttons and When Clicked on Button Show Message


Leave a Reply

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

two + eighteen =