public class Main {
public static void main(String[] args) {
MyFrame frame= new MyFrame("曙光一号");
frame.setVisible(true);
}
}

界面如下图:

简易计算器第一版_i++

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;

// 设定最大长度!!!考虑负数!!!单目运算符多次操作!!!
// .....
String string_1 = "";// 第一个数
String string_2 = "";// 符号
String string_3 = "";// 第二个数
String strings = "0";// 用来设置label(为string_1+string_2+string_3)
boolean isNum_1Exist = false;// 第一个数是否存在
boolean isNum_2Exist = false;// 第二个数是否存在
boolean isSignExist = false;// 是否有运算符
boolean isNum_1Point = false;// 第一个数是否有小数点
boolean isNum_2Point = false;// 第二个数是否有小数点
boolean isAlong = false;//单目运算
JLabel label = new JLabel();

JButton[] buttons = new JButton[20];

public MyFrame(String string) {
super(string);
setBounds(450, 80, 300, 400);// 设置窗口位置大小
setResizable(false);// 窗口不可变
setDefaultCloseOperation(EXIT_ON_CLOSE);// 关闭操作,直接关闭
setLayout(new GridLayout(6, 1));
Font font = new Font(getName(), Font.BOLD, 35);

/// 面板1/
JPanel panel_1 = new JPanel();
label.setHorizontalAlignment(JLabel.RIGHT);
label.setFont(new Font("楷体", ALLBITS, 15));
panel_1.add(label);
add(panel_1);

/// 面板2/
JPanel panel_2 = new JPanel();
panel_2.setLayout(new GridLayout(1, 4));
panel_2.setBackground(Color.lightGray);
panel_2.setFont(font);
buttons[0] = new JButton("%");
buttons[1] = new JButton("sqrt");
buttons[2] = new JButton("^2");
buttons[3] = new JButton("AC");
for (int i = 0; i < 4; i++) {
panel_2.add(buttons[i]);
}
add(panel_2);
/// 面板3/
JPanel panel_3 = new JPanel();
panel_3.setLayout(new GridLayout(1, 4));
panel_3.setBackground(Color.lightGray);
panel_3.setFont(font);
buttons[4] = new JButton("7");
buttons[5] = new JButton("8");
buttons[6] = new JButton("9");
buttons[7] = new JButton("+");
for (int i = 4; i < 8; i++) {
panel_3.add(buttons[i]);
}
add(panel_3);

/// 面板4/
JPanel panel_4 = new JPanel();
panel_4.setLayout(new GridLayout(1, 4));
panel_4.setBackground(Color.lightGray);
panel_4.setFont(font);
buttons[8] = new JButton("4");
buttons[9] = new JButton("5");
buttons[10] = new JButton("6");
buttons[11] = new JButton("-");

for (int i = 8; i < 12; i++) {
panel_4.add(buttons[i]);
}
add(panel_4);

/// 面板5/
JPanel panel_5 = new JPanel();
panel_5.setLayout(new GridLayout(1, 4));
panel_5.setBackground(Color.lightGray);
panel_5.setFont(font);
buttons[12] = new JButton("1");
buttons[13] = new JButton("2");
buttons[14] = new JButton("3");
buttons[15] = new JButton("*");

for (int i = 12; i < 16; i++) {
panel_5.add(buttons[i]);
}
add(panel_5);
/// 面板6/
JPanel panel_6 = new JPanel();
panel_6.setLayout(new GridLayout(1, 4));
panel_6.setBackground(Color.lightGray);
panel_6.setFont(font);
buttons[16] = new JButton("0");
buttons[17] = new JButton(".");
buttons[18] = new JButton("=");
buttons[19] = new JButton("/");

for (int i = 16; i < 20; i++) {
panel_6.add(buttons[i]);
}
add(panel_6);
// 添加监听
for (int i = 0; i < buttons.length; i++) {
buttons[i].addActionListener(this);
}
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttons[3]) {// AC
remove();
}

