예제 #1
0
  public CalculatorPanel() {

    result = 0;
    lastCommand = "+";
    ActionListener command = new CommandAction();

    GridLayout layout = new GridLayout(2, 5);
    layout.setHgap(5);
    layout.setVgap(5);

    panel = new JPanel();
    panel.setLayout(layout);
    panel.setPreferredSize(new Dimension(400, 200));

    // add display
    display = new JTextField();
    Command = new JTextField("+");
    firstNumber = new JTextField();
    secondNumber = new JTextField();
    JTextField equal = new JTextField("=");

    equal.setEnabled(false);
    display.setEnabled(false);
    Command.setEnabled(false);

    panel.add(firstNumber);
    panel.add(Command);
    panel.add(secondNumber);
    panel.add(equal);
    panel.add(display);

    addButton("+", command);
    addButton("-", command);
    addButton("*", command);
    addButton("/", command);
    addButton("OK", command);
    add(panel, BorderLayout.CENTER);
  }
  public CalculatorPanel() {
    setLayout(new BorderLayout());

    result = 0;
    lastCommand = "=";
    start = true;

    // add the display

    display = new JButton("0");
    display.setEnabled(false);
    add(display, BorderLayout.NORTH);

    ActionListener insert = new InsertAction();
    ActionListener command = new CommandAction();

    // add the buttons in a 4 x 4 grid

    panel = new JPanel();
    panel.setLayout(new GridLayout(4, 4));

    addButton("7", insert);
    addButton("8", insert);
    addButton("9", insert);
    addButton("/", command);

    addButton("4", insert);
    addButton("5", insert);
    addButton("6", insert);
    addButton("*", command);

    addButton("1", insert);
    addButton("2", insert);
    addButton("3", insert);
    addButton("-", command);

    addButton("0", insert);
    addButton(".", insert);
    addButton("=", command);
    addButton("+", command);

    add(panel, BorderLayout.CENTER);
  }