计算器交互式Java程序设计_第1页
计算器交互式Java程序设计_第2页
计算器交互式Java程序设计_第3页
计算器交互式Java程序设计_第4页
计算器交互式Java程序设计_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

1、网络交互式-计算器程序: -兰州大学 Java课程设计1.要求: 利用GUI编程技术、网络编程技术、输入输出流技术,编写一个基于C/S的计算器软件。前台为类似于图1的图形界面,主要解决输入界面问题,可以解决简单计算问题,将复杂函数计算如sin、开平方根等操作交给后台服务器计算,计算后结果返回客户端显示,并将客户端的IP地址,发来的计算请求记录在一个日志文件calculate.log。2.实现: 主类: 服务器类:CalculatorServer 客户端类:CalculatorClient 辅类: 查看日志类:lookJFrame 图形面板类:mydraws & mycanvas/客户端impo

2、rt .*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;class CalculatorClient extends JFrame implements Runnable,ActionListenerprivate JTextField show;private Panel dispMain,dispLeft,dispRight;/主(左右面板)private JButton b;/数字private JButton jia,

3、jian,cheng,chu,xy,cos,sin,ln,ce,equ,bfh,point,zf,sqrt;/加减乘除各种运算按钮private Socket socket;private DataInputStream in=null;private DataOutputStream out=null;private Thread thread;private Double result;private Double d1,d2;private int j,action,op;private String opp;private JLabel labelconnect;private JBu

4、tton buttonconnect;private JMenuItem close,version,look,edit,word,background,caltuxing;/菜单栏 菜单项设置public CalculatorClient() super(马康-计算器客户端); JMenuBar mb=new JMenuBar();/菜单栏 JMenu filemenu=new JMenu(计算器); JMenu bigmenu=new JMenu(放大); JMenu calmenu=new JMenu(画板); JMenu vermenu=new JMenu(关于); JMenu edi

5、tmenu=new JMenu(编辑); look=new JMenuItem(查看日志); close=new JMenuItem(退出); version=new JMenuItem(作者); background=new JMenuItem(背景); caltuxing=new JMenuItem(图形画板); word=new JMenuItem(放大); filemenu.add(look); filemenu.add(close); vermenu.add(version); editmenu.add(background); calmenu.add(caltuxing); big

6、menu.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.addAc

