Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

The Biscuit Window Application

SL Mastery Aspects : records, objects, arrays, simple selection, repetition, complex selection.

 

source code

 

Revised source code for Biscuit2.java

 



import java.awt.*;
import java.awt.event.*;
/**
* Example application that stores Biscuit's in an array
*
* @author Mr J
* @version 1.0
*/

public class BiscuitWindow extends Frame implements ActionListener
{
   // GUI objects
   private BiscuitDisplay display = new BiscuitDisplay();
   private Button add = new Button("add");
   private Button del = new Button("delete");
   private Button find = new Button("find");
   private Button next = new Button("next");
   private Button prev = new Button("previous");
   private Label messages = new Label("Any system messages will appear here....");
   // We'll need an array to keep track of our collection
   private static final int MAX = 10;
   private Biscuit2[] biscuits = new Biscuit2[MAX];
   // pointers into the biscuit collection
   int lastBiscuit = -1;
   int current = -1;
   /**
   * Constructor
   */

   public BiscuitWindow()
   {
     // inner class to intercept window events
     addWindowListener ( new WindowAdapter()
     {
       // we are only interested in window closing
       public void windowClosing(WindowEvent e)
       {
         // Exit the system, 0 is a normal exit code
         System.exit(0);
       }
     } );
     // Helper method to set up the display
     makeDisplay();
   }
   /**
   * Helper method to set up the display
   */

   private void makeDisplay()
   {
     // set up the Biscuit display
     setLayout(new FlowLayout());
     add(display.getPanel());
     // Make a Panel for the buttons
     Panel buttonPanel = new Panel();
     buttonPanel.setLayout(new GridLayout(5,1));
     buttonPanel.add(add);
     buttonPanel.add(del);
     buttonPanel.add(find);
     buttonPanel.add(next);
     buttonPanel.add(prev);
     add(buttonPanel);
     // set up the button listeners
     add.addActionListener(this);
     del.addActionListener(this);
     find.addActionListener(this);
     next.addActionListener(this);
     prev.addActionListener(this);
     add(messages);
     setSize(500,200);
     setVisible(true);
   }
   /**
   * main method to call the Constructor
   */

   public static void main(String[] args)
   {
     BiscuitWindow theApp = new BiscuitWindow();
   }
   /**
   * Our old buddy, actionPerformed will detect the button presses
   *
   * @param the object that caused the event to occur
   */

   public void actionPerformed(ActionEvent e)
   {
     if (e.getSource() == add)
     {
       addBiscuit();
     }
     else if (e.getSource() == del)
     {
       messages.setText("My students will try their best");
     }
     else if (e.getSource() == find)
     {
       messages.setText("My brilliant programmers will whizz through this");
     }
     else if (e.getSource() == next)
     {
       messages.setText("My serfs will try to do this");
     }
     else if (e.getSource() == prev)
     {
       messages.setText("My best and brightest will have a go at this");
     }
   }
   public void addBiscuit()
   {
     // Check if the array is full
     if ( lastBiscuit < (MAX-1) )
     {
       lastBiscuit = lastBiscuit + 1;
       Biscuit2 b = display.getBiscuit();
       // check if a valid biscuit was got from display
       if (b != null)
       {
         biscuits[lastBiscuit] = b;
         messages.setText("Biscuit added");
       }
       else
       {
         messages.setText("Could not add the biscuit, check details");
       }
     }
     else
     {
       messages.setText("No more space to add biscuits");
     }
   }
}

Back to top

Related: [ Java home | Previous: Applications | Next: simple files ]

Here we put it all together:


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009