基于JAVA語言的計(jì)算器圖形界面設(shè)計(jì).doc
《基于JAVA語言的計(jì)算器圖形界面設(shè)計(jì).doc》由會員分享,可在線閱讀,更多相關(guān)《基于JAVA語言的計(jì)算器圖形界面設(shè)計(jì).doc(7頁珍藏版)》請?jiān)谘b配圖網(wǎng)上搜索。
題目:簡易計(jì)算器的設(shè)計(jì)一、設(shè)計(jì)概要1、設(shè)計(jì)內(nèi)容計(jì)算器的設(shè)計(jì)使用圖形用戶界面實(shí)現(xiàn),能夠進(jìn)行簡單的加、減、乘、除四則計(jì)算;參與運(yùn)算的數(shù)字通過點(diǎn)擊按鈕輸入。2、程序流程圖開始創(chuàng)建界面框架創(chuàng)建文本框方法創(chuàng)建面板方法創(chuàng)建按鈕方法添加按鈕到面板創(chuàng)建數(shù)字按鈕對象創(chuàng)建運(yùn)算符號按鈕對象運(yùn)算與判斷?NY結(jié)束 2、 詳細(xì)設(shè)計(jì)import java.awt.BorderLayout; /導(dǎo)入AWT頁面設(shè)置類import java.awt.Button; /導(dǎo)入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; /導(dǎo)入AWT鼠標(biāo)監(jiān)視器import java.awt.event.WindowEvent;import java.awt.event.WindowListener;/導(dǎo)入AWT窗口監(jiān)視器/創(chuàng)建計(jì)算器的主窗口,該類含有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);/設(shè)置布局模式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)建運(yùn)算符號按鈕對象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); /設(shè)置界面的位置三、運(yùn)行調(diào)試與分析討論1、乘法計(jì)算,運(yùn)算結(jié)果如圖2、除法計(jì)算,運(yùn)算結(jié)果如圖結(jié)果分析:由計(jì)算結(jié)果可以看出,計(jì)算器可以完成簡單的加、減、乘、除運(yùn)算,但計(jì)算器不具有清零的效果。四、總結(jié)與體會(設(shè)計(jì)遇到的問題及解決辦法,通過設(shè)計(jì)學(xué)到了哪些新知識, 鞏固了哪些知識,有哪些提高。)計(jì)算器設(shè)計(jì)中遇到了AWT方法的使用、鼠標(biāo)響應(yīng)事件的處理等問題,面對這些問題,我通過和同學(xué)商量,以及到圖書館借與java語言相關(guān)的書籍,最終解決了問題。通過計(jì)算器的設(shè)計(jì)我學(xué)會了AWT方法的使用、鼠標(biāo)響應(yīng)事件等新知識,鞏固了多線程、類的繼承、接口等知識點(diǎn)。這次設(shè)計(jì)既鍛煉了解決問題的能力,又深刻的認(rèn)識和理解了java的基礎(chǔ)知識。- 1.請仔細(xì)閱讀文檔,確保文檔完整性,對于不預(yù)覽、不比對內(nèi)容而直接下載帶來的問題本站不予受理。
- 2.下載的文檔,不會出現(xiàn)我們的網(wǎng)址水印。
- 3、該文檔所得收入(下載+內(nèi)容+預(yù)覽)歸上傳者、原創(chuàng)作者;如果您是本文檔原作者,請點(diǎn)此認(rèn)領(lǐng)!既往收益都?xì)w您。
下載文檔到電腦,查找使用更方便
9.9 積分
下載 |
- 配套講稿:
如PPT文件的首頁顯示word圖標(biāo),表示該P(yáng)PT已包含配套word講稿。雙擊word圖標(biāo)可打開word文檔。
- 特殊限制:
部分文檔作品中含有的國旗、國徽等圖片,僅作為作品整體效果示例展示,禁止商用。設(shè)計(jì)者僅對作品中獨(dú)創(chuàng)性部分享有著作權(quán)。
- 關(guān) 鍵 詞:
- 基于 JAVA 語言 計(jì)算器 圖形 界面設(shè)計(jì)
鏈接地址:http://ioszen.com/p-6669717.html