|
|
|
 |
Posted 2006-04-10, 05:31 AM
in reply to Demosthenes's post starting "To start out, you're going to need some..."
|
 |
 |
 |
Ok, so I rethought the whole thing and this is what I came up with. Anyone see any problems?
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class CalculatorSUPER extends Applet implements ActionListener, KeyListener
{
final int MAX_INPUT_LENGTH = 17;
final int INPUT_MODE = 0;
final int RESULT_MODE = 1;
final int ERROR_MODE = 2;
int displayMode;
Label displayLabel;
boolean clearOnNextDigit;
double lastNumber;
char lastOperator;
public CalculatorSUPER()
{
super();
setLayout (new BorderLayout());
Panel buttonPanel = new Panel();
Panel numberPanel = new Panel();
numberPanel.setLayout(new GridLayout(4, 3));
addButtonToPanel (numberPanel, new Button("7"), Color.blue);
addButtonToPanel (numberPanel, new Button("8"), Color.blue);
addButtonToPanel (numberPanel, new Button("9"), Color.blue);
addButtonToPanel (numberPanel, new Button("4"), Color.blue);
addButtonToPanel (numberPanel, new Button("5"), Color.blue);
addButtonToPanel (numberPanel, new Button("6"), Color.blue);
addButtonToPanel (numberPanel, new Button("1"), Color.blue);
addButtonToPanel (numberPanel, new Button("2"), Color.blue);
addButtonToPanel (numberPanel, new Button("3"), Color.blue);
addButtonToPanel (numberPanel, new Button("0"), Color.blue);
buttonPanel.add(numberPanel);
Panel controlPanel = new Panel();
controlPanel.setLayout(new GridLayout(4, 1));
addButtonToPanel (controlPanel, new Button("C"), Color.cyan);
buttonPanel.add(controlPanel);
displayLabel = new Label("");
displayLabel.setAlignment(Label.CENTER);
add(displayLabel, "North");
add(buttonPanel);
addKeyListener(this);
requestFocus();
}
void setDisplayString(String s)
{
displayLabel.setText(s);
}
String getDisplayString ()
{
return displayLabel.getText();
}
void clearLastEntry()
{
setDisplayString("");
clearOnNextDigit = true;
displayMode = INPUT_MODE;
}
void addButtonToPanel(Panel panel, Button button, Color backgroundColour)
{
panel.add(button);
button.setBackground(backgroundColour);
button.addKeyListener(this);
button.addActionListener(this);
}
public void actionPerformed (ActionEvent e)
{
processButton(e.getActionCommand());
}
void processButton(String command)
{
if (command.equals("0")) addDigit(0);
if (command.equals("1")) addDigit(1);
if (command.equals("2")) addDigit(2);
if (command.equals("3")) addDigit(3);
if (command.equals("4")) addDigit(4);
if (command.equals("5")) addDigit(5);
if (command.equals("6")) addDigit(6);
if (command.equals("7")) addDigit(7);
if (command.equals("8")) addDigit(8);
if (command.equals("9")) addDigit(9);
if (command.equals("C")) clearLastEntry();
}
void addDigit(int digit)
{
if (clearOnNextDigit)
setDisplayString("");
String inputString = getDisplayString();
if ((!inputString.equals("0") || digit > 0) && inputString.length() < MAX_INPUT_LENGTH)
{
setDisplayString(inputString + digit);
}
displayMode = INPUT_MODE;
clearOnNextDigit = false;
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
String command;
char keyChar = e.getKeyChar();
if (keyChar == KeyEvent.VK_ESCAPE)
{
command = new String("C");
}
else
{
byte bytes[] = {(byte)keyChar};
command = new String(bytes);
}
processButton(command);
}
} |
|
 |
 |
 |
|
|
|
|
|
|
|