- 苏萦
-
package example;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame {
/**
* 本实例实现功能如下 1.普通加减乘除运算 2.小数点的情况已经解决 3.开始按0已经解决 4.消去键可以起作用
*
*/
private static final long serialVersionUID = 1L;
private String name[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
".", "=", "+", "-", "*", "/", "Backspace", "", "", "C" };
private Button button[] = new Button[name.length];
private TextField textfield = new TextField("0.");
// 设置2个字符A1,A2用于存放点击运算符号之前的String数据
private String A1 = null, A2 = null;
// 设置2个字符B1,B2用于存放点击运算符号之后的String数据
private String B1 = null, B2 = null;
// 存放运算符号前后的数据,douuble类型进行运算
private double A, B;
// s存放为哪种运算符号,Result存放最后的运行结果
private String Result="0", s;
// 判断这个数字是否为小数,小数的时为true不是时为false
private boolean Decimal=false;
// 构造器,显示在标题栏
public Calculator() {
super("TEST:Caculator");
}
// 计算器的基本布局,在一个BorderLayout上面放置了一个GridLayout一个BorderLayout
public void init() {
setLayout(new BorderLayout(2, 2));
// 设置2个Panel
Panel p0 = new Panel();
Panel p1 = new Panel();
// p0上添加所有按扭
p0.setLayout(new GridLayout(5, 4, 1, 1));
// 不同的按扭采用不同的监听事件0-9和"."采用ButtonListener()
for (int i = 0; i < 11; i++) {
button = new Button(name);
// 设置字体颜色为蓝色
button.setForeground(Color.blue);
p0.add(button);
button.addActionListener(new ButtonListener());
}
// 其余的运算符号采取ButtonListener2()另一监听事件
for (int i = 11; i < name.length; i++) {
button = new Button(name);
// 设置字体颜色为红色
button.setForeground(Color.RED);
p0.add(button);
button.addActionListener(new ButtonListener2());
}
// 设置中间2个按扭不可见,增加美观,对称
button[17].setVisible(false);
button[18].setVisible(false);
// p1上添加文本输出区域
p1.setLayout(new BorderLayout(5, 5));
p1.add(textfield);
// 不可以向文本框里随便写东西
textfield.setEditable(false);
// 设置文本框背景为白色
textfield.setBackground(Color.WHITE);
add(p0, BorderLayout.SOUTH);
add(p1, BorderLayout.NORTH);
pack();
// 设置打开窗口在显示器的中间显示
setLocationRelativeTo(null);
// 关闭按扭,适配器模式
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 调整可见
setVisible(true);
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.print(e.getActionCommand() + " ");
// 当A2和B2同时为null时候表示程序第一次运行或者开始一次新的运行,即按"="之后,将按键获取的值赋给A2--setp1
if (A2 == null && B2 == null) {
// 所按的数字按扭不是"0"的时候对A2进行赋值,否则给A2值不变,但是让textfield恢复初始值"0."
if (!"0".equals(e.getActionCommand())) {
// 考虑第一次按小数点的情况,按小数点后将boolean类型的Decimal定义为true
if (".".equals(e.getActionCommand())) {
A2 = "0.";
Decimal = true;
textfield.setText(A2);
} else {
A2 = e.getActionCommand();
textfield.setText(A2);
}
} else {
if ("0".equals(e.getActionCommand())) {
} else {
A2 = e.getActionCommand();
textfield.setText("0");
}
}
// 当A2不等于null,B2和A为null,表示还没有按运算符号,仍然对A2进行赋值
} else if (A2 != null && A1 == null && B2 == null) {
// 已经是小数的在点小数点不做任何动作,不是小数的在按"."的时候追加赋值并且设置Decimal为true
if (".".equals(e.getActionCommand())) {
if (Decimal) {
} else {
Decimal = true;
A2 += e.getActionCommand();
textfield.setText(A2);
}
} else {
A2 += e.getActionCommand();
textfield.setText(A2);
}
// 当A,A2不等于null,B2为null,表示刚按过运算符号,开始对B2进行赋值
// 仍要考虑"0"的情况,但要考虑0做为被减数,乘数和加数,此处B2可以等于0
// B2也可以是小数将Decimal设置回false
} else if (A2 != null && A1 != null && B2 == null) {
Decimal = false;
if (!"0".equals(e.getActionCommand())) {
// 不为"0"但为"."的情况,原理同上
if (".".equals(e.getActionCommand())) {
Decimal = true;
if (Decimal) {
B2 = "0.";
} else {
Decimal = true;
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 = "0";
textfield.setText(B2);
}
// 当A,A2,B2都不为null的时候表示B2已经被赋值,继续按数字按扭对B2继续赋值
// 当B2等于0的时候不进行追加直接赋值,仅当B2不等于0的时候才进行追加
} else if (A2 != null && A1 != null && B2 != null) {
if ("0".equals(B2)) {
// 考虑用户连续2次按"0"的情况
if ("0".equals(e.getActionCommand())) {
B2 = "0";
textfield.setText(B2);
} else {
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
// 不为"0"但为"."的情况,原理同上
if (".".equals(e.getActionCommand())) {
if (Decimal) {
} else {
Decimal = true;
B2 += e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 += e.getActionCommand();
textfield.setText(B2);
}
}
}
}
}
public class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.print(e.getActionCommand() + " ");
// 一旦按了运算符号"+-*/"A2赋值给A,使得A和A2都不为null,但此时B2还为null在按数字键的时候便会给B2赋值
// 给s--运算符号赋值
if ("+".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "+";
} else if ("-".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "-";
} else if ("*".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "*";
} else if ("/".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "/";
// 等号的时候把B2赋值给B,进行最后的运算
} else if ("=".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
if (B2 == null) {
B2 = "0";
}
A1 = A2;
B1 = B2;
// 将String类型转换为double进行运算
A = Double.parseDouble(A1);
B = Double.parseDouble(B1);
if ("+".equals(s)) {
Result = String.valueOf(A + B);
System.out.println();
System.out.println(A + "+" + B + "=" + Result);
} else if ("-".equals(s)) {
Result = String.valueOf(A - B);
System.out.println();
System.out.println(A + "-" + B + "=" + Result);
} else if ("*".equals(s)) {
Result = String.valueOf(A * B);
System.out.println();
System.out.println(A + "*" + B + "=" + Result);
}
textfield.setText(Result);
A1 = null;
A2 = null;
B1 = null;
B2 = null;
Decimal=false;
} else if ("Backspace".equals(e.getActionCommand())) {
String text = "0";
if (A2 == null && A1 == null) {
} else if (A2 != null && A1 == null && B2 == null) {
int t = A2.length();
text = A2;
if (t == 1) {
text = "0";
} else {
text = text.substring(0, t - 1);
}
A2 = text;
} else if (A2 != null && A1 != null && B2 == null) {
} else {
int t = B2.length();
text = B2;
if (t == 1) {
text = "0";
} else {
text = text.substring(0, t - 1);
}
B2 = text;
}
textfield.setText(text);
System.out.println();
System.out.println("text=" + text + " ");
System.out.println(A1 + ":" + A2 + ":" + B1 + ":" + B2);
// 选择"C"的时候将计算器重置,即恢复到开始的状态
} else if ("C".equals(e.getActionCommand())) {
textfield.setText("0.");
A1 = null;
A2 = null;
B1 = null;
B2 = null;
Decimal=false;
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.init();
}
} 下面还有