7、tionListener(this); socket=new Socket(); Image image = getToolkit().getImage(getClass().getResource(calculator.jpg); setIconImage(image);/左上角图片设置 setSize(360, 250); setLocation(400, 300); setBackground(Color.LIGHT_GRAY);/背景设置 setLayout(new FlowLayout(FlowLayout.CENTER); setResizable(false); show=new

8、 JTextField(31); show.setText(); show.setHorizontalAlignment(SwingConstants.RIGHT);/文本框内容右对齐 show.setEditable(false); add(show); dispMain=new Panel(); add(dispMain); buttonconnect=new JButton(连接服务器); labelconnect=new JLabel(); add(buttonconnect); add(labelconnect); dispMain.setLayout(new GridLayout(

9、1, 2, 10, 10); dispLeft=new Panel(); dispMain.add(dispLeft); dispLeft.setLayout(new GridLayout(4, 3, 3, 3); dispRight=new Panel(); dispMain.add(dispRight); dispRight.setLayout(new GridLayout(4, 3, 3, 3); b=new JButton10; int l; for(l=9;l=0;l-) /0-9数字按钮 bl=new JButton(String.valueOf(l); bl.setForegro

10、und(Color.BLUE); dispLeft.add(bl); bl.addActionListener(this); /监听设置 bl.setFocusPainted(false); /去掉焦点框 jia=new JButton(+); jian=new JButton(-); cheng=new JButton(*); chu=new JButton(/); xy=new JButton(xy); cos=new JButton(cos); sin=new JButton(sin); ln=new JButton(ln);/ ce=new JButton(CE);/清除 equ=ne

11、w JButton(=);/等号 bfh=new JButton(%);/求模 point=new JButton(.);/小数点 zf=new JButton( +/- );/正负号 sqrt=new JButton();/根号 jia.setFocusPainted(false); /去掉焦点框 jian.setFocusPainted(false); /去掉焦点框 cheng.setFocusPainted(false); /去掉焦点框 chu.setFocusPainted(false); /去掉焦点框 xy.setFocusPainted(false); /去掉焦点框 cos.set

12、FocusPainted(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);

13、 /去掉焦点框 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(e

14、qu); /左面板 正负号/小数点按钮 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)

15、; 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;

16、public void run() public void actionPerformed(ActionEvent e) if(e.getSource()=look)new lookJFrame();/查看日志 if(e.getSource()=caltuxing) /图形画板 mydraws myapp=new mydraws(); myapp.setSize(380,300); myapp.setLocation(400, 300); myapp.setVisible(true); if(e.getSource()=background)/背景设置 Color color=JColorCh

17、ooser.showDialog(Component)e.getSource().getParent(),自定义,Color.LIGHT_GRAY);getContentPane().setBackground(color);if(e.getSource()=word)/放大按钮setSize(550, 280);show.setColumns(47);int i=0;for(i=0;i=9;i+)bi.setFont(new Font(宋体,Font.PLAIN,20);jia.setFont(new Font(宋体,Font.PLAIN,20); jian.setFont(new Font

18、(宋体,Font.PLAIN,20); cheng.setFont(new Font(宋体,Font.PLAIN,20); chu.setFont(new Font(宋体,Font.PLAIN,20); xy.setFont(new Font(宋体,Font.PLAIN,20); cos.setFont(new Font(宋体,Font.PLAIN,20); sin.setFont(new Font(宋体,Font.PLAIN,20); ln.setFont(new Font(宋体,Font.PLAIN,20); ce.setFont(new Font(宋体,Font.PLAIN,20); e

19、qu.setFont(new Font(宋体,Font.PLAIN,20); bfh.setFont(new Font(宋体,Font.PLAIN,20); point.setFont(new Font(宋体,Font.PLAIN,20); zf.setFont(new Font(宋体,Font.PLAIN,20); sqrt.setFont(new Font(宋体,Font.PLAIN,20); if(e.getSource()=close) System.exit(0);/关闭 /作者 if(e.getSource()=version) JOptionPane.showMessageDia

20、log(null, 交互式计算器n Java实验程序作业n 作者: *n兰州大学12电子商务); /连接服务器 if(e.getSource()=buttonconnect)tryif(socket.isConnected()elseInetAddress address=InetAddress.getByName(127.0.0.1);/此类表示互联网协议 (IP) 地址。 InetSocketAddress socketAddress=new InetSocketAddress(address.getLocalHost(),4444); /getLocalHost()返回本地主机socke

21、t.connect(socketAddress);/ connect(SocketAddress endpoint) 将此套接字连接到服务器 in=new DataInputStream(socket.getInputStream();/socket返回输入流 out=new DataOutputStream(socket.getOutputStream();/socket返回输出流 labelconnect.setText(已连接到服务器); thread.start(); catch(IOException ee) /正负 if(e.getSource()=zf) if(show.getT

22、ext().toString().equals() show.setText(-); else if(show.getText().toString().equals(-) show.setText(); else try d1=Double.parseDouble(show.getText(); d1=-d1; String dd=+d1; dd=dd.substring(0,dd.length()-2); show.setText(dd); catch(Exception ee) /小数点 if(e.getSource()=point) int f; String sshow=show.g

23、etText(); if(f=sshow.indexOf(.)!=-1)/若已存在小数点 JOptionPane.showMessageDialog(null,小数点已存在!); else /不存在小数点 if(sshow.equals() show.setText(0.); else if(sshow.equals(-) show.setText(-0.); else show.setText(show.getText()+.); /清除 if(e.getSource()=ce) show.setText(); /加法运算符 if(e.getSource()=jia) opp=+; try

24、d1=Double.parseDouble(show.getText(); op=0; show.setText(); catch(Exception ee) /减法运算符 if(e.getSource()=jian) opp=-; try d1=Double.parseDouble(show.getText(); op=1; show.setText(); catch(Exception ee) /乘法运算符 if(e.getSource()=cheng) opp=*; try d1=Double.parseDouble(show.getText(); op=2; show.setText(

25、); catch(Exception ee) /除法运算符 if(e.getSource()=chu) opp=/; try d1=Double.parseDouble(show.getText(); op=3; show.setText(); catch(Exception ee) / /服务器运算的运算符 / /x的y次方 if(e.getSource()=xy) opp=xy; try d1=Double.parseDouble(show.getText(); op=5; show.setText(); catch(Exception ee) /cos运算符 if(e.getSource

26、()=cos) opp=cos; try d1=Double.parseDouble(show.getText(); op=6; show.setText(); catch(Exception ee) /sin运算符 if(e.getSource()=sin) opp=sin; try d1=Double.parseDouble(show.getText(); op=7; show.setText(); catch(Exception ee) /ln运算符 if(e.getSource()=ln) opp=ln; try d1=Double.parseDouble(show.getText()

27、; op=8; show.setText(); catch(Exception ee) /百分比运算符 if(e.getSource()=bfh) opp=%; try d1=Double.parseDouble(show.getText(); op=9; show.setText(); catch(Exception ee) /根号运算符 if(e.getSource()=sqrt) opp=; try d1=Double.parseDouble(show.getText(); op=4; show.setText(); catch(Exception ee) /等号 if(e.getSou

28、rce()=equ) int a,b=1; /a的值用于服务器端的计算,传到服务器端,不同的值代表不同的运算符 try d2=Double.parseDouble(show.getText(); switch(op) case 0: result=d1+d2; break; case 1: result=d1-d2; break; case 2: result=d1*d2; break; case 3: if(d2=0) JOptionPane.showMessageDialog(null,除数不能为零,请重输);b=0; else result=d1/d2; break; case 4: i

29、f(d2=0) JOptionPane.showMessageDialog(null,根号数值不能小于零,请重输);b=0; else result=Math.sqrt(d2); break; case 5: /xy a=5; out.writeInt(a); out.writeDouble(d1); out.writeDouble(d2); try result=in.readDouble(); catch(IOException e1)labelconnect.setText(与服务器已断开); break; case 6: /cos a=6; out.writeInt(a); out.w

30、riteDouble(d2); try result=in.readDouble(); catch(IOException e2)labelconnect.setText(与服务器已断开); break; case 7: /sin a=7; out.writeInt(a); out.writeDouble(d2); try result=in.readDouble(); catch(IOException e3)labelconnect.setText(与服务器已断开); break; case 8: /ln a=8; if(d2=0) JOptionPane.showMessageDialo

31、g(null,数值需大于零,请重输);b=0; else out.writeInt(a); out.writeDouble(d2); try result=in.readDouble(); catch(IOException e4)labelconnect.setText(与服务器已断开); break; case 9: /bfh a=9; out.writeInt(a); out.writeDouble(d1); out.writeDouble(d2); try result=in.readDouble(); catch(IOException e5)labelconnect.setText

32、(与服务器已断开); break; default: if(b=1) show.setText(String.valueOf(result); else show.setText(); a=1; /a=1代表传到服务器处进行保存到日志中 out.writeInt(a); out.writeDouble(d1); out.writeDouble(d2); out.writeUTF(opp); out.writeDouble(result); catch(Exception ee1) /数值0-9 if(e.getSource()=b0) if(!(show.getText().equals(0)

33、|show.getText().equals(-0) show.setText(show.getText()+0); if(e.getSource()=b1) if(show.getText().equals(0) show.setText(1); else if(show.getText().equals(-0) show.setText(-1); else show.setText(show.getText()+1); if(e.getSource()=b2) if(show.getText().equals(0) show.setText(2); else if(show.getText

34、().equals(-0) show.setText(-2); else show.setText(show.getText()+2); if(e.getSource()=b3) if(show.getText().equals(0) show.setText(3); else if(show.getText().equals(-0) show.setText(-3); else show.setText(show.getText()+3); if(e.getSource()=b4) if(show.getText().equals(0) show.setText(4); else if(sh

35、ow.getText().equals(-0) show.setText(-4); else show.setText(show.getText()+4); if(e.getSource()=b5) if(show.getText().equals(0) show.setText(5); else if(show.getText().equals(-0) show.setText(-5); else show.setText(show.getText()+5); if(e.getSource()=b6) if(show.getText().equals(0) show.setText(6);

36、else if(show.getText().equals(-0) show.setText(-6); else show.setText(show.getText()+6); if(e.getSource()=b7) if(show.getText().equals(0) show.setText(7); else if(show.getText().equals(-0) show.setText(-7); else show.setText(show.getText()+7); if(e.getSource()=b8) if(show.getText().equals(0) show.se

37、tText(8); else if(show.getText().equals(-0) show.setText(-8); else show.setText(show.getText()+8); if(e.getSource()=b9) if(show.getText().equals(0) show.setText(9); else if(show.getText().equals(-0) show.setText(-9); else show.setText(show.getText()+9); public static void main(String args) new Calcu

38、latorClient(); /查看日志 lookJFrame类 class lookJFrame extends JFrame private JTextArea txt; private Container con; public lookJFrame() super(计算器日志); con=this.getContentPane(); final Image image=getToolkit().getImage(getClass().getResource(27.jpg); /final ImageIcon img=new ImageIcon(H:/星空物语/java实验/Java图标

39、/19.jpg); txt=new JTextArea()public void paintComponent(Graphics g) g.drawImage(image, 0, 0, null); super.paintComponent(g); ;/文本域 txt.setOpaque(false);/设置为透明add(txt);try FileReader file=new FileReader(calculate.log); BufferedReader in=new BufferedReader(file); String s=calculate.log; txt.append(s+n

40、); while(s=in.readLine()!=null) txt.append(s+n); in.close(); file.close(); catch(IOException exp) txt.setText(无此文件); setSize(600, 400); setLocation(270, 100); setVisible(true);/服务器端import java.io.*;import .*;import java.util.*;import java.text.*;public class CalculatorServerpublic static void main(S

41、tring args)ServerSocket server=null;ServerThread thread;Socket client=null; System.out.println(n-计算器 服务器-n);while(true)tryserver=new ServerSocket(4444);catch(IOException e1)System.out.println(正在监听);tryclient=server.accept();/server返回和客户端相连接的SocketSystem.out.println(客户的地址:+client.getInetAddress();catch(IOException e)System.out.println(正在等待客户);if(client!=null)new ServerThread(client).start();elsecontinue;class ServerThread extends ThreadSocket socket;Dat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论