Java计算器(2)

     1  import java.awt.*;
     2  import java.awt.event.*;
     3
     4  class Calc extends Frame{
     5    private Display display;
     6    private Operator operator;
     7    private Context context;
     8    private int register1;
     9    private int register2;
    10
    11    private static class Display extends Label{
    12      Display(){
    13        setAlignment(Label.RIGHT);
    14      }
    15      void setNumber(int value){
    16        setText(String.valueOf(value));
    17      }
    18    }
    19
    20    private Panel createKeyboard(){
    21      Panel keyboard = new Panel();
    22
    23      keyboard.setLayout(new GridLayout(4, 4));
    24
    25      keyboard.add(new AllClearButton());
    26      keyboard.add(new OperatorButton(Operator.DIVIDE));
    27      keyboard.add(new OperatorButton(Operator.MULTIPLY));
    28      keyboard.add(new OperatorButton(Operator.SUBTRACT));
    29
    30      keyboard.add(new NumberButton(7));
    31      keyboard.add(new NumberButton(8));
    32      keyboard.add(new NumberButton(9));
    33      keyboard.add(new OperatorButton(Operator.ADD));
    34
    35      keyboard.add(new NumberButton(4));
    36      keyboard.add(new NumberButton(5));
    37      keyboard.add(new NumberButton(6));
    38      keyboard.add(new ResultButton());
    39
    40      keyboard.add(new NumberButton(1));
    41      keyboard.add(new NumberButton(2));
    42      keyboard.add(new NumberButton(3));
    43      keyboard.add(new NumberButton(0));
    44
    45      return (keyboard);
    46    }
    47
    48    private void allClear(){
    49      operator  = Operator.ADD;
    50      context   = CONTEXT_OPERATOR;
    51      register1 = 0;
    52      register2 = 0;
    53      display.setText("0");
    54    }
    55
    56  /** Calculator class*/
    57      private Calc(){
    58      setSize(300, 300);
    59      setLayout(new BorderLayout());
    60      add(display = new Display(),  BorderLayout.NORTH);
    61      add(createKeyboard(), BorderLayout.CENTER);
    62      allClear();
    63    }
    64
    65    private static abstract class Operator{
    66      abstract int apply(int left, int right);
    67      abstract char symbol();
    68
    69      static final Operator ADD = new Operator(){
    70        int apply(int left, int right){ return (left + right); }
    71        char symbol() { return ('+'); }
    72      };
    73      static final Operator SUBTRACT = new Operator(){
    74        int apply(int left, int right){ return (left - right); }
    75        char symbol() { return ('-'); }
    76      };
    77      static final Operator MULTIPLY = new Operator(){
    78        int apply(int left, int right){ return (left * right); }
    79        char symbol() { return ('*'); }
    80      };
    81      static final Operator DIVIDE = new Operator(){
    82        int apply(int left, int right){ return (left / right); }
    83        char symbol() { return ('/'); }
    84      };
    85    }
    86
    87    private static abstract class Context{
    88      abstract void inputNumber(int input_number);
    89      abstract void inputOperator(Operator input_operator);
    90      abstract void inputResult();
    91    }
    92
    93    private final Context CONTEXT_NUMBER = new Context(){
    94      void inputNumber(int input_number){
    95        register1 = register1 * 10 + input_number;
    96        display.setNumber(register1);
    97      }
    98      void inputOperator(Operator input_operator){
    99        register2 = operator.apply(register2, register1);
   100        display.setNumber(register2);
   101        operator = input_operator;
   102      }
   103      void inputResult(){
   104        register2 = operator.apply(register2, register1);
   105        display.setNumber(register2);
   106      }
   107    };
   108
   109    private final Context CONTEXT_OPERATOR = new Context(){
   110      void inputNumber(int input_number){
   111        register1 = input_number;
   112        display.setNumber(register1);
   113      }
   114      void inputOperator(Operator input_operator){
   115        operator = input_operator;
   116      }
   117      void inputResult(){
   118      }
   119    };
   120
   121    private final Context CONTEXT_RESULT = new Context(){
   122      void inputNumber(int input_number){
   123        allClear();
   124        register1 = input_number;
   125        display.setNumber(register1);
   126      }
   127      void inputOperator(Operator input_operator){
   128        operator = input_operator;
   129      }
   130      void inputResult(){
   131        register2 = operator.apply(register2, register1);
   132        display.setNumber(register2);
   133      }
   134    };
   135
   136    private static abstract class CalcButton extends Button implements ActionListener{
   137      CalcButton(String label){
   138        super(label);
   139        addActionListener(this);
   140      }
   141    }
   142
   143    private class NumberButton extends CalcButton{
   144      int number;
   145
   146      NumberButton(int this_number){
   147        super(String.valueOf(this_number));
   148        number = this_number;
   149      }
   150      public void actionPerformed(ActionEvent e){
   151        context.inputNumber(number);
   152        context = CONTEXT_NUMBER;
   153      }
   154    }
   155
   156    private class OperatorButton extends CalcButton{
   157      Operator operator;
   158
   159      OperatorButton(Operator this_operator){
   160        super(String.valueOf(this_operator.symbol()));
   161        operator = this_operator;
   162      }
   163      public void actionPerformed(ActionEvent e){
   164        context.inputOperator(operator);
   165        context = CONTEXT_OPERATOR;
   166      }
   167    }
   168
   169    private class ResultButton extends CalcButton{
   170      ResultButton(){
   171        super("=");
   172      }
   173      public void actionPerformed(ActionEvent e){
   174        context.inputResult();
   175        context = CONTEXT_RESULT;
   176      }
   177    }
   178
   179    private class AllClearButton extends CalcButton{
   180      AllClearButton(){
   181        super("AC");
   182      }
   183      public void actionPerformed(ActionEvent e){
   184        allClear();
   185      }
   186    }
   187
   188    public static void main(String[] args){
   189      Calc c = new Calc();
   190      try{
   191        synchronized(c){
   192          c.show();
   193          c.wait();
   194        }
   195      }
   196      catch(InterruptedException e){
   197        System.out.println("abort");
   198      }
   199    }
   200  }

