Exam Code:



The following is the code for the program in the midterm exam 1, as you can see it here:




/*
 * @author Blanca J. Polo
 */
 
import java.awt.*;
import java.applet.*;

public class Buttons extends Applet {

  TextField tf = new TextField("       ");
  //Initialize the applet
  public void init() {
     Panel top = new Panel();
     top.setLayout(new FlowLayout());

     Panel allTheButtons = new Panel();
     allTheButtons.setLayout(new GridLayout(2,2));

     Label info = new Label("You clicked button:  ");
     top.add(info);
     top.add(tf);

     Button red = new Button("RED");
     red.setBackground(Color.red);
     Button green = new Button("GREEN");
     green.setBackground(Color.green);
     Button blue = new Button("BLUE");
     blue.setBackground(Color.blue);
     Button mine = new Button("MY COLOR");
     mine.setBackground(new Color(125,80,156));


     allTheButtons.add(red);
     allTheButtons.add(green);
     allTheButtons.add(blue);
     allTheButtons.add(mine);


    // setting the applet's layout

    BorderLayout bl = new BorderLayout();
    setLayout(bl);
    add("North", top);
    add("Center", allTheButtons);
  }


  public boolean action(Event evt, Object arg){
     String color = "";

     if(arg.equals("RED")){
       color = "RED";
     }

          if(arg.equals("GREEN")){
       color = "GREEN";
     }

     if(arg.equals("BLUE")){
       color = "BLUE";
     }

     if(arg.equals("MY COLOR")){
       color = "MY COLOR";
     }

     tf.setText(color);
    return true;
  }


}




Open questions: