packageCaculator;importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Color;importjava.awt.Frame;importjava.awt.GridLayout;importjava.awt.Panel;importjava.awt.TextField;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.math.BigDecimal;importjava.math.MathContext;importjavax.swing.JFrame;importcom.sun.glass.events.WindowEvent;public classMyCalculator {public static voidmain(String[] args) {newMyCalculator().launch();
}public boolean result=false;//输入#之后result变为真,此时可以计算结果了,用于判断能否进行计算。
public boolean end=false;//前面有内容的时候才将其变为true,且输入#之后什么都不能输入了。/*** 首先对这几个boolean变量进行具体阐述:rusult只有在#(结束符)输入之后才会赋为true,也就是说只有输入#之后才能输入==;
* end用于#(结束符)能否进行输入,#前必须为正确的式子,故#的前一位只能是数字或者右括号。
*
* 分为以下几种:数字,算数符(+ - * /),左括号,右括号,点,清零符
* 数字任何时候均可进行输入
* judge1用于运算符的判断,运算符之前只能有数字,括号
* judge2用于点的判断。
* judgeleft用于左括号的判定,另外只有在输入左括号之后才能输入右括号,另外输完左括号之后还能再输,可以连续输入两个左括号
* judgeleftright 用于右括号的判定,另外输完右括号之后还能再输,可以连续输入两个右括号
* 其中还有一些情况没有完全避免
**/
public boolean judge1=false;public boolean judge2=true;public boolean judgeleft=true;public boolean judgeleftright=false;public voidlaunch() {
TextField tf=new TextField(30);
tf.setSize(300, 100);
tf.setBackground(Color.WHITE);
tf.setEditable(true);
JFrame interface1=newJFrame();
interface1.setTitle("按#=得结果");
interface1. setLayout(new BorderLayout());//创建BorderLayout布局(几行几列)
interface1.add(tf,BorderLayout.NORTH);
Panel p=new Panel(new GridLayout(5,4,5,5));//将面板设置成5行四列
for(int i=0;i<10;i++)
{
Button bn=new Button(""+i);
bn.setActionCommand("数字");//数字为一类
p.add(bn);
bn.addActionListener(newActionEventDemo(interface1,tf));
}
Button D=new Button(".");
D.setActionCommand("点");
p.add(D);
D.addActionListener(new ActionEventDemo(interface1,