import java.awt. * ; import java.awt.event. * ; import javax.swing. * ; public class Calculator extends Frame implements ActionListener ... { static MenuBar mb = new MenuBar();//添加菜单 static Menu menu = new Menu("Help"); static MenuItem about = new MenuItem("About Calculator 1.0"); static Calculator frame = new Calculator(); static JPanel panel = new JPanel(new GridLayout(4, 4, 5, 5)); static JPanel panel2 = new JPanel(new GridLayout(1, 2, 5, 5)); static JButton[] button = new JButton[18]; static JLabel label = new JLabel("0.", JLabel.RIGHT); StringBuffer buf = new StringBuffer(); static String state = new String("Clean");//用来记录上一次的操作 //static boolean pmmdState = false; //用来记录加减乘除过没有,默认没有 static boolean pointState = false; //用来指示小数点状态,默认没按小数点 static boolean zeroState = true; //用来指示0在首位的状态,true表示在首位 static boolean zeroState2 = false; //用来记录按了一次0键没有,默认没按 static boolean numState = false; //指示在小数点之前按了数字键没有,默认没按 static boolean signState = true; //指示正负号, true为正, false为负. static boolean likeEqual = false; //指示按加减乘除时该不该计算出结果.默认不该 static boolean keyLock = false; //按键锁,当发生除0异常时,锁住除Clean键以外的所有键,默认没锁 String str = new String(); double num1 = 0, num2 = 0; public static void main(String[] args) ...{ int i; menu.add(about);//菜单 mb.add(menu); about.addActionListener(frame); frame.setMenuBar(mb); frame.setTitle("Calculator"); frame.setLayout(null); frame.setSize(290, 320); frame.setVisible(true); frame.setResizable(false); //若程序无法显示完整的图形界面,可将它设为true frame.addWindowListener(new WindowAdapter() ...{ public void windowClosing(WindowEvent e) ...{ System.exit(0); } }); panel.setBounds(20, 130, 250, 165); panel2.setBounds(20, 90, 250, 33); label.setBounds(20, 50, 250, 35); label.setFont(new Font("result", Font.PLAIN, 18)); label.setOpaque(true); label.setBackground(new Color(125, 125, 180)); Font font = new Font("Times New Roman", Font.PLAIN, 16); for(i = 0; i < 18; i++) ...{ button[i] = new JButton(); button[i].setFont(font); } for(i = 1; i < 10; i++) ...{ button[i].setText(Integer.toString(i)); panel.add(button[i]); } button[0].setText("0"); button[10].setText("+/-"); button[11].setText("."); panel.add(button[0]); panel.add(button[10]); panel.add(button[11]); button[12].setText("+"); button[13].setText("-"); button[14].setText("*"); button[15].setText("/"); panel.add(button[12], 3); panel.add(button[13], 7); panel.add(button[14], 11); panel.add(button[15], 15); button[16].setText("Clean"); button[17].setText("="); panel2.add(button[16], 0); panel2.add(button[17], 1); frame.add(label); frame.add(panel); frame.add(panel2); for(i = 0; i < 18; i++) ...{ button[i].addActionListener(frame); button[i].setActionCommand(button[i].getText()); } } public void actionPerformed(ActionEvent e) ...{ int i; String[] estate = ...{"+", "-", "*", "/", "="}; for(i = 1; i < 10; i++)//1-9数字键 ...{ if(e.getActionCommand().equals(Integer.toString(i)) && keyLock == false) ...{ if(pointState == false) ...{ str = new String(buf.append(i) + "."); label.setText(str); numState = true; break; } else ...{ str = new String(buf.append(i)); label.setText(str); numState = true; break; } } } if(e.getActionCommand().equals("0") && keyLock == false)//0键 ...{ zeroState2 = true; if(zeroState == true && numState == false && pointState == false)//这句也很关键 ...{ label.setText("0."); buf = new StringBuffer("0");//这条语句关系到后面的异常处理 } else ...{ zeroState = false; if(pointState == false) ...{ str = new String(buf.append(0) + "."); label.setText(str); numState = true; } else ...{ str = new String(buf.append(0)); label.setText(str); } } } if(e.getActionCommand().equals(".") && keyLock == false)//小数点键 if(pointState == false) ...{ if(numState == false) ...{ if(zeroState2 == true)//判断按了0键没有 buf.append("."); else buf.append("0."); label.setText(new String(buf));//这句必须要有 pointState = true; } else ...{ buf.append("."); label.setText(new String(buf)); pointState = true; } } if(e.getActionCommand().equals("Clean"))//Clean键 ...{ buf = new StringBuffer(); label.setText("0."); state = new String("Clean"); //记录上次操作 pointState = false; zeroState = true; zeroState2 = false; numState = false; signState = true; likeEqual = false; keyLock = false; //撤消按键锁 } for(i = 0; i < 4; i++)//+-*/运算 ...{ activeLikeEqual(); if(e.getActionCommand().equals(estate[i]) && keyLock == false) ...{ if(likeEqual == false) ...{ num1 = num2 = Double.parseDouble(label.getText()); buf = new StringBuffer(); state = new String(estate[i]); pointState = false;//将小数点再次设置为未使用 zeroState = true; zeroState2 = false; numState = false; signState = true; break; } else ...{ resultOperate(); renewState(); state = new String(estate[i]); num2 = num1; //1+2+=之类的操作 } } } if(e.getActionCommand().equals("=") && keyLock == false)//=运算 ...{ if(state.equals("Clean")) ...{ label.setText(label.getText());//又解决一个bug -> 按3=显示0. buf = new StringBuffer(); state = new String("Clean"); //记录上次操作 pointState = false; zeroState = true; zeroState2 = false; numState = false; signState = true; } else ...{ resultOperate(); renewState(); } } if(e.getSource() == about) ...{ JOptionPane.showMessageDialog(frame, "Version: Calculator 1.0 Author: Mr. tan All Rights Reserved", "About Calculator 1.0", JOptionPane.INFORMATION_MESSAGE); } if(e.getActionCommand().equals("+/-") && keyLock == false) ...{ if(zeroState == true && numState == false && pointState == false) ...{ } else ...{ if(signState == true) ...{ buf.insert(0, '-'); if(pointState == false) label.setText(new String(buf) + "."); else label.setText(new String(buf)); signState = false; } else ...{ buf.deleteCharAt(0); if(pointState == false) label.setText(new String(buf) + "."); else label.setText(new String(buf)); signState = true; } } } } //计算结果的方法,因为有时候按+-*/号时也需要计算出结果,所以把它写在一起节省代码。 public void resultOperate() ...{ if(buf.length() > 0) ...{ str = new String(buf); //考虑这用buf还是label.getText() num2 = Double.parseDouble(str); } //else num2 = num1; //1+2+=之类的操作 if(state.equals("+")) num1 += num2; else if(state.equals("-")) num1 -= num2; else if(state.equals("*")) num1 *= num2; if(state.equals("/")) //对/要进行异常处理 ...{ try ...{ if(num2 != 0) ...{ num1 /= num2; } else throw new ArithmeticException(); } catch(ArithmeticException ZeroException) ...{ label.setText("I think your IQ is only 80!!"); keyLock = true;//启动按键锁 return;//防止出现9/0=9这样的bug } } StringBuffer calculateResult = new StringBuffer(Double.toString(num1)); if(calculateResult.charAt(calculateResult.length() - 2) == '.' && calculateResult.charAt(calculateResult.length() - 1) == '0') calculateResult.deleteCharAt(calculateResult.length() - 1); label.setText(new String(calculateResult)); } public void activeLikeEqual()//激活+-*/的计算功能 ...{ if((state.equals("+") || state.equals("-") || state.equals("*") || state.equals("/")) && buf.length() > 0) likeEqual = true; } public void renewState() ...{ buf = new StringBuffer(); pointState = false; zeroState = true; zeroState2 = false; numState = false; signState = true; likeEqual = false; }}