How to Add Button in java applet?

Hello Friends, 

Today I will explain how can we use Button in java applet.
In this  program I have simply changed background color when button pushed.

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

/*


*/

public class nineA extends Applet
{
Button a=new Button("Click to change the color");

boolean flag=true;

public void init()
{
add(a);
}

public void paint(Graphics g)
{
if(flag)
setBackground(Color.blue);
else
setBackground(Color.red);
}

public boolean action(Event e,Object o)
{
if(e.target==a)
{
flag =! flag;
repaint();

return true;
}
return false;
}
}

Leave a Reply

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