*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that used button and once it clicked, the background
changed with corresponds to its button name.*/
/*Programmer: Miexie D. Tulid
*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that used button and once it clicked, the background
changed with corresponds to its button name.*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorCycle extends JFrame
implements ActionListener {
private WindowTerminator wt = new WindowTerminator(); // Code below
private JButton red,blue, green, gray; // the JButtons which will cause the background colors to change
private Container cp; // our contentpane
public ColorCycle( ) {
setSize(300,300);
setLocation(100,100);
setTitle(getClass().getName());
addWindowListener(wt);
red = new JButton("Red");
green = new JButton("Green");
blue = new JButton("Blue");
gray = new JButton("Gray");
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
gray.addActionListener(this);
cp = getContentPane();
cp.setBackground(Color.white);
cp.setLayout(new FlowLayout());
cp.add(red);
cp.add(green);
cp.add(blue);
cp.add(gray);
show();
}
public void actionPerformed(ActionEvent e) {
JButton s = (JButton) e.getSource(); //get the source of the event
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);
cp.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