21xrx.com
2024-12-23 02:32:14 Monday
登录
文章检索 我的文章 写文章
Java实现简易计算器程序
2023-06-15 19:04:42 深夜i     --     --
Java 计算器 程序设计

作为一名程序员,我们经常需要开发一些小工具来帮助我们完成日常的任务。而计算器恰好是我们日常工作中必不可少的工具之一。本文将教你如何使用Java语言编写一个简易计算器程序,以方便你日常的计算操作。

在Java中,我们可以使用JFrame类来创建一个窗体,使用JPanel来创建计算器的界面布局。然后我们可以定义各种按钮来完成不同的计算操作。最后,我们可以将计算结果输出在文本框中。以下是Java代码实现:


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Calculator extends JFrame implements ActionListener {

private JPanel mainPanel;

private JPanel controlPanel;

private JTextField resultField;

private JButton addButton, subButton, mulButton, divButton, equalButton;

private JButton[] numButtons;

private String result = "0";

private String lastCommand = "=";

private boolean start = true;

public Calculator() {

mainPanel = new JPanel();

mainPanel.setLayout(new BorderLayout());

resultField = new JTextField(result, 20);

resultField.setHorizontalAlignment(JTextField.RIGHT);

resultField.setEditable(false);

mainPanel.add(resultField, BorderLayout.NORTH);

controlPanel = new JPanel();

controlPanel.setLayout(new GridLayout(4, 4));

numButtons = new JButton[10];

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

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

numButtons[i].addActionListener(this);

controlPanel.add(numButtons[i]);

}

addButton = new JButton("+");

addButton.addActionListener(this);

controlPanel.add(addButton);

subButton = new JButton("-");

subButton.addActionListener(this);

controlPanel.add(subButton);

mulButton = new JButton("*");

mulButton.addActionListener(this);

controlPanel.add(mulButton);

divButton = new JButton("/");

divButton.addActionListener(this);

controlPanel.add(divButton);

equalButton = new JButton("=");

equalButton.addActionListener(this);

controlPanel.add(equalButton);

mainPanel.add(controlPanel, BorderLayout.CENTER);

this.add(mainPanel);

this.setTitle("简易计算器程序");

this.setSize(250, 250);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if (command.charAt(0) >= '0' && command.charAt(0) <= '9' || command.equals(".")) {

if (start)

result = "";

start = false;

result += command;

resultField.setText(result);

} else {

if (start) {

if (command.equals("-")) {

result = command;

resultField.setText(result);

start = false;

} else

lastCommand = command;

} else {

calculate(Double.parseDouble(result));

lastCommand = command;

start = true;

}

}

}

public void calculate(double x) {

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

result = String.valueOf(Double.parseDouble(result) + x);

} else if (lastCommand.equals("-")) {

result = String.valueOf(Double.parseDouble(result) - x);

} else if (lastCommand.equals("*")) {

result = String.valueOf(Double.parseDouble(result) * x);

} else if (lastCommand.equals("/")) {

result = String.valueOf(Double.parseDouble(result) / x);

}

resultField.setText(result);

}

public static void main(String[] args) {

new Calculator();

}

}

以上代码实现了加、减、乘、除等基本功能,同时支持小数的计算。你可以复制上面的代码到IDE中进行编译和运行。

  
  

评论区

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