学习java Swing组件后,就想写一个简单的计算器。在博客记录下,代码都经过测试。

1 package cn.edu.usst; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Container; 6 import java.awt.Dimension; 7 import java.awt.GridLayout; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.util.Stack; 11 12 import javax.swing.JButton; 13 import javax.swing.JFrame; 14 import javax.swing.JPanel; 15 import javax.swing.JTextField; 16 import javax.swing.SwingConstants; 17 import javax.swing.WindowConstants; 18 19 public class Calculator extends JFrame implements ActionListener { 20 private static final long serialVersionUID = 1L; 21 String []arr= {"7","8","9","+","4","5","6","-", 22 "1","2","3","*",".","0","=","/"}; 23 JButton b[]=new JButton[16]; 24 JTextField jt; 25 String input="";//输入的字符串 26 public Calculator() { 27 Container c=getContentPane(); 28 JPanel jp1=new JPanel(); 29 JPanel jp2=new JPanel(); 30 JPanel jp3=new JPanel(); 31 JButton b1=new JButton("清零"); 32 JButton b2=new JButton("退格"); 33 GridLayout g=new GridLayout(4,4,5,5); 34 jp1.setLayout(new BorderLayout()); 35 jp2.setLayout(g); 36 37 jp3.setLayout(new GridLayout(1, 2,5,5)); 38 39 jt=new JTextField(); 40 jt.setPreferredSize(new Dimension(150,30)); 41 jt.setHorizontalAlignment(SwingConstants.LEFT); 42 c.add(jp1,BorderLayout.NORTH); 43 jp1.add(jt,BorderLayout.WEST); 44 jp1.add(jp3,BorderLayout.EAST); 45 jp3.add(b1); 46 jp3.add(b2); 47 b1.setBackground(Color.gray); 48 b2.setBackground(Color.gray); 49 b1.addActionListener(this); 50 b2.addActionListener(this); 51 for(int i=0;i<arr.length;i++) { 52 b[i]=new JButton(arr[i]); 53 jp2.add(b[i]); 54 b[i].addActionListener(this); 55 } 56 57 c.add(jp2,BorderLayout.CENTER); 58 setVisible(true); 59 60 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 61 setSize(300, 250); 62 setTitle("计算器"); 63 64 65 } 66 @Override 67 public void actionPerformed(ActionEvent e) { 68 69 // TODO Auto-generated method stub 70 int t=0; 71 String s=e.getActionCommand(); 72 if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) { 73 input+=" "+s+" "; //如果碰到运算符,就在运算符前后分别加一个空格, 74 //为后面的分解字符串做准备 75 76 }else if(s.equals("清零")) { 77 input=""; 78 }else if(s.equals("退格")) { 79 if((input.charAt(input.length()-1))==' ') { //检测字符串的最后一个字符是否为空格, 80 input=input.substring(0,input.length()-3);//如果是则删除末尾3个字符,否则删除 81 //1个字符 82 }else { 83 input=input.substring(0,input.length()-1); 84 } 85 } 86 else if(s.equals("=")) { 87 input=compute(input); 88 jt.setText(input); 89 input=""; 90 t=1; 91 } 92 else 93 input += s; 94 if(t==0) { 95 jt.setText(input); 96 } 97 } 98 private String compute(String str) { 99 // TODO Auto-generated method stub 100 String array[]; 101 array=str.split(" "); 102 Stack<Double> s=new Stack<Double>(); 103 Double a=Double.parseDouble(array[0]); 104 s.push(a); 105 for(int i=1;i<array.length;i++) { 106 if(i%2==1) { 107 if(array[i].compareTo("+")==0) 108 { 109 double b= Double.parseDouble(array[i+1]); 110 s.push(b); 111 } 112 if(array[i].compareTo("-")==0) 113 { 114 double b= Double.parseDouble(array[i+1]); 115 s.push(-b); 116 } 117 if(array[i].compareTo("*")==0) 118 { 119 double b= Double.parseDouble(array[i+1]); 120 121 double c=s.pop(); 122 123 c*=b; 124 s.push(c); 125 126 127 } 128 if(array[i].compareTo("/")==0) 129 { 130 double b= Double.parseDouble(array[i+1]); 131 132 double c=s.peek(); 133 s.pop(); 134 c/=b; 135 s.push(c); 136 137 138 } 139 } 140 } 141 double sum=0; 142 while(!s.isEmpty()) { 143 sum+=s.pop(); 144 145 } 146 String result=String.valueOf(sum); 147 return result; 148 } 149 public static void main(String[] args) { 150 // TODO Auto-generated method stub 151 new Calculator(); 152 } 153 154 155 156 }
计算器界面如图所示:
测试计算功能: