Saturday, March 28, 2009

Exercise 2: Change:Color

/*Programmer: Rosedil Mae Mobreros
*Date started: March 15, 2009
*Date ended: March 28, 2009
*Purpose: To create a program that used button and once it clicked, the background changed with corresponds to its button name.*/


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class ChangeColor extends JFrame
implements ActionListener
{

private WindowTerminator wt= new Terminate();
private JButton gray, red, green,blue // JButtons will cause the background colors to change
private Container rox; // this is the contentpane

public ChangeColor ( )
{
setSize(200,200);
setLocation(50,50);
setTitle(getClass().getName());


addWindowListener(winL);

gray = new JButton("Gray");
red = new JButton("Red");
green = new JButton("Green");
blue = new JButton("Blue");
gray.addActionListener(this);
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);

rox = getContentPane();
rox.setLayout(new FlowLayout());
rox.setBackground(Color.yellow);
rox.add(gray);
rox.add(red);
rox.add(green);
rox.add(blue);

show();
}


public void actionPerform(ActionEvent ae)
{

JButton j = (JButton) ae.getSource();

if ( j == gray)rox.setBackground(Color.gray);
else if ( j == green)rox.setBackground(Color.red);
else if ( j == blue)rox.setBackground(Color.green);
else if ( j == gray)rox.setBackground(Color.blue);


rox.repaint();
}


public static void main (String[] args)
{

JFrame fm = new Color();
}

}

class WindowTerminator extends WindowAdapter
{

public void windowClosing (WindowEvent eve)
{
System.exit(0);
}
}

No comments:

Post a Comment