要清理文件了,把这个代码保存一下。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
* topic:java-Calculate
* author:yll
* date:2019-5-31
* */
public class Calcu extends JFrame implements ActionListener {
// 结果文本框
JTextField result = new JTextField("0");
// 按钮
JButton[] btns = new JButton[30];
// 菜单栏
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenu helpMenu = new JMenu("帮助");
JMenu otherMenu = new JMenu("其他");
JMenuItem saveMI = new JMenuItem("保存");
JMenuItem exitMI = new JMenuItem("退出");
JMenuItem helpMI = new JMenuItem("帮助");
JMenuItem aboutMI = new JMenuItem("关于");
JMenuItem bugMI = new JMenuItem("bug");
JMenuItem feedbackMI = new JMenuItem("反馈");
double cal, before, back;
// 第一个操作数
String foreText = new String("");
// 第二个操作数
String backText = new String("");
boolean flag1 = false; // 判断是否需要第二个操作数
boolean flag2 = false; // 判断第二个操作数是否输入
boolean flag3 = false;// 是否计算完
boolean flag = false; // 第一个操作数是否输完
boolean eq = false;// 是否有等号
String op = "";
public void init() {
fileMenu.add(saveMI);
fileMenu.addSeparator();
fileMenu.add(exitMI);
helpMenu.add(helpMI);
helpMenu.addSeparator();
helpMenu.add(aboutMI);
otherMenu.add(bugMI);
otherMenu.addSeparator();
otherMenu.add(feedbackMI);
mb.add(fileMenu);
mb.add(helpMenu);
mb.add(otherMenu);
this.add(result, BorderLayout.NORTH);
JPanel p = new JPanel();
p.setLayout(new GridLayout(6, 4, 3, 3));
// 把25个按钮放在25个单元格
for (int i = 0; i < 10; i++) {
btns[i] = new JButton(String.valueOf(i));
btns[i].setBackground(Color.white);
btns[i].addActionListener(this);
}
// 一层
btns[10] = new JButton("%");
btns[11] = new JButton("Sqrt");
btns[12] = new JButton("X^2");
btns[13] = new JButton("1/x");
p.add(btns[10]);
p.add(btns[11]);
p.add(btns[12]);
p.add(btns[13]);
// 二层
btns[14] = new JButton("CE");
btns[15] = new JButton("C");
btns[16] = new JButton("<—");
btns[17] = new JButton("÷");
p.add(btns[14]);
p.add(btns[15]);
p.add(btns[16]);
p.add(btns[17]);
// 三层
btns[18] = new JButton("*");
p.add(btns[7]);
p.add(btns[8]);
p.add(btns[9]);
p.add(btns[18]);
// 四层
btns[19] = new JButton("-");
p.add(btns[4]);
p.add(btns[5]);
p.add(btns[6]);
p.add(btns[19]);
// 五层
btns[20] = new JButton("+");
p.add(btns[1]);
p.add(btns[2]);
p.add(btns[3]);
p.add(btns[20]);
// 六层
btns[21] = new JButton("=");
btns[22] = new JButton(".");
btns[23] = new JButton("-+");
p.add(btns[23]);
p.add(btns[0]);
p.add(btns[22]);
p.add(btns[21]);
// 给按钮添加监听器
btns[10].addActionListener(this);// %
btns[11].addActionListener(this);// Sqrt
btns[12].addActionListener(this);// x^2
btns[13].addActionListener(this);// 1/x
btns[14].addActionListener(this);// CE 只是清楚当前输入的数据或符号
btns[15].addActionListener(this);// C 全部清零
btns[16].addActionListener(this);// <- 撤退
btns[17].addActionListener(this);// ÷
btns[18].addActionListener(this);// *
btns[19].addActionListener(this);// -
btns[20].addActionListener(this);// +
btns[21].addActionListener(this);// =
btns[22].addActionListener(this);// .
btns[23].addActionListener(this);// -+
this.add(p);
this.setJMenuBar(mb);
this.setVisible(true);
this.setResizable(true);
this.setBounds(700, 300, 370, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 事件监听器
aboutMI.addActionListener(this);
helpMI.addActionListener(this);
exitMI.addActionListener(this);
saveMI.addActionListener(this);
bugMI.addActionListener(this);
feedbackMI.addActionListener(this);
}
public static void main(String[] args) {
Calcu t = new Calcu();
t.init();
}
@Override
public void actionPerformed(ActionEvent e) {
// 菜单栏有关
dealMenu(e);
/* <- 符号处理 */
dealBack(e);
/* C CE 处理 */
dealClear(e);
// 第一個操作數
if (!flag)
dealNumberone(e);
/* 符号处理 */
if (op.equals(""))
dealCharacter(e);
// 第二個操作數
if (!flag1)
dealNumbertwo(e);
// 等号
if (flag && e.getSource() == btns[21]) {
Calculate(flag1);
}
if (flag3 && e.getSource() != btns[21]) {
System.out.println(flag3);
JOptionPane.showMessageDialog(null, "请先清除全部(C)之后再使用!");
}
}
public void dealMenu(ActionEvent e) {
// 菜单栏有关
if (e.getSource() == this.aboutMI) {
JOptionPane.showMessageDialog(null, "小小Java计算器V2.0\n 开发人:杨露露");
}
if (e.getSource() == this.helpMI) {
JOptionPane.showMessageDialog(null, "百度搜索计算器使用说明学习一下");
}
if (e.getSource() == this.feedbackMI) {
JOptionPane.showMessageDialog(null, " 联系方式\n邮件:1026533954@qq.com \n 谢谢!");
}
if (e.getSource() == this.exitMI) {
System.exit(0);
}
if (e.getSource() == this.saveMI) {
JOptionPane.showMessageDialog(null, "抱歉!此功能尚未开放");
}
if (e.getSource() == this.bugMI) {
JOptionPane.showMessageDialog(null, "1、负数情况不适用\n2、小数点在操作数首位不适用\n3、待发现……");
}
}
/* 第一个操作数 */
public String dealNumberone(ActionEvent e) {
// 第一个操作数
for (int i = 0; i <= 9; i++) {
if (e.getSource() == btns[i] && !flag) {
result.setText("");
foreText = foreText + e.getActionCommand();
result.setText(foreText);
}
}
// 第一个数的小数点
if (!flag && foreText != "") {
if (e.getSource() == btns[22]) {
foreText = foreText + e.getActionCommand();
result.setText(foreText);
}
}
return foreText;
}
/* 第二个操作数 */
public String dealNumbertwo(ActionEvent e) {
if (dealIllegal(e) && flag2 && op != "" && !eq) {
JOptionPane.showMessageDialog(null, "不合法!请重新输入!");
result.setText(backText);
} else {
// 第二个操作数
for (int i = 0; i <= 9; i++) {
if (e.getSource() == btns[i] && flag) {
flag2 = true;
backText = backText + e.getActionCommand();
result.setText(backText);
}
}
// 第二个数的小数点
if (flag && backText != "") {
if (e.getSource() == btns[22]) // 小数点
{
backText = backText + e.getActionCommand();
result.setText(backText);
}
}
}
return backText;
}
// 計算 Flag判斷是否需要第二個操作數
public void Calculate(boolean Flag) {
// 不需要
if (Flag) {
eq = true;
if (op.equals("X^2")) {
cal = this.before * this.before;
} else if (op.equals("1/x")) {
cal = 1 / this.before;
} else if (op.equals("Sqrt")) {
cal = Math.sqrt(this.before);
} else if (op.equals("-+")) {
cal = -this.before;
} else
cal = 0.0;
this.result.setText(String.valueOf(cal));
flag3 = true;
} else // 需要
{
eq = true;
if (backText.equals("")) {
JOptionPane.showMessageDialog(null, "请重新输入合法的操作数!");
ClearAll();
//System.out.println(flag3+"--");
} else {
this.back = Double.parseDouble(backText);
if (op.equals("+"))
cal = this.before + this.back;
else if (op.equals("-"))
cal = this.before - this.back;
else if (op.equals("*"))
cal = this.before * this.back;
else if (op.equals("÷") && this.back != 0.0)
cal = this.before / this.back;
else if (op.equals("%"))
cal = this.before % this.back;
else
cal = 0.0;
this.result.setText(String.valueOf(cal));
flag3 = true;
}
}
}
/* C CE 处理 */
public void dealClear(ActionEvent e) {
/* C 全部清空 CE 清空當前输入的数据或符号 处理 */
if (e.getSource() == btns[15]) // C
{
ClearAll();
}
if (e.getSource() == btns[14])// CE
{
if (!flag) // 代表第一个操作数没有数完
{
foreText = "";
this.result.setText("0");
} else if (backText != "" && eq == false)// 第二个数没有输入完 并且没有输入等号
{
backText = "";
this.result.setText("0");
} else if (flag && backText.equals(""))// 第一个数输入完并且第二个数还没有开始输入
{
op = "";
}
}
}
/* 运算符 */
public void dealCharacter(ActionEvent e) {
if (e.getSource() == btns[10] || e.getSource() == btns[17] || e.getSource() == btns[18]
|| e.getSource() == btns[19] || e.getSource() == btns[20]) {
flag = true;
this.before = Double.parseDouble(foreText);
result.setText("");
op = e.getActionCommand();// op代表运算类型
} else if (e.getSource() == btns[11] || e.getSource() == btns[12] || e.getSource() == btns[13]
|| e.getSource() == btns[23]) {
flag1 = true; // 不需要第二個操作數了
flag = true;
this.before = Double.parseDouble(foreText);
result.setText("");
op = e.getActionCommand();// op代表运算类型
}
}
public void dealBack(ActionEvent e) {
if (e.getSource() == btns[16]) {
/* 在没输入意外的情况下 */
/* 运算符没输入的话说明输入的是第一个操作符 */
if (op.equals("") && !flag) {
foreText = foreText.substring(0, foreText.length() - 1);
result.setText(foreText);
} else if (op != "" && backText.equals(""))// 消除op
{
op = "";
} else if (backText != "") {
backText = backText.substring(0, backText.length() - 1);
result.setText(backText);
}
}
}
/* 处理不合法的情况 输入第二个数的时候出现运算符 */
public boolean dealIllegal(ActionEvent e) {
if (e.getSource() == btns[10] || e.getSource() == btns[17] || e.getSource() == btns[18]
|| e.getSource() == btns[19] || e.getSource() == btns[20] || e.getSource() == btns[11]
|| e.getSource() == btns[12] || e.getSource() == btns[13] || e.getSource() == btns[23])
return true;
return false;
}
/* 全部清除 */
public void ClearAll() {
flag = false;
flag1 = false;
flag2 = false;
flag3 = false;
eq = false;
foreText = "";
backText = "";
op = "";
this.result.setText("0");
}
}