21xrx.com
2024-12-23 03:42:58 Monday
登录
文章检索 我的文章 写文章
Java编写简易计算器界面:实现加减乘除功能
2023-06-16 09:52:01 深夜i     --     --
Java 计算器界面 加减乘除

Java是一种高级编程语言,广泛应用于各行各业。在本篇文章中,我们将使用Java编写一个简易计算器界面,实现加减乘除功能。

我们首先需要创建一个Java工程,并在工程中创建一个名为“Calculator”的Java类。接着,我们需要引入Java Swing和AWT包,以便实现计算器界面的可视化。

下面是完整的Java代码实现:

 java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame {

  private JTextField textField;

  private double firstNum, secondNum, result;

  private String operation;

  // 构造函数

  public Calculator() {

    setTitle("简易计算器");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLocationRelativeTo(null);

    setResizable(false);

    // 界面布局

    JPanel buttonPanel = new JPanel();

    buttonPanel.setLayout(new GridLayout(5, 4));

    // 文本框

    textField = new JTextField();

    textField.setHorizontalAlignment(JTextField.RIGHT);

    textField.setEditable(false);

    textField.setBackground(Color.WHITE);

    // 操作按钮

    JButton addButton = new JButton("+");

    addButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        operation = "+";

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

        textField.setText("");

      }

    });

    JButton subtractButton = new JButton("-");

    subtractButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        operation = "-";

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

        textField.setText("");

      }

    });

    JButton multiplyButton = new JButton("*");

    multiplyButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        operation = "*";

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

        textField.setText("");

      }

    });

    JButton divideButton = new JButton("/");

    divideButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        operation = "/";

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

        textField.setText("");

      }

    });

    // 数字按钮

    JButton button1 = new JButton("1");

    button1.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "1");

      }

    });

    JButton button2 = new JButton("2");

    button2.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "2");

      }

    });

    JButton button3 = new JButton("3");

    button3.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "3");

      }

    });

    JButton button4 = new JButton("4");

    button4.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "4");

      }

    });

    JButton button5 = new JButton("5");

    button5.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "5");

      }

    });

    JButton button6 = new JButton("6");

    button6.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "6");

      }

    });

    JButton button7 = new JButton("7");

    button7.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "7");

      }

    });

    JButton button8 = new JButton("8");

    button8.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "8");

      }

    });

    JButton button9 = new JButton("9");

    button9.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "9");

      }

    });

    JButton button0 = new JButton("0");

    button0.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText() + "0");

      }

    });

    // 等于按钮

    JButton equalsButton = new JButton("=");

    equalsButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

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

        switch (operation) {

          case "+":

            result = firstNum + secondNum;

            break;

          case "-":

            result = firstNum - secondNum;

            break;

          case "*":

            result = firstNum * secondNum;

            break;

          case "/":

            result = firstNum / secondNum;

            break;

          default:

            break;

        }

        textField.setText("" + result);

      }

    });

    // 清除按钮

    JButton clearButton = new JButton("C");

    clearButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        textField.setText("");

      }

    });

    // 添加组件到按钮面板

    buttonPanel.add(button1);

    buttonPanel.add(button2);

    buttonPanel.add(button3);

    buttonPanel.add(addButton);

    buttonPanel.add(button4);

    buttonPanel.add(button5);

    buttonPanel.add(button6);

    buttonPanel.add(subtractButton);

    buttonPanel.add(button7);

    buttonPanel.add(button8);

    buttonPanel.add(button9);

    buttonPanel.add(multiplyButton);

    buttonPanel.add(clearButton);

    buttonPanel.add(button0);

    buttonPanel.add(equalsButton);

    buttonPanel.add(divideButton);

    // 添加组件到窗体

    add(buttonPanel, BorderLayout.CENTER);

    add(textField, BorderLayout.NORTH);

    // 显示窗体

    pack();

    setVisible(true);

  }

  // 主函数

  public static void main(String[] args) {

    new Calculator();

  }

}

现在我们已经实现了一个简易的计算器界面,包括加减乘除和清除功能。我们可以在主函数中调用Calculator类来显示计算器窗口。

三个

  
  

评论区

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