Saturday, March 21, 2009

Exercise2-ColorCycle

/* Programmer: Norfe R. Gregorio* Program name: Color Cycle* Subject: IT134_Computer Programming 3* Instructor: Mr. Dony Dongiapon* Date Started: 03/14/09 * Date Finished: 03/19/09* Purpose:A program that will change the color of the background in Frame when you click the botton*/
import java.*;
public class ColorCycle extends JFrame implements ActionListener {
private WindowTerminator wincolor = new WindowTerminator(); private JButton red, Green, Blue, Gray, Violet; private Container color;
public ColorCycle( ) { setSize(200,200); setLocation(100,100); setTitle(getClass().getName());
addWindowListener(wincolor);
red = new JButton("Red"); green = new JButton("Green"); blue = new JButton("Blue"); gray = new JButton("Gray"); Violet = new JButton("Violet"); red.addActionListener(this); green.addActionListener(this); blue.addActionListener(this); gray.addActionListener(this); Violet.addActionListener(this); color = getContentPane(); color.setBackground(Color.white); color.setLayout(new FlowLayout()); color.add(red); color.add(Green); color.add(Blue); color.add(Gray); color.add(Violet); show(); }
public void actionPerformed(ActionEvent e) {
JButton s = (JButton) e.getSource();
if ( s == Red) cp.setBackground(Color.Red); else if ( s == Green) cp.setBackground ( Color.Green); else if ( s == Blue) cp.setBackground ( Color.Blue); else if ( s == Gray) cp.setBackground ( Color.Gray); else if ( s == Violet) cp.setBackground ( Color.Violet);

color.repaint(); }
public static void main (String[] args) {
JFrame f = new ColorCycle(); }
}
class WindowTerminator extends WindowAdapter {
public void windowClosing (WindowEvent e) { System.exit(0); }}

No comments:

Post a Comment