21xrx.com
2024-11-22 13:59:53 Friday
登录
文章检索 我的文章 写文章
作为一个初学者
2023-06-11 07:09:04 深夜i     --     --
Java 计算器 初学者

作为一个初学者,我一直想学习如何编写一个简易的计算器,最近我学习了Java的基础知识后,终于可以实现我的梦想了。

首先,我需要设计一个用户界面,界面应该包括数字按键、运算符按键、清除键和等于键,通过这些按键来实现计算器的基本功能。

以下是代码示例,您可以将其保存为Calculator.java文件:


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener {

  private JButton[] numButtons;

  private JButton[] opButtons;

  private JButton clearButton, equalButton;

  private JTextField displayField;

  private JPanel numPanel, opPanel, buttonPanel;

  private double firstNum, secondNum, result;

  private String operator;

  public Calculator() {

    super("Calculator");

    numButtons = new JButton[10];

    for (int i = 0; i < 10; i++) {

      numButtons[i] = new JButton(String.valueOf(i));

    }

    opButtons = new JButton[4];

    opButtons[0] = new JButton("+");

    opButtons[1] = new JButton("-");

    opButtons[2] = new JButton("*");

    opButtons[3] = new JButton("/");

    clearButton = new JButton("C");

    equalButton = new JButton("=");

    displayField = new JTextField(25);

    numPanel = new JPanel(new GridLayout(4, 3, 5, 5));

    opPanel = new JPanel(new GridLayout(4, 1, 5, 5));

    buttonPanel = new JPanel(new FlowLayout());

    numPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    opPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    for (int i = 1; i <= 9; i++) {

      numPanel.add(numButtons[i]);

      numButtons[i].addActionListener(this);

    }

    numPanel.add(new JPanel());

    numPanel.add(numButtons[0]);

    numButtons[0].addActionListener(this);

    numPanel.add(new JPanel());

    for (int i = 0; i < 4; i++) {

      opPanel.add(opButtons[i]);

      opButtons[i].addActionListener(this);

    }

    buttonPanel.add(clearButton);

    clearButton.addActionListener(this);

    buttonPanel.add(equalButton);

    equalButton.addActionListener(this);

    getContentPane().setLayout(new BorderLayout());

    getContentPane().add(displayField, BorderLayout.NORTH);

    getContentPane().add(numPanel, BorderLayout.CENTER);

    getContentPane().add(opPanel, BorderLayout.EAST);

    getContentPane().add(buttonPanel, BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pack();

    setLocationRelativeTo(null);

    setVisible(true);

  }

  public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();

    for (int i = 0; i < 10; i++) {

      if (source == numButtons[i]) {

        displayField.setText(displayField.getText() + i);

        return;

      }

    }

    if (source == opButtons[0]) {

      firstNum = Double.parseDouble(displayField.getText());

      operator = "+";

      displayField.setText("");

    } else if (source == opButtons[1]) {

      firstNum = Double.parseDouble(displayField.getText());

      operator = "-";

      displayField.setText("");

    } else if (source == opButtons[2]) {

      firstNum = Double.parseDouble(displayField.getText());

      operator = "*";

      displayField.setText("");

    } else if (source == opButtons[3]) {

      firstNum = Double.parseDouble(displayField.getText());

      operator = "/";

      displayField.setText("");

    } else if (source == clearButton) {

      displayField.setText("");

    } else if (source == equalButton) {

      secondNum = Double.parseDouble(displayField.getText());

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

        result = firstNum + secondNum;

      } else if (operator.equals("-"))

        result = firstNum - secondNum;

       else if (operator.equals("*")) {

        result = firstNum * secondNum;

      } else if (operator.equals("/"))

        result = firstNum / secondNum;

      

      displayField.setText(String.valueOf(result));

    }

  }

  public static void main(String[] args) {

    new Calculator();

  }

}

在这个例子中,我使用了Java Swing库来定制用户界面。通过实现ActionListener接口,我可以在用户点击按键时捕获事件并响应。当用户点击数字按键时,我将其添加到一个文本框中,运算符按键将获取第一个数字并保存运算符,等于按键将获取第二个数字并计算结果。

通过这个例子,我学会了如何使用Java编写一个简单的计算器,我相信这对于初学者来说是非常有用的。

标题:Java编写简易计算器初学者教程

  
  

评论区

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