




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第1頁共20頁廣東金融學院實驗報告課程名稱:Java高級程序設計實驗編號及實驗名稱實驗一計算器系別計科系姓名學號班級實驗地點實驗樓504實驗日期20實驗時數(shù)6指導老師同組其他成員無成績一、實驗目的1、鞏固java開發(fā)基礎2、學習Java圖形界面開發(fā)技術二、實驗環(huán)境1.使用軟件:WindowsXP、jdk1.62.實驗設備:微型計算機三、實驗內容及要求參考書本87頁程序Calculator.java,以此為基礎擴展其功能,設計自己的計算器。四、實驗結果(附程序運行效果圖)importjava.awt.BorderLayout;importjavax.swing.JPanel;importjavax.swing.JFrame;importjavax.swing.JTextField;importjava.awt.Rectangle;importjava.awt.GridBagLayout;importjava.awt.GridLayout;importjavax.swing.JButton;importjava.awt.Point;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.ComponentOrientation;importjava.awt.Dimension;publicclassCalculatorextendsJFrame{ privatestaticfinallongserialVersionUID=1L; privateJPaneljContentPane=null; privateJTextFieldtR=null; privateJPanelp=null; privateJButtonjButton=null; privateJButtonjButton1=null; privateJButtonjButton2=null; privateJButtonjButton3=null; privateJButtonjButton4=null; privateJButtonjButton5=null; privateJButtonjButton6=null; privateJButtonjButton7=null; privateJButtonjButton8=null; privateJButtonjButton9=null; privateJButtonjButton10=null; privateJButtonjButton11=null; privateJButtonjButton12=null; privateJButtonjButton13=null; privateJButtonjButton14=null; privateJButtonjButton15=null; privatedoubleresult=0; privateStringlastCommand="=";//@jve:decl-index=0: privatebooleanstart=true; privateJButtonjButton16=null; privateJButtonjButton17=null; privateJButtonjButton18=null; privateJButtonjButton19=null; /** *Thisisthedefaultconstructor */ publicCalculator(){ super(); initialize(); } privatevoidinitialize(){ this.setSize(290,190); this.setResizable(false); this.setLocation(newPoint(100,300)); this.setContentPane(getJContentPane()); this.setTitle("計算器"); } /** *ThismethodinitializesjContentPane * *@returnjavax.swing.JPanel */ privateJPanelgetJContentPane(){ if(jContentPane==null){ jContentPane=newJPanel(); jContentPane.setLayout(null); jContentPane.add(getTR(),null); jContentPane.add(getP(),null); } returnjContentPane; } /** *ThismethodinitializestR * *@returnjavax.swing.JTextField */ privateJTextFieldgetTR(){ if(tR==null){ tR=newJTextField(); tR.setBounds(newRectangle(0,0,284,22)); tR.setComponentOrientation(ComponentOrientation.UNKNOWN); tR.addKeyListener(newjava.awt.event.KeyAdapter(){ publicvoidkeyPressed(java.awt.event.KeyEvente){ charkey=e.getKeyChar(); if(key=='0'||key=='1'||key=='2'||key=='3'||key=='4'||key=='5' ||key=='6'||key=='7'||key=='8'||key=='9'||key=='.'){ if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()); } elseif(key=='+'||key=='-'||key=='*'||key=='/'||key=='='){ if(start){ if(key=='-'){ tR.setText(String.valueOf(key)); start=false; }else lastCommand=String.valueOf(key); }else{ calculator(Double.parseDouble(tR.getText())); lastCommand=String.valueOf(key); start=true; } } } }); } returntR; } /** *Thismethodinitializesp * *@returnjavax.swing.JPanel */ privateJPanelgetP(){ if(p==null){ GridLayoutgridLayout=newGridLayout(5,4); p=newJPanel(); p.setLayout(gridLayout); p.setBounds(newRectangle(0,21,284,141)); p.add(getJButton16(),null); p.add(getJButton17(),null); p.add(getJButton18(),null); p.add(getJButton19(),null); p.add(getJButton2(),null); p.add(getJButton9(),null); p.add(getJButton13(),null); p.add(getJButton15(),null); p.add(getJButton14(),null); p.add(getJButton11(),null); p.add(getJButton10(),null); p.add(getJButton7(),null); p.add(getJButton6(),null); p.add(getJButton1(),null); p.add(getJButton(),null); p.add(getJButton3(),null); p.add(getJButton4(),null); p.add(getJButton5(),null); p.add(getJButton8(),null); p.add(getJButton12(),null); } returnp; } /** *ThismethodinitializesjButton * *@returnjavax.swing.JButton */ privateJButtongetJButton(){ if(jButton==null){ jButton=newJButton(); jButton.setText("6"); jButton.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton; } /** *ThismethodinitializesjButton1 * *@returnjavax.swing.JButton */ privateJButtongetJButton1(){ if(jButton1==null){ jButton1=newJButton(); jButton1.setText("5"); jButton1.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton1; } /** *ThismethodinitializesjButton2 * *@returnjavax.swing.JButton */ privateJButtongetJButton2(){ if(jButton2==null){ jButton2=newJButton(); jButton2.setText("+"); jButton2.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ lastCommand=input; } else{ calculator(Double.parseDouble(tR.getText())); lastCommand=input; start=true; tR.setText(""); } } } ); } returnjButton2; } /** *ThismethodinitializesjButton3 * *@returnjavax.swing.JButton */ privateJButtongetJButton3(){ if(jButton3==null){ jButton3=newJButton(); jButton3.setText("."); jButton3.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton3; } /** *ThismethodinitializesjButton4 * *@returnjavax.swing.JButton */ privateJButtongetJButton4(){ if(jButton4==null){ jButton4=newJButton(); jButton4.setText("1"); jButton4.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton4; } /** *ThismethodinitializesjButton5 * *@returnjavax.swing.JButton */ privateJButtongetJButton5(){ if(jButton5==null){ jButton5=newJButton(); jButton5.setText("2"); jButton5.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton5; } /** *ThismethodinitializesjButton6 * *@returnjavax.swing.JButton */ privateJButtongetJButton6(){ if(jButton6==null){ jButton6=newJButton(); jButton6.setText("4"); jButton6.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton6; } /** *ThismethodinitializesjButton7 * *@returnjavax.swing.JButton */ privateJButtongetJButton7(){ if(jButton7==null){ jButton7=newJButton(); jButton7.setText("0"); jButton7.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton7; } /** *ThismethodinitializesjButton8 * *@returnjavax.swing.JButton */ privateJButtongetJButton8(){ if(jButton8==null){ jButton8=newJButton(); jButton8.setText("3"); jButton8.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton8; } /** *ThismethodinitializesjButton9 * *@returnjavax.swing.JButton */ privateJButtongetJButton9(){ if(jButton9==null){ jButton9=newJButton(); jButton9.setText("-"); jButton9.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(input); start=false; } else{ calculator(Double.parseDouble(tR.getText())); lastCommand=input; start=true; tR.setText(""); } } }); } returnjButton9; } /** *ThismethodinitializesjButton10 * *@returnjavax.swing.JButton */ privateJButtongetJButton10(){ if(jButton10==null){ jButton10=newJButton(); jButton10.setText("9"); jButton10.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton10; } /** *ThismethodinitializesjButton11 * *@returnjavax.swing.JButton */ privateJButtongetJButton11(){ if(jButton11==null){ jButton11=newJButton(); jButton11.setText("8"); jButton11.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton11; } /** *ThismethodinitializesjButton12 * *@returnjavax.swing.JButton */ privateJButtongetJButton12(){ if(jButton12==null){ jButton12=newJButton(); jButton12.setText("="); jButton12.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ lastCommand=input; } else{ calculator(Double.parseDouble(tR.getText())); lastCommand=input; start=true; tR.setText(""+result); } } }); } returnjButton12; } /** *ThismethodinitializesjButton13 * *@returnjavax.swing.JButton */ privateJButtongetJButton13(){ if(jButton13==null){ jButton13=newJButton(); jButton13.setText("*"); jButton13.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ lastCommand=input; } else{ calculator(Double.parseDouble(tR.getText())); lastCommand=input; start=true; tR.setText(""); } } }); } returnjButton13; } /** *ThismethodinitializesjButton14 * *@returnjavax.swing.JButton */ privateJButtongetJButton14(){ if(jButton14==null){ jButton14=newJButton(); jButton14.setText("7"); jButton14.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ tR.setText(""); start=false; } tR.setText(tR.getText()+input); } }); } returnjButton14; } /** *ThismethodinitializesjButton15 * *@returnjavax.swing.JButton */ privateJButtongetJButton15(){ if(jButton15==null){ jButton15=newJButton(); jButton15.setText("/"); jButton15.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ lastCommand=input; } else{ calculator(Double.parseDouble(tR.getText())); lastCommand=input; start=true; tR.setText(""); } } }); } returnjButton15; } publicvoidcalculator(doublex){ if(lastCommand.equals("+")) result+=x; elseif(lastCommand.equals("-")) result-=x; elseif(lastCommand.equals("*")) result*=x; elseif(lastCommand.equals("/")) result/=x; elseif(lastCommand.equals("squa")) result=Math.pow(Double.parseDouble(tR.getText()),2); elseif(lastCommand.equals("sqrt")) result=Math.sqrt(x); elseif(lastCommand.equals("=")) result=x; } /** *ThismethodinitializesjButton16 * *@returnjavax.swing.JButton */ privateJButtongetJButton16(){ if(jButton16==null){ jButton16=newJButton(); jButton16.setText("squa"); jButton16.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ lastCommand=input; } else{ calculator(Double.parseDouble(tR.getText())); lastCommand=input; start=true; tR.setText(""); } } }); } returnjButton16; } /** *ThismethodinitializesjButton17 * *@returnjavax.swing.JButton */ privateJButtongetJButton17(){ if(jButton17==null){ jButton17=newJButton(); jButton17.setText("sqrt"); jButton17.addActionListener(newjava.awt.event.ActionListener(){ publicvoidactionPerformed(java.awt.event.ActionEvente){ Stringinput=e.getActionCommand(); if(start){ lastCommand=input; } else{ calculator(Double.parseDouble(tR.getText())); lastCommand=input; start=true; tR.setText(""); } } }); } returnjButton17; } /** *Thismeth
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 大數(shù)據(jù)營銷策略咨詢合同
- 農業(yè)產(chǎn)業(yè)園區(qū)投資合作協(xié)議
- 建筑工程玻璃貼膜合同
- 股權激勵保密協(xié)議書
- 公關攝影師聘用合同
- 合伙美容院合同
- 財產(chǎn)分割離婚協(xié)議
- 工業(yè)互聯(lián)網(wǎng)平臺建設與優(yōu)化方案
- 裝修施工安全合同協(xié)議書
- 醫(yī)院大樓裝修工程承包協(xié)議書
- 上海??茖哟巫灾髡猩荚嚵曨}集②(含答案)
- 某市政道路施工交通疏導方案
- 世界主要國際組織課件
- 語言學綱要(新)課件
- 心理評估與診斷簡介課件
- 移動式壓力容器充裝復審換證考試重點題庫(180題)
- 小班安全《湯姆走丟了》PPT課件教案反思微視頻
- 作物栽培學課件棉花
- 最新小學二年級口算及豎式計算練習題
- 生產(chǎn)與運作管理-陳榮秋
- 金雞冠的公雞繪本課件
評論
0/150
提交評論