目录,选择性观看,觉得可以给个辛苦赞的幸苦大家滑下去咯
直接上代码吧,需要的直接复制就好,滑到那下面挺费时的
CalculatorGui类
package gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculatorGui extends JFrame implements ActionListener {
//流程控制数据
double sum = 0;
String x = "";
String y = "";
int subn = 0;
int muln = 0;
int divn = 0;
int num = 0;
double b = 0;
//if判断按钮事件真假
boolean point = false;
boolean add = false;
boolean sub = false;
boolean mul = false;
boolean div = false;
boolean sinc = false;
boolean cosc = false;
boolean t = false;
JPanel topJPanel = new JPanel();// 顶端选项界面
JLabel selectJLabel = new JLabel("选项");// 选项标签
JPanel textJPanel = new JPanel();// 输入数据界面
JLabel textLabel = new JLabel("0");// 文本输入框
JPanel mainJPanel = new JPanel();// 主运算按钮
JButton jb[] = new JButton[9];
JButton jb0 = new JButton("0");
JButton jbleft = new JButton("sin");
JButton jbright = new JButton("cos");
JPanel eastJPanel = new JPanel();// 加减乘除按钮
JButton deleteJButton = new JButton("←");
JButton clearJButton = new JButton("C");
JButton addJButton = new JButton("+");
JButton subJButton = new JButton("-");
JButton mulJButton = new JButton("*");
JButton divJButton = new JButton("/");
JButton pointJButton = new JButton(".");
JButton equalJButton = new JButton("=");
// 添加数字按键方法
public void addJButton() {
mainJPanel.setLayout(new GridLayout(4, 3));
for (int i = 0; i < 9; i++) {
jb[i] = new JButton("" + (i + 1) + "");
jb[i].addActionListener(this);
mainJPanel.add(jb[i]);
}
jb0.addActionListener(this);
jbleft.addActionListener(this);
jbright.addActionListener(this);
mainJPanel.add(jbleft);
mainJPanel.add(jb0);
mainJPanel.add(jbright);
}
@Override
public void actionPerformed(ActionEvent e) {
// 循环处理数字按钮监听
for (int i = 0; i < 10; i++) {
if (e.getActionCommand().equals("" + i + "")) {
if (num == 0) {
if (textLabel.getText().equals("0") || div || sub || add || mul || sinc||cosc) {
textLabel.setText("" + i + "");
x = textLabel.getText();
num++;
}
} else
// if(t){
// textLabel.setText("0");
// y=x;
// t=false;
// }
// else
{
textLabel.setText(textLabel.getText() + i);
x = textLabel.getText();
}
}
}
// 小数点
if (e.getActionCommand().equals(".")) {
if (textLabel.getText().equals("0")) {
textLabel.setText("0.");
x = "0.";
point = true;
}
else {
textLabel.setText(textLabel.getText() + ".");
x = textLabel.getText();
point = true;
}
}
// if(e.getActionCommand().equals("2")) {
// if(textLabel.getText().equals("0")) {
// textLabel.setText("2");
// x=textLabel.getText();
// }else
if(t){
textLabel.setText("0");
y=x;
t=false;
}
else
// {
// textLabel.setText(textLabel.getText()+"2");
// x=textLabel.getText();
// }
// }
// //此下面都是同理复制
// if(e.getActionCommand().equals("3")) {
// if(textLabel.getText().equals("0")) {
// textLabel.setText("3");
// x=textLabel.getText();
// }else
// {
// textLabel.setText(textLabel.getText()+"3");
// x=textLabel.getText();
// }
// }
// 获取加法指令
if (e.getActionCommand().equals("+")) {
if (textLabel.getText().equals("0")) {
textLabel.setText("+");
if (div) {
textLabel.setText("除数不能为0");
}
} else {
if (sub) {
sum = new Getsum().getsub(sum, Double.parseDouble(x));
} else if (mul) {
sum = new Getsum().getmul(sum, Double.parseDouble(x));
} else if (div) {
sum = new Getsum().getdiv(sum, Double.parseDouble(x));
} else if (sinc) {
sum = Math.sin(Double.parseDouble(x));
}else if(cosc) {
sum = Math.cos(Double.parseDouble(x));
}
else {
// textLabel.setText(textLabel.getText()+"+");
sum = new Getsum().getadd(sum, Double.parseDouble(x));
}
textLabel.setText("+");
num = 0;
add = true;
sub = false;
mul = false;
point = false;
div = false;
sinc=false;
cosc=false;
}
}
// 获取减法指令
if (e.getActionCommand().equals("-")) {
if (textLabel.getText().equals("0")) {
// textLabel.setText("-");
y = textLabel.getText();
if (div) {
textLabel.setText("除数不能为0");
}
} else {
// textLabel.setText(textLabel.getText()+"-");
if (add) {
sum = new Getsum().getadd(sum, Double.parseDouble(x));
} else if (mul) {
sum = new Getsum().getmul(sum, Double.parseDouble(x));
} else if (div) {
sum = new Getsum().getdiv(sum, Double.parseDouble(x));
} else if (sinc) {
sum = Math.sin(Double.parseDouble(x));
}else if(cosc) {
sum = Math.cos(Double.parseDouble(x));
}
else if (subn == 0) {
y = textLabel.getText();
sum = Double.parseDouble(y);
} else {
sum = new Getsum().getsub(sum, Double.parseDouble(x));
}
textLabel.setText("-");
num = 0;
subn++;
sub = true;
add = false;
mul = false;
point = false;
div = false;
sinc=false;
cosc=false;
}
}
// 获取乘法指令
if (e.getActionCommand().equals("*")) {
if (textLabel.getText().equals("0")) {
// textLabel.setText("-");
y = textLabel.getText();
if (div) {
textLabel.setText("除数不能为0");
}
} else {
// textLabel.setText(textLabel.getText()+"-");
if (add) {
sum = new Getsum().getadd(sum, Double.parseDouble(x));
} else if (sub) {
sum = new Getsum().getsub(sum, Double.parseDouble(x));
} else if (div) {
sum = new Getsum().getdiv(sum, Double.parseDouble(x));
} else if (sinc) {
sum = Math.sin(Double.parseDouble(x));
}else if(cosc) {
sum = Math.cos(Double.parseDouble(x));
}
else if (muln == 0) {
y = textLabel.getText();
sum = Double.parseDouble(y);
} else {
sum = new Getsum().getmul(sum, Double.parseDouble(x));
}
textLabel.setText("*");
num = 0;
muln++;
mul = true;
add = false;
sub = false;
point = false;
div = false;
sinc=false;
cosc=false;
}
}
// 获取除法指令
if (e.getActionCommand().equals("/")) {
if (textLabel.getText().equals("0")) {
// textLabel.setText("-");
y = textLabel.getText();
} else {
// textLabel.setText(textLabel.getText()+"-");
if (add) {
sum = new Getsum().getadd(sum, Double.parseDouble(x));
} else if (sub) {
sum = new Getsum().getsub(sum, Double.parseDouble(x));
} else if (mul) {
sum = new Getsum().getmul(sum, Double.parseDouble(x));
} else if (divn == 0) {
y = textLabel.getText();
sum = Double.parseDouble(y);
} else if (x.equals("0")) {
textLabel.setText("除数不能为0");
} else if (sinc) {
sum = Math.sin(Double.parseDouble(x));
}else if(cosc) {
sum = Math.cos(Double.parseDouble(x));
}
else {
sum = new Getsum().getdiv(sum, Double.parseDouble(x));
}
textLabel.setText("/");
num = 0;
muln++;
div = true;
mul = false;
add = false;
sub = false;
point = false;
sinc=false;
cosc=false;
}
}
// 退格键的功能实现
if (e.getActionCommand().equals("←")) {
// x=x.substring(x.length()-1,x.length());
try {
x = x.substring(0, x.length() - 1);
} catch (StringIndexOutOfBoundsException ex) {
}
if (x.length() == 0) {
textLabel.setText("0");
} else {
textLabel.setText(x);
}
}
//清零按钮
if (e.getActionCommand().equals("C")) {
sum = 0;
add = false;
sub = false;
mul = false;
point = false;
sinc=false;
cosc=false;
subn = 0;
muln = 0;
x = "";
y = "";
num=0;
textLabel.setText("0");
}
//sin结果
if (e.getActionCommand().equals("sin")) {
if (add) {
sum = new Getsum().getadd(sum, Double.parseDouble(x));
} else if (sub) {
sum = new Getsum().getsub(sum, Double.parseDouble(x));
} else if (mul) {
sum = new Getsum().getmul(sum, Double.parseDouble(x));
} else if (divn == 0) {
y = textLabel.getText();
sum = Double.parseDouble(y);
} else if (x.equals("0")) {
textLabel.setText("除数不能为0");
}else if(cosc) {
sum = Math.cos(Double.parseDouble(x));
}
textLabel.setText("sin");
num = 0;
div = false;
mul = false;
add = false;
sub = false;
point = false;
sinc=true;
cosc=false;
}
//cos结果
if (e.getActionCommand().equals("cos")) {
try {
if (add) {
sum = new Getsum().getadd(sum, Double.parseDouble(x));
} else if (sub) {
sum = new Getsum().getsub(sum, Double.parseDouble(x));
} else if (mul) {
sum = new Getsum().getmul(sum, Double.parseDouble(x));
} else if (x.equals("0")) {
textLabel.setText("除数不能为0");
}
else if (sinc) {
sum = Math.sin(Double.parseDouble(x));
}}
catch(NumberFormatException ex){
}
textLabel.setText("cos");
div = false;
mul = false;
add = false;
sub = false;
point = false;
sinc=false;
cosc=true;
num = 0;
}
//等于的时候判断前面执行的是哪一种运算并且输出结果
if (e.getActionCommand().equals("=")) {
if (point) {
x = textLabel.getText() + "0";
}
if (textLabel.getText().equals("0")) {
textLabel.setText("0");
} else
// 输出加法结果
if (add) {
sum = sum + Double.parseDouble(x);
String s = Double.toString(sum);
if (s.endsWith(".0")) {
s = s.substring(0, s.length() - 2);
}
textLabel.setText(s);
add = false;
}
// 输出减法结果
if (sub) {
sum = sum - Double.parseDouble(x);
String s = Double.toString(sum);
if (s.endsWith(".0")) {
s = s.substring(0, s.length() - 2);
}
textLabel.setText(s);
sub = false;
}
// 输出乘法结果
if (mul) {
sum = sum * Double.parseDouble(x);
String s = Double.toString(sum);
if (s.endsWith(".0")) {
s = s.substring(0, s.length() - 2);
}
textLabel.setText(s);
mul = false;
}
// 输出除法结果
if (div) {
if (x.equals("0")) {
textLabel.setText("除数不能为0");
} else {
sum = sum / Double.parseDouble(x);
String s = Double.toString(sum);
if (s.endsWith(".0")) {
s = s.substring(0, s.length() - 2);
}
textLabel.setText(s);
mul = false;
}
}
//输出sin结果
if (sinc) {
sum = Math.sin(Double.parseDouble(x));
String s = Double.toString(sum);
if (s.endsWith(".0")) {
s = s.substring(0, s.length() - 2);
}
textLabel.setText(s);
sinc = false;
}
//输出cos结果
if (cosc) {
sum = Math.cos(Double.parseDouble(x));
String s = Double.toString(sum);
if (s.endsWith(".0")) {
s = s.substring(0, s.length() - 2);
}
textLabel.setText(s);
cosc = false;
}
}
subn = 0;
muln = 0;
// add=false;
// sub=false;
// mul=false;
}
//构造方法显示窗口
public CalculatorGui() {
textJPanel.add(textLabel);
this.add(textJPanel, BorderLayout.NORTH);
addJButton();
this.add(mainJPanel, BorderLayout.CENTER);
// 加减乘除加到面板
eastJPanel.setLayout(new GridLayout(4, 2));
eastJPanel.add(deleteJButton);
eastJPanel.add(clearJButton);
eastJPanel.add(addJButton);
eastJPanel.add(subJButton);
eastJPanel.add(mulJButton);
eastJPanel.add(divJButton);
eastJPanel.add(pointJButton);
eastJPanel.add(equalJButton);
// 给加减乘除添加监听器
deleteJButton.addActionListener(this);
clearJButton.addActionListener(this);
addJButton.addActionListener(this);
subJButton.addActionListener(this);
mulJButton.addActionListener(this);
divJButton.addActionListener(this);
pointJButton.addActionListener(this);
equalJButton.addActionListener(this);
this.add(eastJPanel, BorderLayout.EAST);
this.setBounds(300, 300, 400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Getsum类
package gui;
/*
* 这个类其实没有什么必要写,但是所有的代码放到一个类中太不容易读了
* 优化过程还会添加更多的其他类和方法的
*/
public class Getsum {
private double sum=0;
//加法
public double getadd(double sum,double x) {
this.sum=sum+x;
return this.sum;
}
public double setsum(double y) {
this.sum=y;
return this.sum;
}
//减法
public double getsub(double sum,double x) {
this.sum=sum-x;
return this.sum;
}
//乘法
public double getmul(double sum,double x) {
this.sum=sum*x;
return this.sum;
}
//除法
public double getdiv(double sum,double x) {
this.sum=sum/x;
return this.sum;
}
}
Test类
package gui;
/*
* 测试类,直接new就好
*/
public class Test {
public static void main(String[] args) {
new CalculatorGui();
}
}
简单说明:
做这个计算器前前后后花了我大概一周左右的时间,毕竟是边学边做的,速度比较慢哈,大佬勿喷,但是啊,我就想着,大家很多人都和我一样,刚刚学这个,所以说思路简单一点也比较方便阅读,注释部分我给的还是挺多的,更多更全的注释也没那么多时间,我想这样其实也还是可以了,500行代码没有功劳也有苦劳,既然您都原因滑下来看到这里啦,我相信还是会有人愿意支持我的,需要后续注释可以私信我,我尽量满足。