|
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.*;
public class BiscuitWindow extends Frame implements ActionListener
{
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....");
private static final int MAX = 10;
private Biscuit2[] biscuits = new Biscuit2[MAX];
int lastBiscuit = -1;
int current = -1;
public BiscuitWindow()
{
addWindowListener ( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
makeDisplay();
}
private void makeDisplay()
{
setLayout(new FlowLayout());
add(display.getPanel());
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);
add.addActionListener(this);
del.addActionListener(this);
find.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
add(messages);
setSize(500,200);
setVisible(true);
}
public static void main(String[] args)
{
BiscuitWindow theApp = new BiscuitWindow();
}
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()
{
if ( lastBiscuit < (MAX-1) )
{
lastBiscuit = lastBiscuit + 1;
Biscuit2 b = display.getBiscuit();
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:
|