calculator

  1 /*
2 * the calculator
3 * function:+,-,*,/
4 * not implement 1+2+4+5;
5 * implement 1+2=3+5=9*5.....
6 */
7
8 import java.awt.*;
9 import java.awt.event.*;
10
11 import javax.swing.*;
12
13
14
15 public class Calculator extends JFrame implements ActionListener {
16
17
18 JPanel panel1,panel2;
19 JTextField textfield;
20 JButton button1,button2,button3,button4,button5,button6,
21 button7,button8,button9,button10,button11,button12,button13,button14,button15,button16,button17;
22 String msg="";
23
24
25 public Calculator(){
26 this.setName("Calculator");
27 panel1 = new JPanel();
28 panel2 = new JPanel();
29 textfield = new JTextField(20);
30 textfield.setSize(200,50);
31 textfield.setEditable(false);
32 button1 = new JButton("1");
33 button2 = new JButton("2");
34 button3 = new JButton("3");
35 button4 = new JButton("+");
36 button5 = new JButton("4");
37 button6 = new JButton("5");
38 button7 = new JButton("6");
39 button8 = new JButton("-");
40 button9 = new JButton("7");
41 button10 = new JButton("8");
42 button11 = new JButton("9");
43 button12 = new JButton("*");
44 button13 = new JButton("0");
45 button14 = new JButton(".");
46 button15 = new JButton("=");
47 button16 = new JButton("/");
48 button17 = new JButton("clear");
49 //set the panel2's layout is GridLayout which default is flowlayout
50 panel2.setLayout(new GridLayout(4,4,2,2));
51 //add the textfield and button17 to the panel1
52 panel1.add(textfield);
53 panel1.add(button17);
54
55 panel2.add(button1);
56 panel2.add(button2);
57 panel2.add(button3);
58 panel2.add(button4);
59 panel2.add(button5);
60 panel2.add(button6);
61 panel2.add(button7);
62 panel2.add(button8);
63 panel2.add(button9);
64 panel2.add(button10);
65 panel2.add(button11);
66 panel2.add(button12);
67 panel2.add(button13);
68 panel2.add(button14);
69 panel2.add(button15);
70 panel2.add(button16);
71 this.add(panel1,BorderLayout.NORTH);
72 this.add(panel2,BorderLayout.SOUTH);
73
74 //add actionlistener to all the buttons
75
76 button1.addActionListener(this);
77 button2.addActionListener(this);
78 button3.addActionListener(this);
79 button4.addActionListener(this);
80 button5.addActionListener(this);
81 button6.addActionListener(this);
82 button7.addActionListener(this);
83 button8.addActionListener(this);
84 button9.addActionListener(this);
85 button10.addActionListener(this);
86 button11.addActionListener(this);
87 button12.addActionListener(this);
88 button13.addActionListener(this);
89 button14.addActionListener(this);
90 button15.addActionListener(this);
91 button16.addActionListener(this);
92 button17.addActionListener(this);
93 button17.addMouseListener(new Mouselistener());
94
95 setSize(250,250);
96 pack();
97 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
98 setVisible(true);
99 }
100 //inner class the mouslisetener
101 public class Mouselistener extends MouseAdapter
102 {
103 //override mouseenter method
104 public void mouseEntered(MouseEvent e)
105 {
106 button17.setForeground(Color.red);
107 button17.setBackground(Color.blue);
108 repaint();
109 }
110 //override mouseexited method;
111 public void mouseExited(MouseEvent e)
112 {
113 button17.setForeground(Color.green);
114 button17.setBackground(Color.black);
115 repaint();
116 }
117
118 }
119 // the main method
120 public static void main(String[] args){
121 JFrame jf = new Calculator();
122 }
123 //implement the interface actionlisetener's abstranct method actionPerformed
124 public void actionPerformed(ActionEvent ex) {
125 // judge which button action is done
126 if(ex.getSource().equals(button1))
127 {
128 msg = msg+"1";
129 }
130 if(ex.getSource().equals(button2))
131 {
132 msg = msg+"2";
133 }
134 if(ex.getSource().equals(button3))
135 {
136 msg = msg+"3";
137 }
138 if(ex.getSource().equals(button4))
139 {
140 //call +
141 msg = msg+"+";
142 }
143 if(ex.getSource().equals(button5))
144 {
145 msg = msg+"4";
146 }
147 if(ex.getSource().equals(button6))
148 {
149 msg = msg+"5";
150 }
151
152 if(ex.getSource().equals(button7))
153 {
154 msg = msg+"6";
155 }
156 if(ex.getSource().equals(button8))
157 {
158 msg = msg+"-";
159 }
160 if(ex.getSource().equals(button9))
161 {
162 msg = msg+"7";
163 }
164 if(ex.getSource().equals(button10))
165 {
166 msg = msg+"8";
167 }
168 if(ex.getSource().equals(button11))
169 {
170 msg = msg+"9";
171 }
172 if(ex.getSource().equals(button12))
173 {
174 msg = msg+"*";
175 }
176 if(ex.getSource().equals(button13))
177 {
178 msg = msg+"0";
179 }
180
181 if(ex.getSource().equals(button14))
182 {
183 msg = msg+".";
184 }
185 if(ex.getSource().equals(button15))
186 {
187 // when the button which is = it wile call the calculator method
188 double total = this.calculator(msg,0);
189 msg= String.valueOf(total);
190
191 }
192 if(ex.getSource().equals(button16))
193 {
194 msg = msg+"/";
195 }
196 // the clear button is been done
197 if(ex.getSource().equals(button17))
198 {
199 msg = "";
200 }
201 textfield.setText(msg);
202 }
203 // the calculator method which is to calculator the result
204 public static double calculator(String a,double total)
205 {
206 int num=0;
207 double a1=0;
208 double a2=0;
209 char c=' ';
210 // find the + ;-;*;/
211 for(int i=0;i<a.length();i++){
212 if(a.charAt(i)=='+'){
213 num = i;
214 c ='+';
215 }
216 if(a.charAt(i)=='-'){
217 num = i;
218 c ='-';
219 }
220 if(a.charAt(i)=='*'){
221 num = i;
222 c ='*';
223 }
224 if(a.charAt(i)=='/'){
225 num = i;
226 c ='/';
227 }
228
229
230 }
231
232 // use the string's method substring to separate the number and the mark
233
234 a1 = Double.parseDouble(a.substring(0, num));
235 a2 = Double.parseDouble(a.substring(num+1, a.length()));
236 if(c=='+'){
237 total = a1+a2;
238 }
239 if(c=='-'){
240 total = a1-a2;
241 }
242 if(c=='*'){
243 total = a1*a2;
244 }
245 if(c=='/'){
246 total = a1/a2;
247 }
248 return total;
249 }
250 }

 

posted @ 2011-12-30 16:29  天涯行客  阅读(145)  评论(0编辑  收藏  举报
天道酬勤--埋头静默--厚积薄发