java課程設(shè)計(jì)模板(windows計(jì)算器)_第1頁(yè)
java課程設(shè)計(jì)模板(windows計(jì)算器)_第2頁(yè)
java課程設(shè)計(jì)模板(windows計(jì)算器)_第3頁(yè)
java課程設(shè)計(jì)模板(windows計(jì)算器)_第4頁(yè)
java課程設(shè)計(jì)模板(windows計(jì)算器)_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

《JAVA程序設(shè)計(jì)》課程設(shè)計(jì)簡(jiǎn)單計(jì)算器姓名:陳凱昂班級(jí):物聯(lián)網(wǎng)(二)班學(xué)號(hào):121210325指導(dǎo)教師:李瑋瑤成績(jī):完成時(shí)間:2014.06.12一、設(shè)計(jì)目的使用Java的GUI圖形用戶界面編程設(shè)計(jì)并編寫(xiě)一個(gè)計(jì)算器程序,用戶可以通過(guò)鼠標(biāo)或者鍵盤操作輸入?yún)⒓舆\(yùn)算的的數(shù)值和運(yùn)算符,要求能進(jìn)行加、減、乘、除等混合運(yùn)算。二、功能介紹本程序能實(shí)現(xiàn)下列各項(xiàng)功能:(1)實(shí)現(xiàn)加、減、乘、除等基本運(yùn)算;(2)實(shí)現(xiàn)開(kāi)方,百分率,倒數(shù)等運(yùn)算;(3)實(shí)現(xiàn)退格,清除,存儲(chǔ),讀取存儲(chǔ)數(shù)據(jù)等功能。三、概要設(shè)計(jì)3.1自定義類說(shuō)明classCalFrameextendsJFrame//自定義子類CalFrame繼承于父類JFrame;classCalService//計(jì)算器計(jì)算方法;classMyMath//算數(shù)進(jìn)度運(yùn)算;publicclassCal//主類;3.2方法定義說(shuō)明privatefinalString[]...//定義按鍵字符privatefinalJButtonbutton//定義按鍵privateJPanelpanel//定義面板privateJTextField//定義文本privateJTextArea//定義文本區(qū)域privateJButton[]//定義按鍵組publicvoidinitialize()//初始化計(jì)算器界面publicActionListenergetActionListener()//設(shè)置偵聽(tīng)器publicvoidactionPerformed(finalActionEvente)//設(shè)置動(dòng)作事件privateJTextFieldgetTextField()//初始化輸入框privateJButton[]getMButton()//獲取計(jì)算器的存儲(chǔ)區(qū)顯示等按鍵privateJButton[]getRButton()//獲得計(jì)算器的退格清零等按鍵privateJButton[]getNButton()//獲得計(jì)算器的加減乘除等其他按鍵publicvoidsetTextArea(JTextAreatextArea)//設(shè)置文本區(qū)域publicStringcallMethod(Stringcmd,Stringtext)//調(diào)用該方法并返回計(jì)算結(jié)果publicStringcal(Stringtext,booleanisPercent)//用來(lái)結(jié)算加、減、乘、除法,并返回封裝成String類型的結(jié)果,參數(shù)text是顯示框中的內(nèi)容,boolean代表是否有“%”運(yùn)算publicStringsetReciprocal(Stringtext)//計(jì)算倒數(shù)publicStringsqrt(Stringtext)//開(kāi)方運(yùn)算publicStringsetOp(Stringcmd,Stringtext)//設(shè)置操作符publicStringsetNegative(Stringtext)//設(shè)置正負(fù)數(shù),text是正數(shù)是返回負(fù)數(shù)數(shù)字字符串,反之,返回正數(shù)數(shù)字字符串publicStringcatNum(Stringcmd,Stringtext)//連接輸入的數(shù)字,每次單擊數(shù)字,都把新加數(shù)字追加到后面,并封裝成字符串返回publicStringbackSpace(Stringtext)//退后一個(gè)字符,并返回結(jié)果publicStringmCmd(Stringcmd,Stringtext)//用來(lái)實(shí)現(xiàn)“M+”“MC”“MR”“MS”等與存儲(chǔ)有關(guān)的功能publicStringclearAll()//清空所有計(jì)算結(jié)果publicStringclear(Stringtext)//清除上次計(jì)算結(jié)果四、詳細(xì)設(shè)計(jì)4.1簡(jiǎn)單計(jì)算器界面設(shè)計(jì)1、具體實(shí)現(xiàn)過(guò)程本程序采用Calaframe子類繼承于父類JFrame,采用BorderLayout邊界布局和GridLayout網(wǎng)格布局,將面板添加到里面來(lái)布置文本區(qū)域以及按鍵區(qū)域。同時(shí),設(shè)置監(jiān)聽(tīng)器ActionListener來(lái)實(shí)現(xiàn)各個(gè)按鍵的動(dòng)作功能。2、關(guān)鍵代碼設(shè)計(jì)//導(dǎo)入組件importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.math.BigDecimal;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.border.EmptyBorder;//定義繼承父類Jframe的子類CalFrameclassCalFrameextendsJFrame{ privatestaticfinallongserialVersionUID=-4043419751779156119L; privatefinalString[]nOp={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};privatefinalString[]mOp={"MC","MR","MS","M+"};privatefinalString[]rOp={"BackSpace","CE","C"};privatefinalStringopString="+-*/";privatefinalJButtonbutton=newJButton();privatefinalCalServiceservice=newCalService();privateJPanelpanel;privateJTextFieldtextField1;privateJTextFieldtextField2;privateJTextAreatextArea;privateJButton[]mButton;privateJPanelpanel1;//顯示框放置區(qū)privateJPanelpanel2;//按鍵組m放置區(qū)privateJButton[]rButton;privateJPanelpanel3;privateJPanelpanel31;//按鍵組r放置區(qū)privateJButton[]nButton;privateJPanelpanel32;//其他按鍵放置區(qū)publicCalFrame(){super();//訪問(wèn)父類initialize();}publicvoidinitialize()//初始化計(jì)算器界面{this.setTitle("計(jì)算器by陳凱昂");panel=newJPanel();panel.setLayout(newBorderLayout(10,10));panel1=newJPanel(newBorderLayout());panel1.setBackground(Color.white);getTextField();panel1.add(textField1,BorderLayout.CENTER);panel1.add(textField2,BorderLayout.NORTH);panel2=newJPanel();mButton=getMButton();panel2.setLayout(newGridLayout(5,1,0,5));for(finalJButtonb:mButton)panel2.add(b);panel3=newJPanel();panel3.setLayout(newBorderLayout(0,3));rButton=getRButton();panel31=newJPanel();panel31.setLayout(newGridLayout(1,3,3,0));for(finalJButtonb:rButton)panel31.add(b);nButton=getNButton();panel32=newJPanel();panel32.setLayout(newGridLayout(4,5,3,3));for(finalJButtonb:nButton)panel32.add(b);panel3.add(panel31,BorderLayout.NORTH);panel3.add(panel32);panel.add(panel1,BorderLayout.NORTH);panel.add(panel2,BorderLayout.WEST);panel.add(panel3);this.add(panel);this.setResizable(false);this.setLocationRelativeTo(null);}//設(shè)置偵聽(tīng)器publicActionListenergetActionListener(){returnnewActionListener(){publicvoidactionPerformed(finalActionEvente){finalStringcmd=e.getActionCommand();Stringresult=null;try{result=service.callMethod(cmd,textField1.getText());}catch(finalExceptionex){System.out.println(ex.getMessage());}if(opString.indexOf(cmd)!=-1){textField2.setText(textField2.getText()+textField1.getText()+cmd);}elseif(cmd.equals("=")){textField2.setText("");}elseif(cmd.indexOf("MC")==0)button.setText("");elseif(cmd.indexOf("M")==0&&service.getStore()>0)button.setText("M");if(result!=null)textField1.setText(result);}};}//初始化輸入框privateJTextFieldgetTextField(){textField1=newJTextField();textField1.setBorder(newEmptyBorder(0,0,0,0));textField1.setText("0");textField1.setHorizontalAlignment(JTextField.RIGHT);textField1.setEditable(false);textField1.setOpaque(false);textField2=newJTextField();textField2.setBorder(newEmptyBorder(0,0,0,0));textField2.setHorizontalAlignment(JTextField.RIGHT);textField2.setEditable(false);textField2.setOpaque(false);returnnull;}//獲得計(jì)算器的存儲(chǔ)顯示等按鍵privateJButton[]getMButton(){finalJButton[]result=newJButton[this.mOp.length+1];result[0]=this.button;for(inti=0;i<this.mOp.length;i++){finalJButtonb=newJButton(this.mOp[i]);b.addActionListener(getActionListener());b.setForeground(Color.black);result[i+1]=b;}returnresult;}//獲得計(jì)算器的退格清零等按鍵privateJButton[]getRButton(){finalJButton[]result=newJButton[this.rOp.length];for(inti=0;i<this.rOp.length;i++){finalJButtonb=newJButton(this.rOp[i]);b.addActionListener(getActionListener());b.setForeground(Color.red);result[i]=b;}returnresult;}//獲得計(jì)算器的加減乘除等其他按鍵privateJButton[]getNButton(){finalJButton[]result=newJButton[this.nOp.length];for(inti=0;i<this.nOp.length;i++){finalJButtonb=newJButton(this.nOp[i]);b.addActionListener(getActionListener());b.setForeground(Color.blue);result[i]=b;}returnresult;} //設(shè)置文本區(qū)域publicvoidsetTextArea(JTextAreatextArea) { this.textArea=textArea; } //獲得文本區(qū)域publicJTextAreagetTextArea() { returntextArea; }}//計(jì)算方法classCalService{privatedoublestore;privatebooleanisSecondNum=false;privateStringnumString=".0123456789";privateStringopString="+-*/";privateStringlastOp;privateStringfirstNum="0";privateStringsecondNum;privatebooleanflag=true;publicdoublegetStore(){returnstore;}//調(diào)用該方法并返回計(jì)算結(jié)果publicStringcallMethod(Stringcmd,Stringtext){if(cmd.equals("C"))returnclearAll();elseif(cmd.equals("CE"))returnclear(text);elseif(cmd.equals("BackSpace"))returnbackSpace(text);elseif(numString.indexOf(cmd)!=-1)returncatNum(cmd,text);elseif(opString.indexOf(cmd)!=-1)returnsetOp(cmd,text);elseif(cmd.equals("=")){flag=false;returncal(text,false);}elseif(cmd.equals("+/-"))returnsetNegative(text);elseif(cmd.equals("1/x"))returnsetReciprocal(text);elseif(cmd.equals("sqrt"))returnsqrt(text);elseif(cmd.equals("%"))returncal(text,true);elsereturnmCmd(cmd,text);}publicStringcal(Stringtext,booleanisPercent)//用來(lái)結(jié)算加、減、乘、除法,并返回封裝成String類型的結(jié)果,參數(shù)text是顯示框中的內(nèi)容,boolean代表是否有“%”運(yùn)算{doublesecondResult=secondNum==null?Double.valueOf(text).doubleValue():Double.valueOf(secondNum).doubleValue();if(secondResult==0&&this.lastOp.equals("/"))return"0";if(isPercent)firstNum=String.valueOf(MyMath.multiply(Double.valueOf(firstNum),MyMath.divide(secondResult,100)));elseif(this.lastOp.equals("+"))firstNum=String.valueOf(MyMath.add(Double.valueOf(firstNum),secondResult));elseif(this.lastOp.equals("-"))firstNum=String.valueOf(MyMath.subtract(Double.valueOf(firstNum),secondResult));elseif(this.lastOp.equals("*"))firstNum=String.valueOf(MyMath.multiply(Double.valueOf(firstNum),secondResult));elseif(this.lastOp.equals("/"))firstNum=String.valueOf(MyMath.divide(Double.valueOf(firstNum),secondResult));secondNum=null;this.isSecondNum=true;returnfirstNum;}publicStringsetReciprocal(Stringtext)//計(jì)算倒數(shù){if(text.equals("0"))returntext;else{this.isSecondNum=true;returnString.valueOf(MyMath.divide(1,Double.valueOf(text)));}}publicStringsqrt(Stringtext)//開(kāi)方運(yùn)算{this.isSecondNum=true;returnString.valueOf(Math.sqrt(Double.valueOf(text)));}publicStringsetOp(Stringcmd,Stringtext)//設(shè)置操作符{if(!firstNum.equals("0")&&flag)firstNum=cal(text,false);else{this.firstNum=text;this.secondNum=null;this.isSecondNum=true;flag=true;}this.lastOp=cmd;returnfirstNum;}publicStringsetNegative(Stringtext)//設(shè)置正負(fù)數(shù),text是正數(shù)是返回負(fù)數(shù)數(shù)字字符串,反之,返回正數(shù)數(shù)字字符串{returnString.valueOf(0-Double.valueOf(text));}publicStringcatNum(Stringcmd,Stringtext)//連接輸入的數(shù)字,每次單擊數(shù)字,都把新加數(shù)字追加到后面,并封裝成字符串返回{Stringresult=cmd;if(!text.equals("0")){if(isSecondNum){isSecondNum=false;}else{result=text+cmd;}}if(result.indexOf(".")==0)result="0"+result;returnresult;}publicStringbackSpace(Stringtext)//退后一個(gè)字符,并返回結(jié)果{return(text.equals("0")||text.equals(""))?"0":text.substring(0,text.length()-1);}publicStringmCmd(Stringcmd,Stringtext)//用來(lái)實(shí)現(xiàn)“M+”“MC”“MR”“MS”等與存儲(chǔ)有關(guān)的功能{if(cmd.equals("M+"))//累加結(jié)果store=MyMath.add(store,Double.valueOf(text));elseif(cmd.equals("MC"))//清除結(jié)果store=0;elseif(cmd.equals("MR"))//讀取結(jié)果{isSecondNum=true;returnString.valueOf(store);}elseif(cmd.equals("MS"))//保存結(jié)果store=Double.valueOf(text).doubleValue();returnnull;}publicStringclearAll()//清空所有計(jì)算結(jié)果{ this.secondNum=null; this.firstNum="0";returnthis.firstNum;}publicStringclear(Stringtext)//清除上次計(jì)算結(jié)果{return"0";}}classMyMath{privatestaticBigDecimalgetBigDecimal(doublenumber){returnnewBigDecimal(number);}publicstaticdoubleadd(doublenum1,doublenum2){BigDecimal

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論