版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
网络交互式-计算器程序: 兰州大学Java课程设计1.要求:利用GUI编程技术、网络编程技术、输入输出流技术,编写一个基于C/S的计算器软件。前台为类似于图1的图形界面,主要解决输入界面问题,可以解决简单计算问题,将复杂函数计算如sin、开平方根等操作交给后台服务器计算,计算后结果返回客户端显示,并将客户端的IP地址,发来的计算请求记录在一个日志文件calculate.log。2.实现:主类:服务器类:CalculatorServer客户端类:CalculatorClient辅类:查看日志类:lookJFrame图形面板类:mydraws&mycanvas//客户端import.*;importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;classCalculatorClientextendsJFrameimplementsRunnable,ActionListener{privateJTextFieldshow;privatePaneldispMain,dispLeft,dispRight;〃主(左右面板)privateJButton[]b;〃数字privateJButtonjia,jian,cheng,chu,xy,cos,sin,ln,ce,equ,bfh,point,zf,sqrt;//加减乘除各种运算按钮privateSocketsocket;privateDataInputStreamin=null;privateDataOutputStreamout=null;privateThreadthread;privateDoubleresult;privateDoubled1,d2;privateintj,action,op;privateStringopp;privateJLabellabelconnect;privateJButtonbuttonconnect;privateJMenultemclose,version,look,edit,word,background,caltuxing;//菜单栏菜单项设publicCalculatorClient(){super("马康-计算器客户端");JMenuBarmb=newJMenuBar();//菜单栏JMenufilemenu=newJMenu("计算器");JMenubigmenu=newJMenu("放大");JMenucalmenu=newJMenu("画板");JMenuvermenu=newJMenu("关于");JMenueditmenu=newJMenu("编辑");look=newJMenuItem("查看日志");close=newJMenuItem("退出");version=newJMenuItem("作者");background=newJMenuItem("背景");caltuxing=newJMenuItem("图形画板");word=newJMenuItem("放大");filemenu.add(look);filemenu.add(close);vermenu.add(version);editmenu.add(background);calmenu.add(caltuxing);bigmenu.add(word);mb.add(filemenu);mb.add(editmenu);mb.add(calmenu);mb.add(bigmenu);mb.add(vermenu);this.setJMenuBar(mb);look.addActionListener(this);close.addActionListener(this);background.addActionListener(this);word.addActionListener(this);version.addActionListener(this);caltuxing.addActionListener(this);socket=newSocket();Imageimage=getToolkit().getImage(getClass().getResource("calculator.jpg"));setIconImage(image);//左上角图片设置setSize(360,250);setLocation(400,300);setBackground(Color.LIGHT_GRAY);/背景设置setLayout(newFlowLayout(FlowLayout.CENTER));setResizable(false);show=newJTextField(31);show.setText("");show.setHorizontalAlignment(SwingConstants.RIGHT);//文本框内容右对齐show.setEditable(false);add(show);dispMain=newPanel();add(dispMain);buttonconnect=newJButton("连接服务器");labelconnect=newJLabel("");add(buttonconnect);add(labelconnect);dispMain.setLayout(newGridLayout(1,2,10,10));dispLeft=newPanel();dispMain.add(dispLeft);dispLeft.setLayout(newGridLayout(4,3,3,3));dispRight=newPanel();dispMain.add(dispRight);dispRight.setLayout(newGridLayout(4,3,3,3));b=newJButton[10];intl;for(l=9;l>=0;l--){ //0-9数字按钮b[l]=newJButton(String.valueOf(l));b[l].setForeground(Color.BLUE);dispLeft.add(b[l]);b[l].addActionListener(this);//监听设置b[l].setFocusPainted(false);//去掉焦点框}jia=newJButton("+");jian=newJButton("-");cheng=newJButton("*");chu=newJButton("/");xy=newJButton("xAy");cos=newJButton("cos");sin=newJButton("sin");ln=newJButton("ln");//ce=newJButton("CE");〃清除equ=newJButton("=");〃等号bfh=newJButton("%");〃求模point=newJButton(".");〃小、数点zf=newJButton("+/-");//正负号sqrt=newJButton("V");//根号jia.setFocusPainted(false);//去掉焦点框jian.setFocusPainted(false);//去掉焦点框cheng.setFocusPainted(false);//去掉焦点框chu.setFocusPainted(false);//去掉焦点框xy.setFocusPainted(false);//去掉焦点框cos.setFocusPainted(false);//去掉焦点框sin.setFocusPainted(false);//去掉焦点框ln.setFocusPainted(false);//去掉焦点框ce.setFocusPainted(false);//去掉焦点框equ.setFocusPainted(false);//去掉焦点框bfh.setFocusPainted(false);//去掉焦点框point.setFocusPainted(false);//去掉焦点框zf.setFocusPainted(false);//去掉焦点框sqrt.setFocusPainted(false);//去掉焦点框buttonconnect.setFocusPainted(false);//去掉焦点框//右面板运算符按钮dispRight.add(chu);dispRight.add(sqrt);dispRight.add(ln);dispRight.add(cheng);dispRight.add(sin);dispRight.add(bfh);dispRight.add(jian);dispRight.add(cos);dispRight.add(ce);dispRight.add(jia);dispRight.add(xy);dispRight.add(equ);//左面板正负号/小数点按钮dispLeft.add(zf);dispLeft.add(point);//按钮监听设置ce.addActionListener(this);jia.addActionListener(this);jian.addActionListener(this);cheng.addActionListener(this);chu.addActionListener(this);equ.addActionListener(this);point.addActionListener(this);zf.addActionListener(this);sqrt.addActionListener(this);bfh.addActionListener(this);sin.addActionListener(this);cos.addActionListener(this);xy.addActionListener(this);ln.addActionListener(this);buttonconnect.addActionListener(this);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);d1=0.0;d2=0.0;op=0;}publicvoidrun(){}publicvoidactionPerformed(ActionEvente){if(e.getSource()==look){newlookJFrame();}//查看日志if(e.getSource()==caltuxing){//图形画板mydrawsmyapp=newmydraws();myapp.setSize(380,300);myapp.setLocation(400,300);myapp.setVisible(true);}if(e.getSource()==background){〃背景设置Colorcolor=JColorChooser.showDialog(((Component)e.getSource()).getParent(),"自定义",Color.LIGHT_GRAY);getContentPane().setBackground(color);}if(e.getSource()==word){〃放大按钮setSize(550,280);show.setColumns(47);inti=0;for(i=0;i<=9;i++){b[i].setFont(newFont("宋体",Font.PLAIN,20));}jia.setFont(newFont("宋体",Font.PLAIN,20));jian.setFont(newFont("宋体",Font.PLAIN,20));cheng.setFont(newFont("宋体",Font.PLAIN,20));chu.setFont(newFont("宋体",Font.PLAIN,20));xy. setFont(newFont('宋体",Font.PLAIN,20));cos.setFont(newFont("宋体",Font.PLAIN,20));sin.setFont(newFont("宋体",Font.PLAIN,20));ln.setFont(newFont("宋体",Font.PLAIN,20));ce.setFont(newFont("宋体",Font.PLAIN,20));equ.setFont(newFont("宋体",Font.PLAIN,20));bfh.setFont(newFont("宋体",Font.PLAIN,20));point.setFont(newFont("宋体",Font.PLAIN,20));zf. setFont(newFont('宋体",Font.PLAIN,20));sqrt.setFont(newFont("宋体",Font.PLAIN,20));}if(e.getSource()==close){System.exit(0);}//关闭//作者if(e.getSource()==version){JOptionPane.showMessageDialog(null,"交互式计算器\nJava实验程序作业\n 作者:****\n兰州大学12电子商务");}//连接服务器if(e.getSource()==buttonconnect){try{if(socket.isConnected()){}else{InetAddressaddress=lnetAddress.getByName("");〃此类表示互联网协议(IP)地址。InetSocketAddress socketAddress=newInetSocketAddress(address.getLocalHost(),4444);//getLocalHost(返回本地主机socket.connect(socketAddress);//connect(SocketAddressendpoint)将此套接字连接到服务器in=newDataInputStream(socket.getInputStream());//socket返回输入流out=newDataOutputStream(socket.getOutputStream());//socket返回输出流labelconnect.setText("已连接到服务器");thread.start();}}catch(IOExceptionee){}}//正负if(e.getSource()==zf){if(show.getText().toString().equals(""))show.setText("-");elseif(show.getText().toString().equals("-"))show.setText("");else{try{d1=Double.parseDouble(show.getText());d1=-d1;Stringdd=""+d1;dd=dd.substring(0,dd.length()-2);show.setText(dd);}catch(Exceptionee){}}}//小数点if(e.getSource()==point){intf;Stringsshow=show.getText();if((f=sshow.indexOf('.'))!=-1)〃若已存在小数点JOptionPane.showMessageDialog(null,"小数点已存在!");else{//不存在小数点if(sshow.equals(""))show.setText("0.");else{if(sshow.equals("-"))show.setText("-0.");elseshow.setText(show.getText()+".");}}}//清除if(e.getSource()==ce){show.setText("");}//加法运算符if(e.getSource()==jia){opp="+";try{d1=Double.parseDouble(show.getText());op=0;show.setText("");}catch(Exceptionee){}}//减法运算符if(e.getSource()==jian){opp="-";try{d1=Double.parseDouble(show.getText());op=1;show.setText("");}catch(Exceptionee){}}//乘法运算符if(e.getSource()==cheng){opp="*";try{d1=Double.parseDouble(show.getText());op=2;show.setText("");}catch(Exceptionee){}}//除法运算符if(e.getSource()==chu){opp="/";try{d1=Double.parseDouble(show.getText());op=3;show.setText("");}catch(Exceptionee){}}//////////////////////服务器运算的运算符//////////////////////x的y次方if(e.getSource()==xy){opp="xAy";try{d1=Double.parseDouble(show.getText());op=5;show.setText("");}catch(Exceptionee){}}//cos运算符if(e.getSource()==cos){opp="cos";try{d1=Double.parseDouble(show.getText());op=6;show.setText("");}catch(Exceptionee){}}//sin运算符if(e.getSource()==sin){opp="sin";try{d1=Double.parseDouble(show.getText());op=7;show.setText("");}catch(Exceptionee){}}//ln运算符if(e.getSource()==ln){opp="ln";try{d1=Double.parseDouble(show.getText());op=8;show.setText("");}catch(Exceptionee){}}//百分比运算符if(e.getSource()==bfh){opp="%";try{d1=Double.parseDouble(show.getText());op=9;show.setText("");}catch(Exceptionee){}}//根号运算符if(e.getSource()==sqrt){opp="V";try{d1=Double.parseDouble(show.getText());op=4;show.setText("");}catch(Exceptionee){}}/ p.//等号if(e.getSource()==equ){inta,b=1;//a的值用于服务器端的计算,传到服务器端,不同的值代表不同的运算符try{d2=Double.parseDouble(show.getText());switch(op){case0:result=d1+d2;break;case1:result=d1-d2;break;case2:result=d1*d2;break;case3:if(d2==0){JOptionPane.showMessageDialog(null,‘除数不能为零,请重输");b=0;}elseresult=d1/d2;break;case4:if(d2<=0){JOptionPane.showMessageDialog(null,‘根号数值不能小于零,请重输");b=0;}elseresult=Math.sqrt(d2);break;case5: //xAya=5;out.writeInt(a);out.writeDouble(d1);out.writeDouble(d2);try{result=in.readDouble();}catch(IOExceptione1){labelconnect.setText(与服务器已断开");}break;case6://cosa=6;out.writeInt(a);out.writeDouble(d2);try{result=in.readDouble();}catch(IOExceptione2){labelconnect.setText(与服务器已断开");}break;case7://sina=7;out.writeInt(a);out.writeDouble(d2);try{result=in.readDouble();}catch(IOExceptione3){labelconnect.setText(与服务器已断开");}break;case8://lna=8;if(d2<=0){JOptionPane.showMessageDialog(null,‘数值需大于零,请重输");b=0;}else{out.writeInt(a);out.writeDouble(d2);try{result=in.readDouble();}catch(IOExceptione4){labelconnect.setText(‘与服务器已断开");}}break;case9://bfha=9;out.writeInt(a);out.writeDouble(d1);out.writeDouble(d2);try{result=in.readDouble();}catch(IOExceptione5){labelconnect.setText(与服务器已断开");}break;default:}if(b==1)show.setText(String.valueOf(result));elseshow.setText("");a=1;//a=1代表传到服务器处进行保存到日志中out.writeInt(a);out.writeDouble(d1);out.writeDouble(d2);out.writeUTF(opp);out.writeDouble(result);}catch(Exceptionee1){}}////////////////////数值0-9if(e.getSource()==b[0]){if(!(show.getText().equals("0")||show.getText().equals("-0")))show.setText(show.getText()+"0");}if(e.getSource()==b[1]){if(show.getText().equals("0"))show.setText("1");else{if(show.getText().equals("-0"))show.setText("-1");elseshow.setText(show.getText()+"1");}}if(e.getSource()==b[2]){if(show.getText().equals("0"))show.setText("2");else{if(show.getText().equals("-0"))show.setText("-2");elseshow.setText(show.getText()+"2");}}if(e.getSource()==b[3]){if(show.getText().equals("0"))show.setText("3");else{if(show.getText().equals("-0"))show.setText("-3");elseshow.setText(show.getText()+"3");}}if(e.getSource()==b[4]){if(show.getText().equals("0"))show.setText("4");else{if(show.getText().equals("-0"))show.setText("-4");elseshow.setText(show.getText()+"4");}}if(e.getSource()==b[5]){if(show.getText().equals("0"))show.setText("5");else{if(show.getText().equals("-0"))show.setText("-5");elseshow.setText(show.getText()+"5");}}if(e.getSource()==b[6]){if(show.getText().equals("0"))show.setText("6");else{if(show.getText().equals("-0"))show.setText("-6");elseshow.setText(show.getText()+"6");}}if(e.getSource()==b[7]){if(show.getText().equals("0"))show.setText("7");else{if(show.getText().equals("-0"))show.setText("-7");elseshow.setText(show.getText()+"7");}}if(e.getSource()==b[8]){if(show.getText().equals("0"))show.setText("8");else{if(show.getText().equals("-0"))show.setText("-8");elseshow.setText(show.getText()+"8");}}if(e.getSource()==b[9]){if(show.getText().equals("0"))show.setText("9");else{if(show.getText().equals("-0"))show.setText("-9");elseshow.setText(show.getText()+"9");}}}publicstaticvoidmain(Stringargs[]){newCalculatorClient();}}//查看日志lookJFrame类classlookJFrameextendsJFrame{privateJTextAreatxt;privateContainercon;publiclookJFrame(){super("计算器日志");con=this.getContentPane();finalImageimage=getToolkit().getImage(getClass().getResource("27.jpg"));//finalImageiconimg=newlmagelcon("H:/星空物语/java实验/Java图标/19.jpg");txt=newJTextArea(){publicvoidpaintComponent(Graphicsg){g.drawlmage(image,0,0,null);super.paintComponent(g);}};//文本域txt.setOpaque(false);//设置为透明add(txt);try{FileReaderfile=newFileReader("calculate.log");BufferedReaderin=newBufferedReader(file);Strings="calculate.log";txt.append(s+"\n");while((s=in.readLine())!=null){txt.append(s+"\n");}in.close();file.close();}catch(IOExceptionexp){txt.setText("无此文件”);}setSize(600,400);setLocation(270,100);setVisible(true);}}//服务器端importjava.io.*;import.*;importjava.util.*;importjava.text.*;publicclassCalculatorServer{publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;ServerThreadthread;Socketclient=null;System.out.println("\n---计算器服务器---\n");while(true){try{server=newServerSocket(4444);}catch(IOExceptione1){System.out.println('正在监听");}try{client=server.accept();//server返回和客户端相连接的SocketSystem.out.println("客户的地址:"+client.getlnetAddress());}catch(IOExceptione){System.out.printlnC正在等待客户");}if(client!=null){newServerThread(client).start();}else{continue;}}}}classServerThreadextendsThread{Socketsocket;DataOutputStreamout=null;DatalnputStreamin=null;Strings=null;FileWriterfile;BufferedWriteroutfile;ServerThread(Sockett){try{file=newFileWriter("calculate.log",true);outfile=newBufferedWriter(file);}catch(Exceptionee){}socket=t;try{in=newDataInputStream(socket.getInputStream());//socket返回输入流out=newDataOutputStream
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人教版四年级上册数学第六单元《除数是两位数的除法》测试卷审定版
- 食品供货协议书
- 认证服务合同服务说明
- 设备授权经销合同案例
- 设备采购方式合同
- 诚信纺织品采购协议
- 语文要素教学的创新思路
- 财务担保保函
- 购车协议合同注意事项
- 购销合同买方权益分析
- 幼教培训课件:《幼儿园区域活动材料投放及指导策略》
- 《情绪的管理作业设计方案-2023-2024学年初中道德与法治统编版》
- 融资担保公司工资制度与绩效考核实施细则
- 《区块链金融》考试复习题库(含答案)
- 《非洲音乐》简介
- 医药级八角茴香油特点2023药典备案
- 沪教版九年级(下)物理第八章电能与磁8.1电功率练习题一和参考答案
- 机电题库2024(矿安益考试平台题库)
- 设备维保的故障分析和故障率统计
- 一例止血带放气不全导致的不良事件
- 网络工程职业生涯展示
评论
0/150
提交评论