欢迎来到天天文库
浏览记录
ID:39682652
大小:32.45 KB
页数:8页
时间:2019-07-09
《java实验报告——简单计算器的编写》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、JAVA实验报告——简单计算器的编写班级:学号:姓名:一、实验目的1.掌握java图形用户界面(GUI)的设计原理和程序结构2.能设计复核问题要求的图形用户界面程序 3.掌握常用组件的事件接口 4.应用awt和swing组件进行应用程序设计二、实验条件1.计算机一台2.java软件开发环境 三、实验步骤1、编写代码:mportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassJCalculatorextendsJFrameimplementsAct
2、ionListener{privatestaticfinallongserialVersionUID=-169068472193786457LprivateclassWindowCloserextendsWindowAdapter{publicvoidwindowClosing(WindowEventwe){System.exit(0);}}inti;privatefinalString[]str={"7","8","9","/","4","5","6","*","1","2","3","-",".","0","=","+"}
3、;JButton[]buttons=newJButton[str.length];JButtonreset=newJButton("CE");JTextFielddisplay=newJTextField("0");publicJCalculator(){super("Calculator");JPanelpanel1=newJPanel(newGridLayout(4,4));for(i=0;i4、i]);}JPanelpanel2=newJPanel(newBorderLayout());panel2.add("Center",display);panel2.add("East",reset);getContentPane().setLayout(newBorderLayout());getContentPane().add("North",panel2);getContentPane().add("Center",panel1);for(i=0;i5、nListener(this);reset.addActionListener(this);display.addActionListener(this);addWindowListener(newWindowCloser());setSize(800,800);setVisible(true);pack();}publicvoidactionPerformed(ActionEvente){Objecttarget=e.getSource();Stringlabel=e.getActionCommand();if(target6、==reset)handleReset();elseif("0123456789.".indexOf(label)>0)handleNumber(label);elsehandleOperator(label);}booleanisFirstDigit=true;publicvoidhandleNumber(Stringkey){if(isFirstDigit)display.setText(key);elseif((key.equals("."))&&(display.getText().indexOf(".")<0))di7、splay.setText(display.getText()+".");elseif(!key.equals("."))display.setText(display.getText()+key);isFirstDigit=false;}publicvoidhandleReset(){display.setText("0");isFirstDigit=true;operator="=";}doublenumber=0.0;Stringoperator="=";publicvoidhandleOperator(Stringke8、y){if(operator.equals("+"))number+=Double.valueOf(display.getText());elseif(operator.equals("-"))number-=Double.valueOf(display.getText())
4、i]);}JPanelpanel2=newJPanel(newBorderLayout());panel2.add("Center",display);panel2.add("East",reset);getContentPane().setLayout(newBorderLayout());getContentPane().add("North",panel2);getContentPane().add("Center",panel1);for(i=0;i5、nListener(this);reset.addActionListener(this);display.addActionListener(this);addWindowListener(newWindowCloser());setSize(800,800);setVisible(true);pack();}publicvoidactionPerformed(ActionEvente){Objecttarget=e.getSource();Stringlabel=e.getActionCommand();if(target6、==reset)handleReset();elseif("0123456789.".indexOf(label)>0)handleNumber(label);elsehandleOperator(label);}booleanisFirstDigit=true;publicvoidhandleNumber(Stringkey){if(isFirstDigit)display.setText(key);elseif((key.equals("."))&&(display.getText().indexOf(".")<0))di7、splay.setText(display.getText()+".");elseif(!key.equals("."))display.setText(display.getText()+key);isFirstDigit=false;}publicvoidhandleReset(){display.setText("0");isFirstDigit=true;operator="=";}doublenumber=0.0;Stringoperator="=";publicvoidhandleOperator(Stringke8、y){if(operator.equals("+"))number+=Double.valueOf(display.getText());elseif(operator.equals("-"))number-=Double.valueOf(display.getText())
5、nListener(this);reset.addActionListener(this);display.addActionListener(this);addWindowListener(newWindowCloser());setSize(800,800);setVisible(true);pack();}publicvoidactionPerformed(ActionEvente){Objecttarget=e.getSource();Stringlabel=e.getActionCommand();if(target
6、==reset)handleReset();elseif("0123456789.".indexOf(label)>0)handleNumber(label);elsehandleOperator(label);}booleanisFirstDigit=true;publicvoidhandleNumber(Stringkey){if(isFirstDigit)display.setText(key);elseif((key.equals("."))&&(display.getText().indexOf(".")<0))di
7、splay.setText(display.getText()+".");elseif(!key.equals("."))display.setText(display.getText()+key);isFirstDigit=false;}publicvoidhandleReset(){display.setText("0");isFirstDigit=true;operator="=";}doublenumber=0.0;Stringoperator="=";publicvoidhandleOperator(Stringke
8、y){if(operator.equals("+"))number+=Double.valueOf(display.getText());elseif(operator.equals("-"))number-=Double.valueOf(display.getText())
此文档下载收益归作者所有