21xrx.com
2024-12-22 21:39:14 Sunday
登录
文章检索 我的文章 写文章
Java实现简易计算器 - 完整代码及案例分析
2023-06-16 18:28:11 深夜i     --     --
Java 计算器 GUI 逻辑 完整代码

计算器一直是程序开发中的一个经典案例,无论是初学者还是资深程序员都能从中受益。本文将介绍如何使用Java编写一个简易计算器,并提供完整的代码和实际演示案例。

1. 编写计算器界面

首先,我们需要为计算器创建一个用户界面。使用Java Swing提供的GUI组件库,我们可以轻松地创建一个支持标准算术操作的简单计算器。以下是一个基本的计算器GUI示例:


import javax.swing.*;

import java.awt.*;

public class CalculatorGUI {

  public static void main(String[] args) {

    JFrame frame = new JFrame("简易计算器");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(300, 400);

    JPanel panel1 = new JPanel();

    JPanel panel2 = new JPanel();

    panel1.setLayout(new GridLayout(4, 3));

    panel2.setLayout(new GridLayout(5,1));

    

    JTextField textField = new JTextField("0");

    textField.setEditable(false);

    JButton button0 = new JButton("0");

    JButton button1 = new JButton("1");

    JButton button2 = new JButton("2");

    JButton button3 = new JButton("3");

    JButton button4 = new JButton("4");

    JButton button5 = new JButton("5");

    JButton button6 = new JButton("6");

    JButton button7 = new JButton("7");

    JButton button8 = new JButton("8");

    JButton button9 = new JButton("9");

    JButton buttonC = new JButton("C");

    JButton buttonPlus = new JButton("+");

    JButton buttonMinus = new JButton("-");

    JButton buttonMultiply = new JButton("x");

    JButton buttonDivide = new JButton("/");

    JButton buttonEquals = new JButton("=");

    

    panel1.add(button1);

    panel1.add(button2);

    panel1.add(button3);

    panel1.add(button4);

    panel1.add(button5);

    panel1.add(button6);

    panel1.add(button7);

    panel1.add(button8);

    panel1.add(button9);

    panel1.add(buttonC);

    panel1.add(button0);

    panel1.add(buttonEquals);

    

    panel2.add(buttonPlus);

    panel2.add(buttonMinus);

    panel2.add(buttonMultiply);

    panel2.add(buttonDivide);

    panel2.add(textField);

    

    frame.getContentPane().add(BorderLayout.CENTER, panel1);

    frame.getContentPane().add(BorderLayout.EAST, panel2);

    frame.setVisible(true);

  }

}

2. 实现计算器逻辑

下一步是实现计算器的逻辑。我们需要在用户按下数字和操作符按钮时执行相应的计算,并在文本框中显示结果。以下是一种实现方法:


public class CalculatorLogic {

  private double currentResult;

  private String currentOperator;

  private boolean operationStarted;

  

  public CalculatorLogic()

    currentResult = 0;

    currentOperator = "";

    operationStarted = false;

  

  

  public void enterDigit(int digit) {

    if(operationStarted) {

      currentResult = Double.parseDouble(JTextField.getText());

      operationStarted = false;

    }

    else {

      currentResult *= 10;

    }

    currentResult += digit;

    updateDisplay();

  }

  

  public void enterOperator(String operator) {

    if(currentOperator.equals("+")) {

      currentResult += Double.parseDouble(JTextField.getText());

    }

    else if(currentOperator.equals("-")) {

      currentResult -= Double.parseDouble(JTextField.getText());

    }

    else if(currentOperator.equals("x")) {

      currentResult *= Double.parseDouble(JTextField.getText());

    }

    else if(currentOperator.equals("/")) {

      currentResult /= Double.parseDouble(JTextField.getText());

    }

    else {

      currentResult = Double.parseDouble(JTextField.getText());

    }

    currentOperator = operator;

    operationStarted = true;

  }

  

  private void updateDisplay() {

    JTextField.setText(Double.toString(currentResult));

  }

}

3. 组合GUI和逻辑

最后,我们需要将计算器GUI和逻辑组合在一起。我们可以将计算器逻辑作为一个单独的类,并在GUI中创建一个实例。然后,我们可以将按钮的动作侦听器分配给适当的方法,例如:


public class CalculatorGUI {

  private CalculatorLogic logic;

  private JTextField textField;

  private JButton button0;

  private JButton button1;

  // ...

  public CalculatorGUI() {

    JFrame frame = new JFrame("简易计算器");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(300, 400);

    JPanel panel1 = new JPanel();

    JPanel panel2 = new JPanel();

    panel1.setLayout(new GridLayout(4, 3));

    panel2.setLayout(new GridLayout(5,1));

    

    textField = new JTextField("0");

    textField.setEditable(false);

    button0 = new JButton("0");

    button1 = new JButton("1");

    // ...

    

    logic = new CalculatorLogic();

    

    button0.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent event) {

        logic.enterDigit(0);

      }

    });

    button1.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent event) {

        logic.enterDigit(1);

      }

    });

    // ...

    

    panel1.add(button1);

    panel1.add(button2);

    // ...

    panel2.add(buttonPlus);

    panel2.add(buttonMinus);

    // ...

    panel2.add(textField);

    

    frame.getContentPane().add(BorderLayout.CENTER, panel1);

    frame.getContentPane().add(BorderLayout.EAST, panel2);

    frame.setVisible(true);

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复