简单计算器
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Calculator extends JFrame { JTextField windows; JButton data0,data1,data2,data3,data4,data5,data6,data7,data8; JButton data9,datajia,datajian,datacheng,datachu,dataequl,databack; JButton clear; JPanel pbutton; InnerPolice police; String data=" "; double[] number=new double[10]; char[] signal=new char[10]; public Calculator() { windows=new JTextField(34); //设置显示窗口 //windows.setSize(20, 20); windows.setEditable(false); JPanel Pwindows=new JPanel(); Pwindows.add(windows); data0=new JButton("0"); //定义按键以及字体 data0.setFont(new Font("宋体",Font.BOLD,30)); data1=new JButton("1"); data1.setFont(new Font("宋体",Font.BOLD,30)); data2=new JButton("2"); data2.setFont(new Font("宋体",Font.BOLD,30)); data3=new JButton("3"); data3.setFont(new Font("宋体",Font.BOLD,30)); data4=new JButton("4"); data4.setFont(new Font("宋体",Font.BOLD,30)); data5=new JButton("5"); data5.setFont(new Font("宋体",Font.BOLD,30)); data6=new JButton("6"); data6.setFont(new Font("宋体",Font.BOLD,30)); data7=new JButton("7"); data7.setFont(new Font("宋体",Font.BOLD,30)); data8=new JButton("8"); data8.setFont(new Font("宋体",Font.BOLD,30)); data9=new JButton("9"); data9.setFont(new Font("宋体",Font.BOLD,30)); datajia=new JButton("+"); datajia.setFont(new Font("宋体",Font.BOLD,30)); datajian=new JButton("-"); datajian.setFont(new Font("宋体",Font.BOLD,30)); datacheng=new JButton("*"); datacheng.setFont(new Font("宋体",Font.BOLD,30)); datachu=new JButton("/"); datachu.setFont(new Font("宋体",Font.BOLD,30)); dataequl=new JButton("="); dataequl.setFont(new Font("宋体",Font.BOLD,30)); databack=new JButton("backspace"); databack.setFont(new Font("宋体",Font.BOLD,30)); clear=new JButton("删除"); clear.setFont(new Font("宋体",Font.BOLD,30)); //clear.setSize(100, 10); //clear.addActionListener(police); JPanel pclear=new JPanel(); pclear.add(clear); pbutton=new JPanel(); //向容器中添加按钮 pbutton.setLayout(new GridLayout(4,4)); pbutton.add(data1); pbutton.add(data2); pbutton.add(data3); pbutton.add(datajia); pbutton.add(data4); pbutton.add(data5); pbutton.add(data6); pbutton.add(datajian); pbutton.add(data7); pbutton.add(data8); pbutton.add(data9); pbutton.add(datacheng); pbutton.add(data0); pbutton.add(databack); pbutton.add(dataequl); pbutton.add(datachu); setBounds(100,100,400,260); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(Pwindows,BorderLayout.NORTH); add(pbutton,BorderLayout.CENTER); add(pclear,BorderLayout.SOUTH); police=new InnerPolice(); //添加监视器进行监视 data0.addActionListener(police); data1.addActionListener(police); data2.addActionListener(police); data3.addActionListener(police); data4.addActionListener(police); data5.addActionListener(police); data6.addActionListener(police); data7.addActionListener(police); data8.addActionListener(police); data9.addActionListener(police); datajia.addActionListener(police); datajian.addActionListener(police); datacheng.addActionListener(police); datachu.addActionListener(police); dataequl.addActionListener(police); databack.addActionListener(police); clear.addActionListener(police); } class InnerPolice implements ActionListener //监视器函数体 { public void actionPerformed(ActionEvent e) { if(e.getSource()==clear) { //System.out.println("hello world"); data=" "; windows.setText(data); } if(e.getSource()==data0) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data1) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data2) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data3) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data4) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data5) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data6) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data7) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data8) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==data9) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==datajia) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==datajian) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==datacheng) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==datachu) { data+=e.getActionCommand(); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } if(e.getSource()==databack) { int lengthofdata=data.length(); if(lengthofdata==1) ; else lengthofdata--; data=data.substring(0,lengthofdata); windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); //System.out.println(data); } if(e.getSource()==dataequl) { data+=e.getActionCommand(); windows.setText(data); int n=data.length(); String memorial; int fore=1,end,lengthofnumber=0,lengthofsignal=0; double now; for(int i=0;i<n;i++) //以运算符号作为标志,将数字以及运算符号分别入栈,并进行相应的处理 if(data.charAt(i)=='+'||data.charAt(i)=='-'||data.charAt(i)=='*'||data.charAt(i)=='/'||data.charAt(i)=='=') { end=i; memorial=data.substring(fore, end); //将数字抽离出来 // System.out.println(fore+" "+end+" number:"+memorial+" signal:"+data.charAt(i)); number[lengthofnumber++]=Integer.parseInt(memorial); //将数字入栈 if(lengthofsignal==0) //第一次时, { signal[lengthofsignal++]=data.charAt(i); //lengthofsignal++; fore=i+1; continue; } if(signal[lengthofsignal-1]=='*') //如果是乘法,则提前运算 { now=number[--lengthofnumber]; //乘数出栈 double before=number[--lengthofnumber]; //被乘数出栈 now=before*now; number[lengthofnumber++]=now; //运算结果进栈 lengthofsignal--; //符号栈减一 } else if(signal[lengthofsignal-1]=='/') //如果是除法,则提前运算 { now=number[--lengthofnumber]; //除数出栈 double before=number[--lengthofnumber]; //被除数出栈 now=before/now; number[lengthofnumber++]=now; //运算结果进栈 lengthofsignal--; //符号栈减一 } //System.out.println(number[lengthofnumber]); signal[lengthofsignal]=data.charAt(i); lengthofsignal++; fore=i+1; } lengthofsignal--; //System.out.println("lengthofnumber:"+lengthofnumber+" lengthofsignal:"+lengthofsignal); //for(int i=0;i<lengthofnumber;i++) //System.out.println("number["+i+"]"+number[i]); //for(int i=0;i<lengthofsignal;i++) //System.out.println("signal["+i+"]"+signal[i]); //int k=0; while(lengthofsignal!=0) //计算剩余加减法 { //System.out.println("hello world"+lengthofsignal); if(signal[lengthofsignal-1]=='-') //判断是否是减法 { //System.out.println("test:"+lengthofsignal); now=number[--lengthofnumber]; //减数出栈 double before=number[--lengthofnumber]; //被减数判断 now=before-now; number[lengthofnumber++]=now; //将结果入栈 lengthofsignal--; //符号出栈 //System.out.println("values:"+now); } else if(signal[lengthofsignal-1]=='+') //判断是否是加法 { //System.out.println("test:"+lengthofsignal); now=number[--lengthofnumber]; //加数出栈 double before=number[--lengthofnumber]; //被加数出栈 now=before+now; number[lengthofnumber++]=now; //将结果入栈 lengthofsignal--; //符号出栈 //System.out.println("values:"+now); } } String values=""+number[0]; //System.out.println(values); data+=values; windows.setText(data); windows.setFont(new Font("宋体",Font.BOLD,20)); } } } public static void main(String[] args) { new Calculator(); } }
结果: