Return to Unit Menu | Java Main Page | | Terms of Use  

Breezy with Conditionals

//Perimeter of a Square with Breezy
//This program will show how conditional statements (ifs)
//can be used when working with buttons.
//This program has two button choices.
import java.awt.*;
import BreezyGUI.*;

public class PerimeterSquare extends GBFrame
   Label sideLabel = addLabel ("Side",1,1,1,1);
   Label perimeterLabel = addLabel ("Perimeter",2,1,1,1);

   DoubleField sideField = addDoubleField (0 ,1,2,1,1);
   DoubleField perimeterField = addDoubleField (0 ,2,2,1,1);

   Button sideButton = addButton ("Compute Side",3,1,1,1);
   Button perimeterButton = addButton ("Compute Perimeter", 3,2,1,1);

   public void buttonClicked (Button buttonObj)
      double side, perimeter;
      if (buttonObj == perimeterButton)
      {                                                            // if the perimeter button is pushed
            side = sideField.getNumber();          //get side length
            perimeter = 4 * side;                        //compute perimeter formula
            perimeterField.setNumber(perimeter); //send perimeter to box
            perimeterField.setPrecision(2);   //set number of decimals seen
      {                                                        // if the side button is pushed
             perimeter = perimeterField.getNumber();     //get perimeter size
             side=perimeter/4;                           //compute side
             sideField.setNumber(side);     //send side answer to box
             sideField.setPrecision(2);       //set number of decimals seen

   public static void main (String[ ] args)
         Frame frm = new PerimeterSquare ();
         frm.setSize (200, 150);
         frm.setVisible (true);




Return to Unit Menu | Java Main Page | | Terms of Use