import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import javax.swing.*;
public class panelCommon extends JPanel implements ActionListener {
JButton button1, button2, button3, button4, button5, button6, button7,
button8, button9, button0, buttonJia, buttonJian, buttonCheng,
buttonChu, buttonDeng, buttonDian, buttonC, buttonDel, buttonZF,
buttonGH;
JTextField jtf;
CalWin calwin;
char oper;
String key, allKey = "1234567890.±del", jjccKey = "+-*/";
BigDecimal d1, d2;
boolean first;
public panelCommon(CalWin cw) {
calwin = cw;
jtf = new JTextField(18);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jtf.setEditable(false);
jtf.setBackground(new Color(255, 255, 255));
jtf.setText("0");
this.add(jtf);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
button0 = new JButton("0");
buttonJia = new JButton("+");
buttonJian = new JButton("-");
buttonCheng = new JButton("*");
buttonChu = new JButton("/");
buttonDeng = new JButton("=");
buttonDian = new JButton(".");
buttonDel = new JButton("del");
buttonC = new JButton("c");
buttonZF = new JButton("±");
buttonGH = new JButton("√");
this.add(buttonDel);
this.add(buttonC);
this.add(buttonZF);
this.add(buttonGH);
this.add(button1);
this.add(button2);
this.add(button3);
this.add(buttonJia);
this.add(button4);
this.add(button5);
this.add(button6);
this.add(buttonJian);
this.add(button7);
this.add(button8);
this.add(button9);
this.add(buttonCheng);
this.add(button0);
this.add(buttonDian);
this.add(buttonDeng);
this.add(buttonChu);
buttonDel.addActionListener(this);
buttonC.addActionListener(this);
buttonZF.addActionListener(this);
buttonGH.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button0.addActionListener(this);
buttonJia.addActionListener(this);
buttonJian.addActionListener(this);
buttonCheng.addActionListener(this);
buttonChu.addActionListener(this);
buttonDeng.addActionListener(this);
buttonDian.addActionListener(this);
init();
}
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
if (b == buttonDian) {
if (this.jtf.getText().indexOf(".") > 0)
calwin.cal.move();
else
jtf.setText(jtf.getText() + ".");
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
} else if (b == buttonZF) {
BigDecimal d = getNum();
d = new BigDecimal("0").subtract(d);
setFormatNum(d);
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
} else if (b == buttonDel) {
if (jtf.getText().length() == 1 || jtf.getText().length() == 2
&& jtf.getText().startsWith("-")) {
jtf.setText("0");
} else {
String s = jtf.getText();
s = s.substring(0, s.length() - 1);
if (s.endsWith("E+") || s.endsWith("E-"))
s = s.substring(0, s.length() - 2);
jtf.setText(s);
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
}
} else if (b == buttonC) {
init();
} else if (b == buttonJia) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
if (oper == '-' || oper == '/') {
s = operate(d1, oper, d2);
} else {
s = operate(d2, oper, d1);
}
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
// System.out.print("111我执行啦\n");
oper = '+';
} else if (b == buttonJian) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
s = operate(d1, oper, d2);
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
oper = '-';
} else if (b == buttonCheng) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
if (oper == '-' || oper == '/') {
s = operate(d1, oper, d2);
} else {
s = operate(d2, oper, d1);
}
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
oper = '*';
} else if (b == buttonChu) {
BigDecimal s;
if (!first && allKey.indexOf(key) >= 0) {
s = operate(d1, oper, d2);
setFormatNum(s);
}// 如果不是第一次,且上一次操作是对数字进行操作
else {
first = false;
}
d2 = d1 = getNum();
oper = '/';
} else if (b == buttonGH) {
if (jtf.getText().startsWith("-")) {
calwin.cal.move();
init();
return;
}
BigDecimal d = getNum();
MathContext mc = new MathContext(10, RoundingMode.HALF_DOWN);
d = new BigDecimal(Math.sqrt(d.doubleValue()), mc);
setFormatNum(d);
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}
} else if (b == buttonDeng) {
BigDecimal s = null;
d2 = getNum();
if (!key.equals("=")) {
s = operate(d1, oper, d2);
} else {
s = operate(d2, oper, d1);
}
if (oper != '=' && !key.equals("=")) {// 上一次操作为 +-*/
d1 = getNum();
}
setFormatNum(s);
first = true;
} else {
if (this.jtf.getText().equals("0") || allKey.indexOf(key) == -1
) {
this.jtf.setText(b.getText());
}
else {
this.jtf.setText(jtf.getText() + b.getText());
}
if (first) {
d2 = d1 = getNum();
} else {
d2 = getNum();
}// d2作被加数:d2+d1
}
key = b.getText();
// System.out.println(d1.toString()+" "+d2.toString());
}
public BigDecimal getNum() {
BigDecimal d = new BigDecimal(jtf.getText());
return d;
}
public void setFormatNum(BigDecimal dd) {
MathContext mc = new MathContext(18, RoundingMode.HALF_DOWN);
BigDecimal d = new BigDecimal(dd.toString(), mc);
String num = d.toString();
if (num.endsWith(".0"))
num = num.replaceAll("\\.0", "");
this.jtf.setText(num);
// System.out.print("222我执行啦\n");
}
public void init() {
d1 = new BigDecimal("0");
d1 = new BigDecimal("0");
key = "=";
oper = '=';
jtf.setText("0");
first = true;
}
public BigDecimal operate(BigDecimal a1, char c, BigDecimal a2) {
MathContext mc = new MathContext(10, RoundingMode.HALF_DOWN);
switch (c) {
case '+':
return a1.add(a2);
case '-':
return a1.subtract(a2);
case '*':
return a1.multiply(a2);
case '/':
if (a2.compareTo(new BigDecimal("0")) == 0) {
init();
calwin.cal.move();
return new BigDecimal("0");
}
return a1.divide(a2, mc);
default:
return a2;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java swing带GUI界面的计算器系统源码.zip
共15个文件
class:6个
java:5个
jar:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 177 浏览量
2023-11-08
14:55:29
上传
评论
收藏 880KB ZIP 举报
温馨提示
实例源码,参考学习使用。 资源使用有什么问题可以直接私信博主,博主每日都会看csdn,看到会回复。
资源推荐
资源详情
资源评论
收起资源包目录
java swing带GUI界面的计算器系统源码.zip (15个子文件)
calculator2.5
lib
mail.jar 451KB
.classpath 283B
src
panelCommon.java 7KB
Cal.java 5KB
BugWin.java 1KB
CalWin.java 4KB
Mailbug.java 1KB
bin
BugWin.class 2KB
panelCommon.class 7KB
Cal.class 7KB
CalWin.class 6KB
BugWin$MyThread.class 575B
Mailbug.class 2KB
.project 389B
计算器.jar 469KB
共 15 条
- 1
资源评论
等天晴i
- 粉丝: 3430
- 资源: 10万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功