《Java高级程序设计》实验报告一_第1页
《Java高级程序设计》实验报告一_第2页
《Java高级程序设计》实验报告一_第3页
《Java高级程序设计》实验报告一_第4页
《Java高级程序设计》实验报告一_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第1页共20页广东金融学院实验报告课程名称:Java高级程序设计实验编号及实验名称实验一计算器系别计科系姓名学号班级实验地点实验楼504实验日期20实验时数6指导老师同组其他成员无成绩一、实验目的1、巩固java开发基础2、学习Java图形界面开发技术二、实验环境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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论