




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Java課程設計報告(2014)Java課程設計報告保存計算過程的計算器的設計專業(yè)計算科學與技術(網絡技術)學生姓名班級學號1指導教師完成日期1目 錄1 設計要求12 總體設計12.1 CalculatorWindow類設計22.2 NumberButton類設計22.3 OperationButton類設計22.4 HandleDigit類設計22.5 HandleOperation類設計22.6 HandleDot類設計32.7 HandleEquality類設計32.8 HandleSin類設計32.9 HandleBack類設計32.10 HandleClear類設計32.11 Han
2、dlePOrN類設計33詳細設計33.1CalculatorWindow類33.2NumberButton類93.3 OperationButton類103.4 HandleDigit類113.5 HandleOperation類133.6 HandleDot類163.7 HandlePOrN類183.8 HandleEquality類193.9 HandleSin類223.10 HandleBack類243.11 HandleClear類264代碼調試問題285軟件發(fā)布286 程序運行效果287 設計心得303保存過程的計算器的設計1 設計要求Windows 2000/XP系統(tǒng)中的“計算器”
3、是一個方便實用的計算機工具,但是沒有提供顯示計算過程的功能。本計算器所遵循的計算規(guī)則與Windows 2000/XP系統(tǒng)中的“計算器”相同,出了具有普通的計算功能外,還具有現(xiàn)實計算過程,保存計算過程的功能。(1)單擊“計算器”上的數(shù)字按鈕(0、1、2、3、4、5、6、7、8、9)可以設置參與計算的運算數(shù)。(2)單擊“計算器”上的運算符按鈕(+、-、*、/)可以選擇運算符號。(3)單擊“計算器”上的函數(shù)按鈕可以顯示相應的函數(shù)值。(4)單擊“計算器”上的“=”按鈕顯示計算結果。(5)在一個文本框中顯示當前的計算過程,在一個文本區(qū)中顯示以往的計算過程和發(fā)生計算時的系統(tǒng)時間。(6)單擊“保存”按鈕可以
4、將文本區(qū)中的全部計算過程保存到文件;單擊“復制”按鈕可以將文本區(qū)中選中的文本復制到剪貼板;單擊“清除”按鈕可以清楚文本區(qū)中的全部內容。2 總體設計在設計計算器時,可以編寫11個Java源文件:CalculatorWindow.java、NumberButton.java、OperationButton.java、HandleDigit.java、HandleOperation.java、HandleDot.java、HandleEquality.java、HandleSin.java、HandleBack.java、HandleClear.java、HandlePOrN.java。計算器除了上
5、述的11個Java源文件給出的類外,還需要Java提供的一些重要的類,入JButton、JTextField、JTextArea和LinkedList等。CalculatorWindowLinkedListJTexrAreaNumberButtonOperationButtonHandleDigitHandleOperationHandleDotJButtonJTextFieldHandleDotHandlePOrNHandleEqualityHandleSinHandleBackHandleBack圖2.1 類之間的組合關系2.1 CalculatorWindow類設計CalculatorW
6、indow類負責創(chuàng)建計算器的主窗口,該類含有Main方法,計算器從該類開始執(zhí)行。CalculatorWindow有十四種類型的對象,分別是LinkedList<String>、NumberButton、OperationButton、JButton、JTextField、JTextArea、HandleDigit、HandleOperation、HandleDot、HandleEquality、HandleSin、HandleBack、HandleClear、HandlePOrN對象。CalcuatorWindow類創(chuàng)建的窗口以及其中的主要成員如圖2.2所示。CalculatorW
7、indow類的主要成員的作用將在后面的詳細設計中闡述。圖2.2 CalculatorWindow窗口及主要的對象成員2.2 NumberButton類設計NumberButton類創(chuàng)建的對象是主類CalculatorWindow窗口中的一個“數(shù)字”按鈕。NumberButton類有一個int類型的成員number,標明所創(chuàng)建的按鈕所含有的數(shù)字。NumberButton所創(chuàng)建的10個按鈕被添加到CalcuatorWindow窗口中。2.3 OperationButton類設計OperationButton類創(chuàng)建的對象是主類CalculatorWindow窗口中的一個“運算符”按鈕。Operati
8、onButton類有一個String類型的成員operateSign,用于標明所創(chuàng)建的按鈕所含有的運算符號。OperationButton所創(chuàng)建的4個按鈕被添加到CalculatorWindow窗口中。2.4 HandleDigit類設計 HandleDigit類創(chuàng)建的對象負責處理ActionEvent事件。當用戶單擊“數(shù)字”按鈕時,HandleDigit類所創(chuàng)建的對象負責處理和數(shù)字有關的計算過程。2.5 HandleOperation類設計HandleOperation類創(chuàng)建的對象負責處理ActionEvent事件,當用戶單擊“運算符”按鈕時,HandleOperation類所創(chuàng)建的對象負責
9、處理和運算符有關的計算過程。2.6 HandleDot類設計HandleDot類創(chuàng)建的對象負責處理ActionEvent事件,當用戶單擊“.”按鈕(小數(shù)點)時,HandleDot類所創(chuàng)建的對象負責處理小數(shù)點。2.7 HandleEquality類設計HandleEquality類創(chuàng)建的對象負責處理ActionEvent事件。當用戶單擊“=”按鈕時,HandleEquality類所創(chuàng)建的對象計算有關數(shù)據(jù)。2.8 HandleSin類設計HandleSin類創(chuàng)建的對象負責處理ActionEvent事件。當用戶單擊“sin”按鈕(正弦三角函數(shù))時,HandleSin類所創(chuàng)建的對象負責處理正弦三角函數(shù)
10、的值。2.9 HandleBack類設計HandleBack類創(chuàng)建的對象負責處理ActionEvent事件。當用戶單擊“退格”按鈕時,HandleBack類所創(chuàng)建的對象負責計算正弦三晉函數(shù)的值。2.10 HandleClear類設計HandleClear所創(chuàng)建的對象負責處理ActionEvent事件。當用戶成功單擊C按鈕(清零)時,HandleClear類所創(chuàng)建的對象負責清除有關的計算結果。2.11 HandlePOrN類設計HandlePOrN類創(chuàng)建的對象負責處理ActionEvent事件。當用戶單擊“+/-”(正負)按鈕時,HandlePOrN類所創(chuàng)建的對象負責處理數(shù)字的正負轉換。3詳細設
11、計3.1CalculatorWindow類1、效果圖CalculatorWindow創(chuàng)建的窗口效果如圖3.1所示。圖3.1 CalculatorWindow創(chuàng)建的窗口2、UML圖CalculatorWindow類是javax.swing包中JFrame的一個子類,并實現(xiàn)了ActionListener接口。標明該類的主要成員變量和方法的UML圖如圖3.2所示。JFrameActionListenerCalculatorWindownumberButton:NumberButton;operationButton:OperationButton;Flex, zhengfu, Backspace,
12、Equals, Zero, sin:JButtonresultShow,showComputerProcess:JTextFieldsaveComputerProcess:JTextArealist:LinkedList<String>handleDigit:HandleDigithandleOperation:HandleOperationhandleBack:HandleBackhandleClear:HandleClearhandleEquality:HandleEqualityhandleDot:HandleDothandlePOrN:HandlePOrNhandleSin
13、:HandleSinactionPerformed(ActionEvent):voidCalculatorWindow():無類型main(String):void圖3.2 CalculatorWindow類的UML圖3、類設計模塊名稱CalculatorWindow功能描述負責創(chuàng)建計算器的主窗口接口與屬性Void actionPerformed()Function:對saveButton,copyButton,clearButton三個按鈕的ActionEvent事件監(jiān)聽InPut: 無OutPut: 無Return:空CalculatorWindow()Function:是構造方法,負責完
14、成窗口的初始化InPut: 無OutPut:無Return:空Static void Main()Functon:是計算器程序運行的入口方法InPut:無OutPut:無Return:空數(shù)據(jù)結構與算法Linkedlist(String)list補充說明無4、代碼(CalculatorWindow.java)import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.util.LinkedList;import java.io.*;public cla
15、ss CalculatorWindow extends JFrame implements ActionListener NumberButton numberButton; OperationButton operationButton; JButton Flex, zhengfu, Backspace, Equals, Zero, sin; JTextField resultShow; /顯示計算結果 JTextField showComputerProcess; /顯示當前計算過程 JTextArea saveComputerProcess; /顯示計算步驟 JButton saveBu
16、tton, copyButton, clearButton; LinkedList<String> list; /鏈表用來存放第一個運算數(shù)、運算符號和第二個運算數(shù) HandleDigit handleDigit; /負責處理ActionEvent事件 HandleOperation handleOperation; HandleBack handleBack; HandleClear handleClear; HandleEquality handleEquality; HandleDot handleDot; HandlePOrN handlePOrN; HandleSin ha
17、ndleSin; public CalculatorWindow() setTitle("計算器 計算機112班 程逸群"); JPanel panelLeft, panelRight; list = new LinkedList<String>(); resultShow = new JTextField(10); resultShow.setHorizontalAlignment(JTextField.RIGHT); resultShow.setForeground(Color.blue); resultShow.setFont(new Font("
18、;TimesRoman", Font.BOLD, 16); resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED); resultShow.setEditable(false); resultShow.setBackground(Color.white); showComputerProcess = new JTextField(); showComputerProcess.setHorizontalAlignment(JTextField.CENTER); showComputerProcess.setFont(n
19、ew Font("Arial", Font.BOLD, 16); showComputerProcess.setBackground(Color.cyan); showComputerProcess.setEditable(false); saveComputerProcess = new JTextArea(); saveComputerProcess.setEditable(false); saveComputerProcess.setFont(new Font("宋體", Font.PLAIN, 16); numberButton = new Nu
20、mberButton10; handleDigit = new HandleDigit(list, resultShow, showComputerProcess); for (int i = 0; i <= 9; i+) numberButtoni = new NumberButton(i); numberButtoni.setFont(new Font("Arial", Font.BOLD, 20); numberButtoni.addActionListener(handleDigit); operationButton = new OperationButto
21、n4; handleOperation = new HandleOperation(list, resultShow, showComputerProcess, saveComputerProcess); String yunsuan = "+", "-", "*", "/" for (int i = 0; i < 4; i+) operationButtoni = new OperationButton(yunsuani); operationButtoni.setFont(new Font("A
22、rial", Font.BOLD, 20); operationButtoni.addActionListener(handleOperation); Flex = new JButton("."); handleDot = new HandleDot(list, resultShow, showComputerProcess); Flex.addActionListener(handleDot); zhengfu = new JButton("+/-"); handlePOrN = new HandlePOrN(list, resultSho
23、w, showComputerProcess); zhengfu.addActionListener(handlePOrN); Equals = new JButton("="); handleEquality = new HandleEquality(list, resultShow, showComputerProcess, saveComputerProcess); Equals.addActionListener(handleEquality); sin = new JButton("sin"); handleSin = new HandleSi
24、n(list, resultShow, showComputerProcess, saveComputerProcess); sin.addActionListener(handleSin); Backspace = new JButton("退格"); handleBack = new HandleBack(list, resultShow, showComputerProcess); Backspace.addActionListener(handleBack); Zero = new JButton("C"); handleClear = new
25、HandleClear(list, resultShow, showComputerProcess); Zero.addActionListener(handleClear); Zero.setForeground(Color.red); Backspace.setForeground(Color.red); Equals.setForeground(Color.red); sin.setForeground(Color.blue); zhengfu.setForeground(Color.blue); Flex.setForeground(Color.blue); panelLeft = n
26、ew JPanel(); panelRight = new JPanel(); panelLeft.setLayout(new BorderLayout(); JPanel centerInLeft = new JPanel(); panelLeft.add(resultShow, BorderLayout.NORTH); panelLeft.add(centerInLeft, BorderLayout.CENTER); centerInLeft.setLayout(new GridLayout(4, 5); centerInLeft.add(numberButton1); centerInL
27、eft.add(numberButton2); centerInLeft.add(numberButton3); centerInLeft.add(operationButton0); centerInLeft.add(Zero); centerInLeft.add(numberButton4); centerInLeft.add(numberButton5); centerInLeft.add(numberButton6); centerInLeft.add(operationButton1); centerInLeft.add(Backspace); centerInLeft.add(nu
28、mberButton7); centerInLeft.add(numberButton8); centerInLeft.add(numberButton9); centerInLeft.add(operationButton2); centerInLeft.add(sin); centerInLeft.add(numberButton0); centerInLeft.add(zhengfu); centerInLeft.add(Flex); centerInLeft.add(operationButton3); centerInLeft.add(Equals); panelRight.setL
29、ayout(new BorderLayout(); panelRight.add(showComputerProcess, BorderLayout.NORTH); saveButton = new JButton("保存"); copyButton = new JButton("復制"); clearButton = new JButton("清除"); saveButton.setToolTipText("保存計算過程到文件"); copyButton.setToolTipText("復制選中的計算過
30、程"); clearButton.setToolTipText("清除計算過程"); saveButton.addActionListener(this); copyButton.addActionListener(this); clearButton.addActionListener(this); panelRight.add(new JScrollPane(saveComputerProcess), BorderLayout.CENTER); JPanel southInPanelRight = new JPanel(); southInPanelRight
31、.add(saveButton); southInPanelRight.add(copyButton); southInPanelRight.add(clearButton); panelRight.add(southInPanelRight, BorderLayout.SOUTH); JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight); add(split, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT
32、_ON_CLOSE); setVisible(true); setBounds(100, 50, 528, 258); validate(); public void actionPerformed(ActionEvent e) if (e.getSource() = copyButton) saveComputerProcess.copy(); if (e.getSource() = clearButton) saveComputerProcess.setText(null); if (e.getSource() = saveButton) JFileChooser chooser = ne
33、w JFileChooser(); int state = chooser.showSaveDialog(null); File file = chooser.getSelectedFile(); if (file != null && state = JFileChooser.APPROVE_OPTION) try String content = saveComputerProcess.getText(); StringReader read = new StringReader(content); BufferedReader in = new BufferedReade
34、r(read); FileWriter outOne = new FileWriter(file); BufferedWriter out = new BufferedWriter(outOne); String str = null; while (str = in.readLine() != null) out.write(str); out.newLine(); in.close(); out.close(); catch (IOException e1) public static void main(String args) new CalculatorWindow(); 3.2Nu
35、mberButton類1、效果圖NumberButton創(chuàng)建的數(shù)字按鈕效果如圖3.3所示。圖3.3 NumberButton創(chuàng)建的數(shù)字按鈕2、UML圖NumberButton類是javax.swing包中JButton的一個子類,所創(chuàng)建的對象是CalculatorWindow類中NumberButton型數(shù)組:numberButton中的元素。標明NumberButton類的主要成員變量、方法以及CalculatorWindow類之間組合關系的UML圖如圖3.4所示。JButtonNumberButtonCalculatorWindownumber:intnumberButton:Number
36、ButtonNumberButton(int):無類型getNumber():int圖3.4 NumberButton類的UML圖3、類設計模塊名稱NumberButton功能描述負責創(chuàng)建主窗口中的一個“數(shù)字”按鈕接口與屬性String getNumber()Function:返回數(shù)字符號 InPut: 無OutPut: 無Return:數(shù)字符號NumberButton()Function:是構造方法,創(chuàng)建NumberButton對象InPut: 無OutPut:無Return:空數(shù)據(jù)結構與算法Linkedlist(String)list 補充說明無4、代碼(NumberButton.java
37、)import java.awt.*;import java.awt.event.*;import javax.swing.*;public class NumberButton extends JButton int number; public NumberButton(int number) super("" + number); this.number = number; setForeground(Color.blue); public int getNumber() return number; 3.3 OperationButton類1、效果圖Operatio
38、nButton創(chuàng)建的運算符按鈕的效果如圖3.5所示。圖3.5 OperationButton創(chuàng)建的數(shù)字按鈕2、UML圖OperationButton是Javax.swing包中JButton組件的子類。所創(chuàng)建的對象是CalculatorWindow類中OperationButton型數(shù)組:operationButton中的元素。標明OperationButton類的主要成員變量、方法以及和CalculatorWindow類直接的組合關系的UML如圖3.6所示。JButtonOperationButtonCalculatorWindowoperationSign:StringOperationB
39、utton:OperationButtonOperationButton(String):無類型getOperation():String圖3.6 OperationButton類的UML圖3、類設計模塊名稱OperationButton功能描述創(chuàng)建主窗口中的一個“運算符”按鈕接口與屬性String getOperationSign()Function:返回操作符號InPut: 無OutPut: 無Return:運算符號OperationButton()Function:是構造方法,創(chuàng)建OperationButton對象InPut: SOutPut:無Return:空數(shù)據(jù)結構與算法無補充說明無
40、4、代碼(OperationButton.java)import java.awt.*;import java.awt.event.*;import javax.swing.*;public class OperationButton extends JButton String operateSign; public OperationButton(String s) super(s); operateSign = s; setForeground(Color.red); public String getOperateSign() return operateSign; 3.4 Handl
41、eDigit類1、效果圖HandleDigit類創(chuàng)建的對象無效果圖。2、UML圖HandleDigit類實現(xiàn)了ActionListener接口,創(chuàng)建的對象handleDigit是CalculatorWindow窗口的成員之一。標明HandleDigit類的主要成員變量、方法以及和CalculatorWindow類之間組合關系的UML圖如圖3.7所示。ActionListenerHandleDigitCalculatorWindowlist:LinkedList<String>resultShow:JTextFieldshowComputerProcess:JTextFieldhan
42、dleDigit:HandleDigitHandleDigit(LinkedList<String>,JTextField,JTextField):無類型actionPerformed(ActionEvent):void圖3.7 HandleDigit類的UML圖3、類設計模塊名稱HandleDigit功能描述用戶單擊“數(shù)字”按鈕時,負責處理和運算符有關的計算結果接口與屬性Void actionPerformed()Function:處理list鏈表中存儲的第一個操作數(shù)和第二個操作數(shù)InPut: 無OutPut: 無Return:空HandleDigit()Function:是構造
43、方法,創(chuàng)建HandleDigit對象InPut: t1,t2OutPut:無Return:空數(shù)據(jù)結構與算法Linkedlist(String)list補充說明無4、代碼(HandleDigit.java)import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.LinkedList;import javax.swing.JTextField;public class HandleDigit implements ActionListener LinkedList<Stri
44、ng> list; JTextField resultShow; JTextField showComputerProcess; HandleDigit(LinkedList<String> list, JTextField t1, JTextField t2) this.list = list; resultShow = t1; showComputerProcess = t2; public void actionPerformed(ActionEvent e) NumberButton b = (NumberButton) e.getSource(); if (list
45、.size() = 0) int number = b.getNumber(); list.add("" + number); resultShow.setText("" + number); showComputerProcess.setText("" + list.get(0); else if (list.size() = 1) int number = b.getNumber(); String num = list.getFirst(); String s = num.concat("" + number
46、); list.set(0, s); resultShow.setText(s); showComputerProcess.setText("" + list.get(0); else if (list.size() = 2) int number = b.getNumber(); list.add("" + number); resultShow.setText("" + number); showComputerProcess.setText("" + list.get(0) + "" +
47、list.get(1) + "" + list.get(2); else if (list.size() = 3) int number = b.getNumber(); String num = list.getLast(); String s = num.concat("" + number); list.set(2, s); resultShow.setText(s); showComputerProcess.setText("" + list.get(0) + "" + list.get(1) + &quo
48、t;" + list.get(2); 3.5 HandleOperation類1、效果圖HandleOperation類創(chuàng)建的對象無效果圖。2、UML圖HandleOperation類實現(xiàn)了ActionListener接口,創(chuàng)建的對象handleOperation是CalculatorWindow窗口的成員之一。標明HandleOperation類的主要成員變量、方法以及和CalculatorWindow類之間組合關系的UML圖如圖3.8所示。ActionListenerHandleOperationCalculatorWindowlist:LinkedList<String&
49、gt;resultShow:JTextFieldshowComputerProcess:JTextFieldsaveComputerProcess:JTextAreahandleOperation:HandleOperationHandleOperation (LinkedList<String>,JTextField,JTextField,JTextArea):無類型actionPerformed(ActionEvent):void圖3.8 HandleOperation類的UML圖3、類設計模塊名稱HandleOperation功能描述用戶單擊“運算符”按鈕時,負責處理和運算符
50、有關的計算結果接口與屬性Void actionPerformed()Function:處理list鏈表中存儲的運算符和必要的計算 InPut: 無OutPut: 無Return:空HandleOperation()Function:是構造方法,創(chuàng)建HandleOperationt對象InPut: t1,t2,t3OutPut:無Return:空數(shù)據(jù)結構與算法Linkedlist(String)list補充說明無4、代碼(HandleOperation.java)import java.awt.event.ActionEvent;import java.awt.event.ActionListener;impor
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 科技產品研發(fā)中的系統(tǒng)化工程應用案例
- 生產現(xiàn)場的環(huán)保與可持續(xù)發(fā)展策略
- 2025江蘇亞威鑄造材料科技有限公司招聘41人筆試參考題庫附帶答案詳解
- 2025廣西來賓武宣園區(qū)高新技術產業(yè)發(fā)展有限責任公司招聘6人筆試參考題庫附帶答案詳解
- 二零二五年度臨時工臨時用工管理規(guī)范合同
- 社交媒體與電子商務的融合創(chuàng)新
- 科技公司如何幫助員工心理調適
- 2025年度林業(yè)資源綜合利用承包合同
- 2025至2030年中國自行車座套數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國自動分切機數(shù)據(jù)監(jiān)測研究報告
- 第04課 輸入輸出與計算(說課稿)2024-2025學年六年級上冊信息技術人教版
- 部編五下語文教學多元評價方案
- 2024年09月江蘇2024年蘇州金融租賃校園招考筆試歷年參考題庫附帶答案詳解
- 2025年八省聯(lián)考數(shù)學試題(原卷版)
- 《榜樣9》觀后感心得體會二
- 重慶市2024-205學年秋高二(上)期末考試歷史試卷(含答案)康德卷
- 廣西柳州市2025屆高三第二次模擬考試政治試題含答案
- 設備維修績效考核方案
- 《宏觀經濟管理研究》課件
- 鳳凰衛(wèi)視中文臺節(jié)目表
- 2025屆廣東省佛山一中、石門中學高考數(shù)學考前最后一卷預測卷含解析
評論
0/150
提交評論