Practical-7 Develop a program that has only one button in the frame, clicking on the button cycles through the colors: red->green->blue and so on. One color changes per click.(use getBackGround() method to get the current color)
import java.awt.*;
import java.awt.event.*;
public class Practical7 implements ActionListener {
Frame f;
Button b;
public Practical7()
{
f=new Frame("Practical-7");
b=new Button("Click");
b.setBackground(Color.red);
b.addActionListener(this);
f.setLayout(new FlowLayout());
f.add(b);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
Practical7 f1=new Practical7();
}
public void actionPerformed(ActionEvent e)
{
if(b.getBackground() == Color.RED)
{
b.setBackground(Color.GREEN);
}
else if(b.getBackground() == Color.GREEN)
{
b.setBackground(Color.BLUE);
}
else
{
b.setBackground(Color.RED);
}
}
}
Comments
Post a Comment