符号/
if (isNum_1Exist) {
// 单目运算不需要按等号
if (e.getSource() == buttons[1] && isNum_1Exist) {// sqrt
double i = Double.parseDouble(string_1);
double s = Math.sqrt(i);
String string = "" + s;
label.setText(string);
string_1 = string;
isNum_1Exist = true;
isAlong = true;
} else if (e.getSource() == buttons[2] && isNum_1Exist) {// ^2
double i = Double.parseDouble(string_1);
i = i * i;
String string = "" + i;
label.setText(string);
string_1 = string;
isNum_1Exist = true;
isAlong = true;
} else if (e.getSource() == buttons[0]) {// %
setDual("%");
} else if (e.getSource() == buttons[7]) {// +
setDual("+");
} else if (e.getSource() == buttons[11]) {// -
setDual("-");
} else if (e.getSource() == buttons[15]) {// *
setDual("*");
} else if (e.getSource() == buttons[19]) {// /
setDual("/");
} else if (e.getSource() == buttons[18]) {// =
// 计算:
String str = count();
// 清空数据
remove();
label.setText(str);
}
}
/// 数字//

// 第一个数没有 或者 有了第一个数还没有符号 就加到第一个数
if (e.getSource() == buttons[4]) {// 7
setNum("7");
} else if (e.getSource() == buttons[5]) {// 8
setNum("8");
} else if (e.getSource() == buttons[6]) {// 9
setNum("9");
} else if (e.getSource() == buttons[8]) {// 4
setNum("4");
} else if (e.getSource() == buttons[9]) {// 5
setNum("5");
} else if (e.getSource() == buttons[10]) {// 6
setNum("6");
} else if (e.getSource() == buttons[12]) {// 1
setNum("1");
} else if (e.getSource() == buttons[13]) {// 2
setNum("2");
} else if (e.getSource() == buttons[14]) {// 3
setNum("3");
} else if (e.getSource() == buttons[16]) {// 0
setNum("0");
} else if (e.getSource() == buttons[17]) {// .
// 一个数只能有一个
if ((!isNum_1Exist) || (isNum_1Exist && !isSignExist)) {// 1
if (!isNum_1Point) {
setNum1(".");
isNum_1Point = true;
}
} else {// 2
if (!isNum_2Point)
setNum2(".");
isNum_2Point = true;
}
}
}

// 计算(双目)
// 结果的label 在这里统一设置
private String count() {
try {
BigDecimal b1 = new BigDecimal(string_1);
BigDecimal b2 = new BigDecimal(string_3);
switch (string_2) {
case "+":
BigDecimal b = b1.add(b2);
strings = b.toString();
break;
case "-":
b = b1.subtract(b2);
strings = b.toString();
break;
case "*":
b = b1.multiply(b2);
strings = b.toString();
break;
case "/":
if (!(string_3.equals("0"))) {
b = b1.divide(b2);
strings = b.toString();
}else {
strings = "除数不能为零";
}
break;
case "%":
b = b1.remainder(b2);
strings = b.toString();
break;
default:

}
} catch (Exception e) {
}
return strings;
}

private void setDual(String s) {
// 双目运算:第一个数存在,第二个数还不存在的是时候可以加符号
// 否则如果两个都存在了就相当于等号(执行运算方法!!)
if (isNum_1Exist && !isNum_2Exist) {
string_2 = s;
isSignExist = true;
strings = string_1 + string_2;
label.setText(strings);
} else if (isNum_1Exist && isNum_2Exist) {
String str = count();
remove();
string_1 = str;
isNum_1Exist = true;
string_2 = s;
isSignExist = true;
strings = string_1 + string_2;
label.setText(strings);
}
}

private void remove() {
label.setText("");
isNum_1Exist = false;
isNum_2Exist = false;
isSignExist = false;
isNum_1Point = false;
isNum_2Point = false;
string_1 = "";
string_2 = "";
string_3 = "";
}

private void setNum(String s) {
if(isAlong){
string_1 = "";
isNum_1Exist = false;
isAlong = false;
}
if ((!isNum_1Exist) || (isNum_1Exist && !isSignExist)) {// 1
setNum1(s);
} else {// 2
setNum2(s);
}
}

private void setNum1(String s) {
string_1 += s;
isNum_1Exist = true;
label.setText(string_1);
}

private void setNum2(String s) {
string_3 += s;
isNum_2Exist = true;
strings = string_1 + string_2 + string_3;
label.setText(strings);
}
}