效果如上,代码如下
整体分为窗口、运算、按钮事件三个部分
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.StrictMath.pow;
import static java.lang.StrictMath.sqrt;
public class Calculator extends JFrame implements ActionListener
{
private JTextField resultText = new JTextField("0.0");//输出结果
private double now=0,ex=0;//now:当前答案,ex:二目运算符的前一个数
private double l=0;//小数部
private int flag=0;//记录小数位数,0时表示当前不是输入小数的状态
private String how="";//当前的运算符
private String temp="";//上一个运算符
private double mem=0;//存储区
private Boolean haveMem=false;//存储区是否有内容
public Calculator()
{
super("计算器");
this.setLayout(null);//手动计算各部分的位置
resultText.setBounds(20, 5, 320, 40);//设置答案框的位置和大小
resultText.setHorizontalAlignment(JTextField.RIGHT);//答案右对齐
resultText.setEditable(false);
this.add(resultText);
int x=20,y=55;//(x,y)表示当前插入按钮的光标的位置
String[] KEYS = {"MC", "MR", "MS", "M+", "M-", "Del", "CE", "C", "+-", "sqrt", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ".", "+"};
//按钮对象数组
JButton[] keys = new JButton[KEYS.length];
for (int i = 0; i< KEYS.length; i++) {
keys[i] = new JButton();//声明和初始化按钮
keys[i].setText(KEYS[i]);
if(i==25) {
keys[i].setBounds(x, y, 125, 40);//0的宽度占2格
x+=65;
}
else if(i==24) keys[i].setBounds(x, y, 60, 85);//=的高度占2格
else keys[i].setBounds(x, y, 60, 40);//按钮的默认大小60*40,间距为5
if(x<260) {
x+=65;
}
else {
x = 20;
y+=45;
}//每五个按钮换一行,行距为5
this.add(keys[i]);
}
for (int i = 0; i < KEYS.length; i++)
{
keys[i].addActionListener(this);
}
this.setResizable(false);
this.setBounds(1000, 400, 375, 380);//设置对话框的位置和大小
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
//得到运算结果
private double play(double ex,double now,String how){
double ans=0;
switch (how) {
case "+":
ans = ex + now;
break;
case "-":
ans = ex - now;
break;
case "*":
ans = ex * now;
break;
case "/":
ans = ex / now;
break;
case "%":
ans = ex % now;
break;
case "+-":
ans = -now;
break;
case "1/x":
ans = 1 / now;
break;
case "sqrt":
ans = sqrt(now);
break;
default:
ans = now;
break;
}
return ans;
}
public void actionPerformed(ActionEvent e) {
String x = e.getActionCommand();
String[] num ={"1","2","3","4","5","6","7","8","9","0"};
String[] cal2 ={"+","-","*","/","%"};//双目运算符
String[] cal1 ={"+-","sqrt","1/x"};//单目运算符
for(String i:num){//输入数字
if(x==i){
//上一个数是否是“结果”
if (how == "=") {
now = 0;
how = "";
}
//整数部分
if(flag==0) {
now = now * 10 + Integer.parseInt(i);
}
//小数部分
else {
l = l * 10 + Integer.parseInt(i);
now=now-(now%1)+l/pow(10,flag);
flag++;
}
}
}
for(String i:cal2){//输入是双目运算符
if(x==i){
flag=0;
if(temp==x) return ;
//第一个数
if(ex==0){
ex=now;
now=0;
}
//第二个数
else{
now=play(ex,now,how);
ex=0;
}
how=i;
}
}
for(String i:cal1){//输入是单目运算符
if(x==i){
flag=0;
how=x;
}
}
if(x=="Del"){//删除
//整数
if(flag==0) now=(now-now%10)/10;
//小数
else{
l=(l-l%10)/10;
now=now-(now%1)+l/pow(10,flag-2);
flag--;
}
}
else if(x=="="){//等于
flag=0;
now=play(ex,now,how);
ex=0;
how="=";
}
else if(x=="CE"){
//消除当前输入的数
flag=0;
now=0;
l=0;
//消除运算符
for(String i:cal2)
if (temp == i) {
how = "";
break;
}
for(String i:cal1)
if (temp == i) {
how = "";
break;
}
}
else if(x=="C"){//重启计算器
flag=0;
now=0;
ex=0;
how="";
}
else if(x=="."&&flag==0){
flag++;
l=0;
}
else if(x=="MC"){
haveMem=false;
}
else if(x=="MS"){
mem=now;
now=0;
haveMem=true;
}
else if(x=="MR") if (haveMem) now=mem;
else if(x=="M+") if (haveMem) mem=mem+now;
else if(x=="M-") if (haveMem) mem=mem-now;
resultText.setText(String.valueOf(now));//显示结果
System.out.println(ex+" "+now+" "+mem);
temp=x;//最后更新temp
}
public static void main(String[] args) {
Calculator a=new Calculator();
}
}
//青大学子勿用
参考了https://blog.csdn.net/qq_41398808/article/details/79558789