用Java实现计算器

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: Java 标签: Java计算器

2021-01-01 21:04:57 709浏览

Java实现简单的计算器
今天给大家分享一个用java实现的计算器的功能。废话不说直接上代码
代码实现(本需求完全有个人实现):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
class MyComputerWin extends JFrame implements ActionListener {
JTextField expressText; //创建一个单行文本框引用
JPanel numPanel, operPanel;// 创建两个面板引用
JPanel panel; //创建一个面板用于存放文本框与"="按钮。
//创建按钮数组用于存放按钮,并定义数组得长度
JButton[] numBtn = new JButton[12];
JButton[] operBtn = new JButton[6];
String express = "";

public void init() {
expressText = new JTextField(1); //实例化单行文本框对象
//实例化面板对象
numPanel = new JPanel();
operPanel = new JPanel();
panel = new JPanel();
//设置面板为网格布局(GridLayout), 注意: 默认是流动布局(FlowLayout)
panel.setLayout(new BorderLayout());
numPanel.setLayout(new GridLayout(4, 3));
operPanel.setLayout(new GridLayout(5, 1));
//将数字按钮添加到数字面板上
for (int i = 0; i < numBtn.length; i++) {
//创建数字按钮
if(i>=0&&i<=2) {
numBtn[i] = new JButton(""+(7+i));
}else if(i>=3&&i<=5) {
numBtn[i] = new JButton(""+(i+1));
}else if(i>=6&&i<=8) {
numBtn[i] = new JButton(""+(i-5));
}else {
numBtn[9] = new JButton(""+"00");
numBtn[10] = new JButton(""+"0");
numBtn[11] = new JButton(""+"^");
}
numPanel.add(numBtn[i]);
numBtn[i].addActionListener(this);

}
//设置符号按钮
operBtn[0] = new JButton("%");
operBtn[1] = new JButton("/");
operBtn[2] = new JButton("*");
operBtn[3] = new JButton("-");
operBtn[4] = new JButton("+");
operBtn[5] = new JButton("=");
//将符号按钮添加到符号面板上
for (int i = 0; i < operBtn.length-1; i++) {
operPanel.add(operBtn[i]);
operBtn[i].addActionListener(this);
}
panel.add(expressText);
panel.add(operBtn[5],BorderLayout.EAST);
operBtn[5].addActionListener(this);


//将文本框添加到窗口上,并设置为边界布局
this.add(panel, BorderLayout.NORTH);
//将面板添加到窗口上
this.add(numPanel, BorderLayout.CENTER);
this.add(operPanel, BorderLayout.EAST);
}

public MyComputerWin(String title) {
super(title);
init();
this.setVisible(true);
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.expressText.setBackground(Color.YELLOW);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand(); //获得发生该事件得相关命令,在这里就是获得按钮事件上得数字。
express += s; //将获得得命令保存起来
expressText.setText(express); //将获得得命令放入文本框中。
//因为s代表得是单个按钮事件上得命令,所以这里判断s获得得命令是不是"="。
if ("=".equals(s)) {
String[] nums = express.split("\\p{Punct}");// 5+6= 正则表达式 \\p{Punct}代表得是标点符号,这里就是通过标点符号(+,-,*,/.....)来进行分割,然后得到我们想要计算得数字。
String[] opers =express.split("\\d+"); //利用正则表达式中得多位数字来进行分解。然后得到我们想要得运算符号
String oper=opers[1]; //定义一个String类型得变量存储符号
double num1=Integer.parseInt(nums[0]);
double num2=Integer.parseInt(nums[1]);
double result=1;
switch(oper) {
case "+":express+=num1+num2;break;
case "-":express+=num1-num2;break;
case "*":express+=num1*num2;break;
case "/":if(num2!=0) {
express+=num1/num2;
}else {
express+="分母为0,不能计算!!";
} break;
case "%":express+=num1/100*num2;break;
case "^":for(int i=0;i<num2;i++) {
result*=num1;
}
express+=result;break;
}
//express+=result;
expressText.setText(express);
express=""; //计算完之后将数据清空
}
}

}

public class MyComputer {

public static void main(String[] args) {
new MyComputerWin("我得计算器");
}
}
行,今天就给大家分享到这里吧,您的一份支持就是我最大的动力,最后打个小广告,我们程序员在学习和工作中或多或少会遇到一些比较棘手的问题,也就所谓的一时半会解决不了的bug,可以来杰凡IT问答平台上提问,平台上大佬很多可以快速给你一对一解决问题,有需要的朋友可以去关注下,平台网址: https://www.jf3q.com

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695