예제 #1
0
  /**
   * An interface action has been performed. Find out what it was and handle it.
   *
   * @param event The event that has occured.
   */
  public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();

    if (command.equals("0")
        || command.equals("1")
        || command.equals("2")
        || command.equals("3")
        || command.equals("4")
        || command.equals("5")
        || command.equals("6")
        || command.equals("7")
        || command.equals("8")
        || command.equals("9")) {
      int number = Integer.parseInt(command);
      calc.numberPressed(number);
    } else if (command.equals("A")
        || command.equals("B")
        || command.equals("C")
        || command.equals("D")
        || command.equals("E")
        || command.equals("F")) {
      int number = getNumberFromHexString(command);
      calc.numberPressed(number);
    } else if (command.equals("+")) {
      calc.plus();
    } else if (command.equals("-")) {
      calc.minus();
    } else if (command.equals("=")) {
      calc.equals();
    } else if (command.equals("C")) {
      calc.clear();
    } else if (command.equals("?")) {
      showInfo();
    }
    // Exercise 1 multi
    else if (command.equals("*")) {
      calc.multiplication();
    }
    // Exercise 1 division
    else if (command.equals("/")) {
      calc.division();
    } else if (command.equals("Cl")) {
      // had to change this, so it works in hexadecimal mode
      calc.clear();
    } else if (command.equals("Hexadecimal mode")) {
      calc.flipHexSwitch(); // flip the switch
    }

    // else unknown command.

    redisplay();
  }
예제 #2
0
  /** Update the interface display to show the current value of the calculator. */
  private void redisplay() {
    if (calc.isHexOn()) {
      int displayInt = calc.getDisplayValue();
      if (displayInt < 0) {
        // toHexString() returns an unsigned integer as a String,
        // meaning negative values return (String)(ffffffff - n) instead of (-n)
        // example: -9 would show ffffffff - 9 = fffffff4
        display.setText("-" + Integer.toHexString(Math.abs(displayInt)));
      } else {
        display.setText(Integer.toHexString(displayInt));
      }

      // show all hexButtons since hexSwitch is true
      hexPanel.setVisible(true);
    } else {
      display.setText("" + calc.getDisplayValue());
      // hide all hexButtons since hexSwitch is false
      hexPanel.setVisible(false);
    }
  }
예제 #3
0
  /** Make the frame for the user interface. */
  private void makeFrame() {
    frame = new JFrame(calc.getTitle());

    JPanel contentPane = (JPanel) frame.getContentPane();
    contentPane.setLayout(new BorderLayout(8, 8));
    contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));

    display = new JTextField();
    contentPane.add(display, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel(new GridLayout(5, 4));
    addButton(buttonPanel, "7");
    addButton(buttonPanel, "8");
    addButton(buttonPanel, "9");
    addButton(buttonPanel, "Cl");
    // had to change this, so it works in hexadecimal mode

    addButton(buttonPanel, "4");
    addButton(buttonPanel, "5");
    addButton(buttonPanel, "6");
    addButton(buttonPanel, "?");

    addButton(buttonPanel, "1");
    addButton(buttonPanel, "2");
    addButton(buttonPanel, "3");

    addButton(buttonPanel, "0");
    addButton(buttonPanel, "+");
    addButton(buttonPanel, "-");
    // Exercise 1 division
    addButton(buttonPanel, "/");
    // Exercise 1 multi
    addButton(buttonPanel, "*");

    addButton(buttonPanel, "=");

    contentPane.add(buttonPanel, BorderLayout.CENTER);

    status = new JLabel(calc.getAuthor());
    contentPane.add(status, BorderLayout.SOUTH);

    // hexa stuff
    JPanel hexContainer = new JPanel(new GridLayout(2, 1));
    JCheckBox hexBox = new JCheckBox("Hexadecimal mode");
    hexBox.addActionListener(this);

    hexContainer.add(hexBox);

    hexPanel = new JPanel(new GridLayout(2, 3));
    addButton(hexPanel, "A");
    addButton(hexPanel, "B");
    addButton(hexPanel, "C");

    addButton(hexPanel, "D");
    addButton(hexPanel, "E");
    addButton(hexPanel, "F");

    hexContainer.add(hexPanel);

    hexPanel.setVisible(false); // hide it because we start in decimal mode

    contentPane.add(hexContainer, BorderLayout.EAST);

    frame.pack();
  }
예제 #4
0
  /**
   * Toggle the info display in the calculator's status area between the author and version
   * information.
   */
  private void showInfo() {
    if (showingAuthor) status.setText(calc.getVersion());
    else status.setText(calc.getAuthor());

    showingAuthor = !showingAuthor;
  }