版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 淮南師范學(xué)院電氣信息工程學(xué)院2013屆電子信息科學(xué)與技術(shù)專(zhuān)業(yè)課程設(shè)計(jì)報(bào)告課程設(shè)計(jì)報(bào)告題 目: 基于JAVA計(jì)算器小軟件設(shè)計(jì) 學(xué)生姓名: 廖永治 束翔 陶善慶 學(xué)生學(xué)號(hào): 0908050220 0908050229 0908050233 系 別: 電氣信息工程學(xué)院 專(zhuān) 業(yè): 電子信息科學(xué)與技術(shù) 屆 別: 2013屆 指導(dǎo)教師: 李營(yíng) 電氣信息工程學(xué)院制2012年5月 基于JAVA計(jì)算器小軟件設(shè)計(jì)學(xué)生:廖永治 束翔 陶善慶指導(dǎo)教師:李營(yíng)電氣信息工程學(xué)院 1 課程設(shè)計(jì)的任務(wù)及要求 1.1課程設(shè)計(jì)的任務(wù) (1)掌握J(rèn)AVA語(yǔ)言。 (2)創(chuàng)建一個(gè)簡(jiǎn)單的計(jì)算器,具有簡(jiǎn)單人機(jī)交互界面,易于操作,便于進(jìn)行
2、數(shù)據(jù)之間的簡(jiǎn)單運(yùn)算。 (3)設(shè)計(jì)要完成設(shè)計(jì)、編碼、測(cè)試等過(guò)程。 1.2 課程設(shè)計(jì)的要求 (1) 能夠?qū)崿F(xiàn)簡(jiǎn)單的加、減、乘、除四則運(yùn)算。 (2) 運(yùn)算結(jié)果能夠正常顯示,并能夠清零。 (3)實(shí)數(shù)運(yùn)算中小數(shù)位的精度一致。 1.3 課程設(shè)計(jì)的研究基礎(chǔ)及主要設(shè)計(jì)思想對(duì)于此計(jì)算器程序,首先要設(shè)計(jì)其GUI界面,總體界面有一個(gè)文本框,19個(gè)按鈕,總體界面用GridBagLayout布局。 文本框放置在JTextField,然后0到9以及+,-,×,÷的按鈕放置一個(gè)面板container中,添加到整體布局中,界面設(shè)計(jì)完成。布局設(shè)計(jì)好后再添加按鈕事件。對(duì)于此程序要考慮到點(diǎn)擊加、減、乘、除按鈕時(shí)
3、是否有點(diǎn)擊數(shù)字按鈕,如果是第一次點(diǎn)擊數(shù)字按鈕或者是點(diǎn)擊數(shù)字按鈕前點(diǎn)擊了加、減、乘、除按鈕則直接把數(shù)字按鈕的數(shù)值設(shè)置到文本框中,否則則應(yīng)把文本框中的內(nèi)容加上你所點(diǎn)擊按鈕的數(shù)值設(shè)置到文本框中。在進(jìn)行加、減、乘、除計(jì)算時(shí)要把點(diǎn)擊加、減、乘、除按鈕之前的數(shù)值保存下來(lái),運(yùn)算時(shí)是這個(gè)被保存下來(lái)的數(shù)值和文本框中的數(shù)值加、減、乘、除。2 總體方案制定 2.1需求分析日常生活中經(jīng)常需要用到計(jì)算器,比如科學(xué)計(jì)算、數(shù)值計(jì)算、會(huì)計(jì)業(yè)務(wù)等,在Applet中實(shí)現(xiàn)一個(gè)精巧實(shí)用的計(jì)算器,可以讓用戶(hù)在使用網(wǎng)站服務(wù)的同時(shí),可以很方便的驚醒數(shù)據(jù)運(yùn)算,可增加頁(yè)面的實(shí)用性,同時(shí)讓用戶(hù)的操作也更加方便月快捷。本項(xiàng)目需要實(shí)現(xiàn)一個(gè)具有四則運(yùn)
4、算的小型計(jì)算器,擁有加,減,乘,除功能。 2.2 總體設(shè)計(jì)(1)界面設(shè)計(jì)界面設(shè)計(jì)要求用GUI,界面設(shè)計(jì)中有用到swing組件的JTextField,用到awt中的GridLayout布局管理方式,窗體名稱(chēng)Conclutor繼承JFrame類(lèi),InsertAction實(shí)現(xiàn)ActionListener一個(gè)接口,并實(shí)現(xiàn)public void actionPerformed(ActionEvent e)方法。定義菜單、菜單條、菜單項(xiàng)、按鈕、文本框、容器,設(shè)置為private類(lèi)型。定義變量double result;/使用內(nèi)存中存儲(chǔ)的數(shù)字和結(jié)果;JTextField resultfield;/顯示計(jì)算結(jié)
5、果。使用private void addButton()方法添加按鈕并給按鈕命名;其圖形界面如下圖(1): 圖1:圖形界面(2)功能介紹 這是計(jì)算器中的一種:簡(jiǎn)單計(jì)算器,本項(xiàng)目用到了Java中的GUI界面和swing組件以及awt布局,所以簡(jiǎn)單易用,它能完成如下的幾個(gè)小功能:點(diǎn)擊按鈕中的數(shù)字鍵和運(yùn)算符鍵就能進(jìn)行簡(jiǎn)單的加、減、乘、除求模四則運(yùn)算 ;還能通過(guò)用戶(hù)自己在文本框中輸入數(shù)字,進(jìn)行簡(jiǎn)單運(yùn)算;如果輸入有誤時(shí)可以按BackSpace撤銷(xiāo)后重新輸入新數(shù)據(jù)也可以通過(guò)CE清除所有數(shù)據(jù)。 3 詳細(xì)方案設(shè)計(jì)3.1 設(shè)計(jì)所需的環(huán)境及軟件介紹(1)JDK(Java Development Kit)是Sun
6、Microsystems針對(duì)Java開(kāi)發(fā)員的產(chǎn)品。JDK是整個(gè)Java的核心,包括了Java運(yùn)行環(huán)境、Java工具和Java基礎(chǔ)類(lèi)庫(kù)。 (2)本次設(shè)計(jì)使用Eclipe開(kāi)發(fā)平臺(tái)。Eclipse 是一個(gè)開(kāi)放源代碼的、基于Java的可擴(kuò)展開(kāi)發(fā)平臺(tái)。就其本身而言,它只是一個(gè)框架和一組服務(wù), 用于通過(guò)插件組建構(gòu)建開(kāi)發(fā)環(huán)境。3.2 程序流程圖如下: 圖(2):程序設(shè)計(jì)流程圖3.3 部分代碼說(shuō)明 (1)程序的主框架設(shè)計(jì)代碼: private Container container; private GridBagLayout layout; private GridBagConstraints constr
7、aints; private JTextField resultfield;/ 計(jì)算結(jié)果顯示區(qū) private String lastCommand;/ 保存+,-,×,÷,=命令 private double result;/ 保存計(jì)算結(jié)果 private boolean start;/ 判斷是否為數(shù)字的開(kāi)始public Conclutor() /創(chuàng)建和定義計(jì)算器的圖形界面;setTitle("計(jì)算器");setSize(300, 500);setVisible(true);container = getContentPane();layout = n
8、ew GridBagLayout();container.setLayout(layout);constraints = new GridBagConstraints();constraints.gridx = 0;constraints.gridy = 0;constraints.gridwidth = 4;constraints.gridheight = 1;constraints.fill = GridBagConstraints.BOTH;constraints.weightx = 300;constraints.weighty = 300;resultfield = new JTex
9、tField(19);resultfield.setHorizontalAlignment(JTextField.RIGHT);start = true;result = 0;lastCommand = "="layout.setConstraints(resultfield, constraints);container.add(resultfield); (2)增加數(shù)字按鈕以及符號(hào)按鈕并對(duì)按鈕進(jìn)行監(jiān)聽(tīng)的代碼: ActionListener insert = new InsertAction();ActionListener command = new CommandAc
10、tion();addButton("Backspace", 0, 1, 2, 1, insert);addButton("CE", 2, 1, 2, 1, insert);addButton("7", 0, 2, 1, 1, insert);addButton("8", 1, 2, 1, 1, insert);addButton("9", 2, 2, 1, 1, insert);addButton("÷", 3, 2, 1, 1, command);addButto
11、n("4", 0, 3, 1, 1, insert);addButton("5", 1, 3, 1, 1, insert);addButton("6", 2, 3, 1, 1, insert);addButton("×", 3, 3, 1, 1, command);addButton("1", 0, 4, 1, 1, insert);addButton("2", 1, 4, 1, 1, insert);addButton("3", 2, 4, 1
12、, 1, insert);addButton("-", 3, 4, 1, 1, command);addButton(".", 0, 5, 1, 1, insert);addButton("+/-", 2, 5, 1, 1, insert); / 實(shí)現(xiàn)負(fù)數(shù)輸入addButton("0",1, 5, 1, 1, insert);addButton("+", 3, 5, 1, 1, command);addButton("=", 0, 6, 4, 1, command); (3)
13、程序運(yùn)算過(guò)程的設(shè)計(jì)代碼 :private class InsertAction implements ActionListener public void actionPerformed(ActionEvent event) String input = event.getActionCommand();if (start) resultfield.setText("");start = false;if (input.equals("+/-")/if(displayField.getText().charAt(0)!='-')resul
14、tfield.setText( "-" + resultfield.getText() );/else /displayField.setText(displayField.getText().substring(1);/return;/if (!input.equals("+/-") if (input.equals("Backspace") String str = resultfield.getText();if (str.length() > 0)resultfield.setText(str.substring(0,
15、str.length() - 1); else if (input.equals("CE") resultfield.setText("0");start = true; elseresultfield.setText(resultfield.getText() + input);private class CommandAction implements ActionListener public void actionPerformed(ActionEvent evt) String command = evt.getActionCommand();
16、if (start) lastCommand = command; else calculate(Double.parseDouble(resultfield.getText();lastCommand = command;start = true;public void calculate(double x) if (lastCommand.equals("+")result += x;else if (lastCommand.equals("-")result -= x;else if (lastCommand.equals("×
17、")result *= x;else if (lastCommand.equals("÷")result /= x;else if (lastCommand.equals("=")result = x;resultfield.setText("" + result);(4)計(jì)算器入口程序設(shè)計(jì)代碼:public static void main(String args) Conclutor calculator = new Conclutor();calculator.setDefaultCloseOperation
18、(JFrame.EXIT_ON_CLOSE);4 程序的調(diào)試與運(yùn)行結(jié)果說(shuō)明 示例1 當(dāng)乘數(shù)為0時(shí)的運(yùn)行結(jié)果如下圖(3)所示: 圖(3)示例2:43.6+23.4 預(yù)計(jì)結(jié)果 67.0 實(shí)際結(jié)果如下圖(4)所示: 圖(4) 示例3: 189/4 預(yù)計(jì)結(jié)果47.25 實(shí)際結(jié)果如下圖(5)所示: 圖(5) 示例4: 11-88 預(yù)計(jì)結(jié)果-77.0 實(shí)際結(jié)果如下圖(6)所示: 圖(6)示例5: -8*6 預(yù)計(jì)結(jié)果 -48.0 實(shí)際結(jié)果如下圖(7)所示: 圖(7) 示例 6:當(dāng)除數(shù)為0時(shí)的運(yùn)行結(jié)果如下圖(8)所示: 圖(8)5 設(shè)計(jì)總結(jié)通過(guò)此次的課程設(shè)計(jì)實(shí)踐,我們不但進(jìn)一步理解掌握了課上老師教授的知識(shí),而
19、且通過(guò)查找資料學(xué)會(huì)了課上沒(méi)有的東西,進(jìn)一步理解了面向?qū)ο蟮暮x,發(fā)現(xiàn)了很多它比面向過(guò)程突出的地方,第一次接觸了圖形界面,初步了解了GUI方面的知識(shí)。 這次課程設(shè)計(jì),我們按照課程設(shè)計(jì)的要求,。在實(shí)踐中我遇到許多的問(wèn)題,但我們都克服,最終我成功的完成了這次課程設(shè)計(jì)。從中我充分理解了Java面對(duì)對(duì)象的特性,并體會(huì)到Java與C語(yǔ)言的不同之處。在實(shí)踐的同時(shí),我又從中成長(zhǎng)了許多,特別是在對(duì)程序的編譯和調(diào)試中掌握了許多的技巧和方法,為今后的其他計(jì)算機(jī)語(yǔ)言的學(xué)習(xí)奠定了一定的基礎(chǔ)。通過(guò)編寫(xiě)這個(gè)相對(duì)復(fù)雜的程序,我們認(rèn)識(shí)解決實(shí)際問(wèn)題前應(yīng)先建立模型,再通過(guò)建立的模型使問(wèn)題簡(jiǎn)單化,從而解決問(wèn)題。這次編寫(xiě)的程序大部分是
20、通過(guò)調(diào)用各包中的方法來(lái)實(shí)現(xiàn)的,這也讓我充分理解了包和類(lèi)調(diào)用的關(guān)系。由于這次編寫(xiě)的程序相對(duì)比較復(fù)雜,所以不能一次性編寫(xiě)、調(diào)試,也由此讓我學(xué)會(huì)把程序分塊,逐個(gè)編寫(xiě)、調(diào)試。6 參考文獻(xiàn)1 王鵬,零基礎(chǔ)學(xué)JavaM,機(jī)械工業(yè)出版社,2008-1-1. 2 王鵬,何昀峰.Java Swing圖形界面開(kāi)發(fā)案例詳解M,清華大學(xué)出版社,2008-12-01.7 附錄 package cn.hn.school; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Conclutor extends JFra
21、me private Container container; private GridBagLayout layout;private GridBagConstraints constraints;private JTextField resultfield;/ 計(jì)算結(jié)果顯示區(qū) private String lastCommand;/ 保存+,-,×,÷,=命令private double result;/ 保存計(jì)算結(jié)果private boolean start;/ 判斷是否為數(shù)字的開(kāi)始public Conclutor() /創(chuàng)建和定義計(jì)算器的圖形界面;setTitle(
22、"計(jì)算器");setSize(300, 500);setVisible(true);container = getContentPane();layout = new GridBagLayout();container.setLayout(layout);constraints = new GridBagConstraints();constraints.gridx = 0;constraints.gridy = 0;constraints.gridwidth = 4;constraints.gridheight = 1;constraints.fill = GridBag
23、Constraints.BOTH;constraints.weightx = 300;constraints.weighty = 300;resultfield = new JTextField(19);resultfield.setHorizontalAlignment(JTextField.RIGHT);start = true;result = 0;lastCommand = "="layout.setConstraints(resultfield, constraints);container.add(resultfield);/增加計(jì)算器的按鈕/* * 調(diào)用監(jiān)聽(tīng)輸
24、入操作的命令; * insert 表示輸入字符; * command 表示輸入符號(hào) */ActionListener insert = new InsertAction();ActionListener command = new CommandAction();addButton("Backspace", 0, 1, 2, 1, insert);addButton("CE", 2, 1, 2, 1, insert);addButton("7", 0, 2, 1, 1, insert);addButton("8",
25、 1, 2, 1, 1, insert);addButton("9", 2, 2, 1, 1, insert);addButton("÷", 3, 2, 1, 1, command);addButton("4", 0, 3, 1, 1, insert);addButton("5", 1, 3, 1, 1, insert);addButton("6", 2, 3, 1, 1, insert);addButton("×", 3, 3, 1, 1, comman
26、d);addButton("1", 0, 4, 1, 1, insert);addButton("2", 1, 4, 1, 1, insert);addButton("3", 2, 4, 1, 1, insert);addButton("-", 3, 4, 1, 1, command);addButton(".", 0, 5, 1, 1, insert);addButton("+/-", 2, 5, 1, 1, insert); / 實(shí)現(xiàn)負(fù)數(shù)輸入addButton("
27、;0",1, 5, 1, 1, insert);addButton("+", 3, 5, 1, 1, command);addButton("=", 0, 6, 4, 1, command);this.setResizable(false);private void addButton(String label, int row, int column, int with,int height, ActionListener listener) JButton button = new JButton(label);constraints.gr
28、idwidth = with;constraints.gridheight = height;constraints.gridx = row;constraints.gridy = column;constraints.fill = GridBagConstraints.BOTH;button.addActionListener(listener);layout.setConstraints(button, constraints);container.add(button);public static void main(String args) Conclutor calculator =
29、 new Conclutor();calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / 實(shí)現(xiàn)ActionListener這樣一個(gè)監(jiān)聽(tīng)器接口 private class InsertAction implements ActionListener public void actionPerformed(ActionEvent event) String input = event.getActionCommand();if (start) resultfield.setText("");start = false;if (input.equals("+/-")/if(displayField.getText().charAt(0)!='-')resultfield.setText( "-" + resultfield.getText() );/else /displayField.setText(displayFie
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- CCAA - 2024年03月建筑施工領(lǐng)域?qū)I(yè)答案及解析 - 詳解版(65題)
- 山東省煙臺(tái)市海陽(yáng)市2025-2026學(xué)年七年級(jí)上學(xué)期期末生物學(xué)試題(含解析)
- 中學(xué)學(xué)生課外實(shí)踐基地建設(shè)制度
- 養(yǎng)老院環(huán)境衛(wèi)生與綠化制度
- 企業(yè)信息安全規(guī)范制度
- 電池制液工崗前基礎(chǔ)驗(yàn)收考核試卷含答案
- 鐵氧體材料燒成工崗后能力考核試卷含答案
- 海藻飼料肥料制作工崗前達(dá)標(biāo)考核試卷含答案
- 我國(guó)上市公司機(jī)構(gòu)投資者持股、融資方式與并購(gòu)績(jī)效的關(guān)聯(lián)性探究
- 藏藥調(diào)理師安全檢查水平考核試卷含答案
- 門(mén)診導(dǎo)診工作流程
- 2025初三英語(yǔ)中考英語(yǔ)滿分作文
- 寫(xiě)字樓物業(yè)安全管理實(shí)務(wù)操作手冊(cè)
- 解析卷蘇科版八年級(jí)物理下冊(cè)《物質(zhì)的物理屬性》單元測(cè)試試題(含解析)
- 2025年及未來(lái)5年中國(guó)飲料工業(yè)行業(yè)競(jìng)爭(zhēng)格局分析及發(fā)展趨勢(shì)預(yù)測(cè)報(bào)告
- 魯迅的救國(guó)之路
- 液壓機(jī)安全操作培訓(xùn)課件
- 孕期梅毒課件
- 鋼箱梁施工安全培訓(xùn)課件
- 畢業(yè)論文寫(xiě)作與答辯(第三版)課件 專(zhuān)題二 論文選題
- 含Al奧氏體耐熱鋼:強(qiáng)化機(jī)制剖析與高溫性能探究
評(píng)論
0/150
提交評(píng)論