Java-Calculator
此代码仅供娱乐,Java实训课跟同学打赌一小时内完成的图形计算器。
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class Calculator extends JFrame { 6 7 8 private ButtonPane buttonPane = new ButtonPane(); 9 private TextPane textPane = new TextPane(); 10 private numPane numPane = new numPane(); 11 private int hkw=0,num=0; 12 private double operator1, operator2,temp,T; 13 14 public static void main(String[] args) { 15 Calculator calculator = new Calculator(); 16 17 calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 calculator.pack(); 19 calculator.setLocationRelativeTo(null); 20 calculator.setVisible(true); 21 } 22 23 private Calculator() { 24 add(textPane, BorderLayout.NORTH); 25 add(numPane, BorderLayout.CENTER); 26 add(buttonPane, BorderLayout.SOUTH); 27 } 28 29 private void parseOperators(double t1) { 30 31 if(num==0) 32 operator1 = t1; 33 else if(num!=0) 34 operator2 =t1; 35 num++; 36 } 37 38 private void add() { 39 hkw=1; 40 41 } 42 43 private void subtract() { 44 hkw=2; 45 46 } 47 48 private void multiply() { 49 hkw=3; 50 51 } 52 53 private void divide() { 54 hkw=4; 55 56 } 57 private void equal() { 58 if(hkw==1) 59 {T=operator1 + operator2; 60 setAnswer(T);} 61 else if(hkw==2) 62 {T=operator1 - operator2; 63 setAnswer(T);} 64 else if(hkw==3) 65 {T=operator1 * operator2; 66 setAnswer(T);} 67 else if(hkw==4) 68 {T=operator1 / operator2; 69 setAnswer(T);} 70 71 } 72 73 private void setAnswer(double value) { 74 textPane.txtResult.setText(value + ""); 75 operator1=T; 76 num=1; 77 } 78 79 // The panel containing our operation buttons 80 private class ButtonPane extends JPanel { 81 82 private JButton btnAdd = new JButton("+"); 83 private JButton btnSubtract = new JButton("-"); 84 private JButton btnMultiply = new JButton("*"); 85 private JButton btnDivide = new JButton("/"); 86 private JButton btnequl = new JButton("="); 87 private ButtonListener listener = new ButtonListener(); // Just one listener 88 89 private ButtonPane() { 90 btnAdd.addActionListener(listener); 91 btnSubtract.addActionListener(listener); 92 btnMultiply.addActionListener(listener); 93 btnDivide.addActionListener(listener); 94 btnequl.addActionListener(listener); 95 96 add(btnAdd); 97 add(btnSubtract); 98 add(btnMultiply); 99 add(btnDivide); 100 add(btnequl); 101 } 102 103 private class ButtonListener implements ActionListener { 104 105 106 public void actionPerformed(ActionEvent e) { 107 108 109 if (e.getSource() == btnAdd) { 110 Calculator.this.add(); 111 } else if (e.getSource() == btnSubtract) { 112 Calculator.this.subtract(); 113 } else if (e.getSource() == btnMultiply) { 114 Calculator.this.multiply(); 115 } else if (e.getSource() == btnDivide) { 116 Calculator.this.divide(); 117 } 118 else if (e.getSource() == btnequl) { 119 equal(); 120 } 121 } 122 } // end ButtonListener class 123 124 } // end ButtonPane class 125 126 127 public static class TextPane extends JPanel { 128 129 private JLabel labelNum1 = new JLabel("请输入:"); 130 public JTextField txtResult = new JTextField("0",15); 131 private TextPane() { 132 add(labelNum1); 133 add(txtResult); 134 135 txtResult.setEnabled(false); 136 txtResult.setDisabledTextColor(Color.BLACK); 137 } 138 139 } // end TextPane class 140 private class numPane extends JPanel { 141 142 private JButton yi = new JButton("1"); 143 private JButton er = new JButton("2"); 144 private JButton san = new JButton("3"); 145 private JButton si = new JButton("4"); 146 private JButton wu = new JButton("5"); 147 private JButton liu = new JButton("6"); 148 private JButton qi = new JButton("7"); 149 private JButton ba = new JButton("8"); 150 private JButton jiu = new JButton("9"); 151 private JButton ling = new JButton("0"); 152 153 private ButtonListener listener = new ButtonListener(); // Just one listener 154 155 private numPane() { 156 yi.addActionListener(listener); 157 er.addActionListener(listener); 158 san.addActionListener(listener); 159 si.addActionListener(listener); 160 wu.addActionListener(listener); 161 liu.addActionListener(listener); 162 qi.addActionListener(listener); 163 ba.addActionListener(listener); 164 jiu.addActionListener(listener); 165 ling.addActionListener(listener); 166 167 168 add(yi); 169 add(er); 170 add(san); 171 add(si); 172 add(wu); 173 add(liu); 174 add(qi); 175 add(ba); 176 add(jiu); 177 add(ling); 178 179 } 180 181 private class ButtonListener implements ActionListener { 182 183 184 public void actionPerformed(ActionEvent e) { 185 186 187 if (e.getSource() == yi) { 188 textPane.txtResult.setText("1"); 189 temp=1; 190 191 } else if (e.getSource() == er) { 192 textPane.txtResult.setText("2"); 193 temp=2; 194 195 } else if (e.getSource() == san) { 196 textPane.txtResult.setText("3"); 197 temp=3; 198 199 } else if (e.getSource() == si) { 200 textPane.txtResult.setText("4"); 201 temp=4; 202 203 } 204 else if (e.getSource() == wu) { 205 textPane.txtResult.setText("5"); 206 temp=5; 207 208 } 209 else if (e.getSource() == liu) { 210 textPane.txtResult.setText("6"); 211 temp=6; 212 213 } 214 else if (e.getSource() == qi) { 215 textPane.txtResult.setText("7"); 216 temp=7; 217 218 } 219 else if (e.getSource() == ba) { 220 textPane.txtResult.setText("8"); 221 temp=8; 222 223 } 224 else if (e.getSource() == jiu) { 225 textPane.txtResult.setText("9"); 226 temp=9; 227 228 } 229 else if (e.getSource() == ling) { 230 textPane.txtResult.setText("0"); 231 temp=0; 232 233 } 234 235 parseOperators(temp); 236 } 237 } // end ButtonListener class 238 239 } // end ButtonPane class 240 241 } // end Calculator