好的,下面是一个Java计算器的示例代码,它支持2、8、10进制计算: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener { private JTextField display; private String operator = ""; private double num1 = 0, num2 = 0; private boolean decimal = false; private int base = 10; public Calculator() { super("Calculator"); // Create the display field display = new JTextField("0"); display.setEditable(false); display.setHorizontalAlignment(JTextField.RIGHT); getContentPane().add(display, BorderLayout.NORTH); // Create the button panel JPanel buttonPanel = new JPanel(new GridLayout(4, 4)); // Add the number buttons for (int i = 0; i <= 9; i++) { JButton button = new JButton(Integer.toString(i, base)); button.addActionListener(this); buttonPanel.add(button); } // Add the operator buttons JButton addButton = new JButton("+"); addButton.addActionListener(this); buttonPanel.add(addButton); JButton subtractButton = new JButton("-"); subtractButton.addActionListener(this); buttonPanel.add(subtractButton); JButton multiplyButton = new JButton("*"); multiplyButton.addActionListener(this); buttonPanel.add(multiplyButton); JButton divideButton = new JButton("/"); divideButton.addActionListener(this); buttonPanel.add(divideButton); JButton equalsButton = new JButton("="); equalsButton.addActionListener(this); buttonPanel.add(equalsButton); JButton clearButton = new JButton("C"); clearButton.addActionListener(this); buttonPanel.add(clearButton); JButton decimalButton = new JButton("."); decimalButton.addActionListener(this); buttonPanel.add(decimalButton); JButton binaryButton = new JButton("Bin"); binaryButton.addActionListener(this); buttonPanel.add(binaryButton); JButton octalButton = new JButton("Oct"); octalButton.addActionListener(this); buttonPanel.add(octalButton); JButton decimalBaseButton = new JButton("Dec"); decimalBaseButton.addActionListener(this); buttonPanel.add(decimalBaseButton); getContentPane().add(buttonPanel, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { String action = e.getActionCommand(); if (action.equals("+") || action.equals("-") || action.equals("*") || action.equals("/")) { operator = action; num1 = Double.parseDouble(display.getText()); display.setText("0"); decimal = false; } else if (action.equals("=")) { num2 = Double.parseDouble(display.getText()); double result = 0; if (operator.equals("+")) { result = num1 + num2; } else if (operator.equals("-")) { result = num1 - num2; } else if (operator.equals("*")) { result = num1 * num2; } else if (operator.equals("/")) { result = num1 / num2; } display.setText(Double.toString(result)); decimal = false; } else if (action.equals("C")) { display.setText("0"); decimal = false; } else if (action.equals(".")) { if (!decimal) { display.setText(display.getText() + "."); decimal = true; } } else if (action.equals("Bin")) { base = 2; display.setText(Integer.toString((int) num1, base)); } else if (action.equals("Oct")) { base = 8; display.setText(Integer.toString((int) num1, base)); } else if (action.equals("Dec")) { base = 10; display.setText(Double.toString(num1)); } else { if (display.getText().equals("0")) { display.setText(action); } else { display.setText(display.getText() + action); } } } public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setSize(300, 300); calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); calculator.setVisible(true); } } ``` 该计算器类使用Swing库来实现GUI界面,并实现了ActionListener接口以处理各种事件。它支持数字输入、四则运算、小数点输入、清除、二进制、八进制、十进制数的转换。 希望这个示例代码可以帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值