基于JAVA語言的計算器圖形界面設計.doc
題目:簡易計算器的設計一、設計概要1、設計內(nèi)容計算器的設計使用圖形用戶界面實現(xiàn),能夠進行簡單的加、減、乘、除四則計算;參與運算的數(shù)字通過點擊按鈕輸入。2、程序流程圖開始創(chuàng)建界面框架創(chuàng)建文本框方法創(chuàng)建面板方法創(chuàng)建按鈕方法添加按鈕到面板創(chuàng)建數(shù)字按鈕對象創(chuàng)建運算符號按鈕對象運算與判斷?NY結(jié)束 2、 詳細設計import java.awt.BorderLayout; /導入AWT頁面設置類import java.awt.Button; /導入AWT按鈕類import java.awt.Frame;import java.awt.GridLayout;import java.awt.Panel; import java.awt.TextField;import java.awt.event.MouseEvent;import java.awt.event.MouseListener; /導入AWT鼠標監(jiān)視器import java.awt.event.WindowEvent;import java.awt.event.WindowListener;/導入AWT窗口監(jiān)視器/創(chuàng)建計算器的主窗口,該類含有main函數(shù)public class Calculator public static void main(String args) /創(chuàng)建Frame對象Frame f = new Frame("Calculator");/創(chuàng)建文本框,用于接收數(shù)字和操作final TextField tf = new TextField();/聲明一個面板用于存放按鈕Panel p = new Panel();f.setLayout(new BorderLayout();/將文本框放在北部f.add(tf, BorderLayout.NORTH);/將按鈕放于中部f.add(p, BorderLayout.CENTER);/設置布局模式p.setLayout(new GridLayout(4, 4);/創(chuàng)建16個按鈕Button one = new Button("1");Button two = new Button("2");Button three = new Button("3");Button four = new Button("4");Button five = new Button("5");Button six = new Button("6");Button seven = new Button("7");Button eight = new Button("8");Button nine = new Button("9");Button zero = new Button("0");Button add = new Button("+");Button sub = new Button("-");Button mul = new Button("*");Button div = new Button("/");Button decimal = new Button(".");final Button equal = new Button("=");/將按鈕添加到Pp.add(one);p.add(two);p.add(three);p.add(four);p.add(five);p.add(six);p.add(seven);p.add(eight);p.add(nine);p.add(zero);p.add(add);p.add(sub);p.add(mul);p.add(div);p.add(decimal);p.add(equal);final String ary = "", null, ""/該類為主類創(chuàng)建數(shù)字按鈕對象class NumMouseListener implements MouseListenerpublic void mouseClicked(MouseEvent e) String num = (Button)e.getSource().getLabel().trim();if(ary1 = null) ary0 = ary0 + "" + num; else ary2 = ary2 + "" + num; tf.setText(tf.getText() + "" + num);public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mousePressed(MouseEvent e)public void mouseReleased(MouseEvent e)/為每個按鈕添加事件one.addMouseListener(new NumMouseListener();two.addMouseListener(new NumMouseListener();three.addMouseListener(new NumMouseListener();four.addMouseListener(new NumMouseListener();five.addMouseListener(new NumMouseListener();six.addMouseListener(new NumMouseListener();seven.addMouseListener(new NumMouseListener();eight.addMouseListener(new NumMouseListener();nine.addMouseListener(new NumMouseListener();zero.addMouseListener(new NumMouseListener();/該類為主類創(chuàng)建運算符號按鈕對象class Operatotr MouseListener implements MouseListenerpublic void mouseClicked(MouseEvent e) if(e.getSource() = equal)try long result = calculate(Integer.parseInt(ary0), Integer.parseInt(ary2), ary1);tf.setText(String.valueOf(result);ary0 = tf.getText();ary1 = null;ary2 = ""catch (Exception e1) e1.printStackTrace(); else ary1 = (Button) e.getSource().getLabel();if(ary0.length() > 0) ary2 = "" elseary0 = String.valueOf(tf.getText(); tf.setText(""); private long calculate(int i, int j, String operator) throws Exception if(operator.equals("+")return i + j; else if(operator.equals("-")return i - j; else if(operator.equals("*")return i * j;else if(operator.equals("/")return i / j;elsethrow new Exception("Invalid operator.(Only +-/* accepted"); public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mousePressed(MouseEvent e)public void mouseReleased(MouseEvent e)add.addMouseListener(new OperatotrMouseListener();mul.addMouseListener(new OperatotrMouseListener();div.addMouseListener(new OperatotrMouseListener();sub.addMouseListener(new OperatotrMouseListener();equal.addMouseListener(new OperatotrMouseListener();f.addWindowListener(new WindowListener()public void windowClosing(WindowEvent e) System.exit(0); public void windowActivated(WindowEvent e)public void windowClosed(WindowEvent e)public void windowDeactivated(WindowEvent e)public void windowDeiconified(WindowEvent e)public void windowIconified(WindowEvent e)public void windowOpened(WindowEvent e);f.setVisible(true); /使界面可現(xiàn)f.setBounds(200, 200, 250, 250); /設置界面的位置三、運行調(diào)試與分析討論1、乘法計算,運算結(jié)果如圖2、除法計算,運算結(jié)果如圖結(jié)果分析:由計算結(jié)果可以看出,計算器可以完成簡單的加、減、乘、除運算,但計算器不具有清零的效果。四、總結(jié)與體會(設計遇到的問題及解決辦法,通過設計學到了哪些新知識, 鞏固了哪些知識,有哪些提高。)計算器設計中遇到了AWT方法的使用、鼠標響應事件的處理等問題,面對這些問題,我通過和同學商量,以及到圖書館借與java語言相關(guān)的書籍,最終解決了問題。通過計算器的設計我學會了AWT方法的使用、鼠標響應事件等新知識,鞏固了多線程、類的繼承、接口等知識點。這次設計既鍛煉了解決問題的能力,又深刻的認識和理解了java的基礎知識。