Tuesday, March 17, 2009

Exercise no.2 (COLOR CYCLE)

/*
ProgrammerName: Dandy A. Macaubos
ProgramName : CoLor CycLe 
Purpose : to make a program that will cycle through four different colors.
Date : March 17,2009
Instructor : Dony Dongiapon
*/


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonDemo extends JPanel implements ActionListener {

 private static boolean USE_CROSS_PLATFORM_UI = false;
 
 int buttonLabelIndex = 0;
 String buttonLabels[] = { "Green", "blue", "Gray", "Red" };
 Color buttonColors[] = { Color.GREEN, Color.BLUE, Color.GRAY, Color.RED,};
 
 JButton button;
 
 public ButtonDemo() {
  super(new BorderLayout());
  
  button = new JButton(buttonLabels[buttonLabelIndex]);
  // In the default UI look and feel you cannot easily alter the background color
  // for buttons since it is designed to match the OS X UI.
  if(USE_CROSS_PLATFORM_UI) {
  button.setBackground(buttonColors[buttonLabelIndex]);
  } else {
  button.setForeground(buttonColors[buttonLabelIndex]);
  }
  
  button.addActionListener(this);
 
  
  this.add(button, BorderLayout.CENTER);
  
  this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
 }
 
 public void actionPerformed(ActionEvent e) {
  buttonLabelIndex = ++buttonLabelIndex < frame =" new" contentpane =" new">}

Exercise no.3 (Combination Lock)

/*
ProgrammerName : Dandy A. Macaubos
ProgramName       : Combination Lock 
Purpose                 : To make a program that will unlocked a combination
Date                        : March 17,2009
Instructor               : Dony Dongiapon
*/

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

public class CombinationLock extends JFrame {

public static void main (String [] args) {
  new CombinationLock().setVisible(true);
  }

public CombinationLock () {

  Container cp = getContentPane();
  cp.setLayout(new FlowLayout());

   
  cp.add(new JButton("a"));
  cp.add(new JButton("b"));
  cp.add(new JButton("c"));
  cp.add(new JButton("d"));
  cp.add(new JButton("e"));
  cp.add(new JButton("f"));
  cp.add(new JButton("g"));
  cp.add(new JButton("h"));
  cp.add(new JButton("i"));
  cp.add(new JButton("j"));
  cp.setBackground(Color.white);
  pack();
   

  }
}

No comments:

Post a Comment