How to create color changer mini-project using java applet with source code

How do I change the background color in an applet?

In this program, we will learn to create a page using an applet and on that page, there will be three buttons to change the color of the background by clicking on that button. The default color of the background will be Black and when If we click on the red button then the background color will change into red, if we click on the yellow button the background color will change into yellow and if we click on the green button then the background color will change into the green.

Program to set background and foreground color of an Applet | color changer
color changer

Source Code: Program to set background and foreground color of an Applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Color;
class ColourChange extends Applet implements ActionListener
{
Frame f;
/* public void init()
{
f.setbackground(Color.pink);
}
*/

Button b1,b2,b3;
ColourChange()
{
f=new Frame("Colour");

b1=new Button("red");
b2=new Button("yellow");
b3=new Button("green");

f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());
f.setBackground(Color.black);

f.add(b1);
f.add(b2);
f.add(b3);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(b1))
{
f.setBackground(Color.red);
}
if(e.getSource().equals(b2))
{
f.setBackground(Color.yellow);
}
if(e.getSource().equals(b3))
{
f.setBackground(Color.green);
}
}
public static void main(String...aa)
{
new ColourChange();
}
}
 

COMPLETED PROJECTS:

See my more website

Leave a Reply

%d bloggers like this: