計(jì)算器交互式Java程序設(shè)計(jì)_第1頁(yè)
計(jì)算器交互式Java程序設(shè)計(jì)_第2頁(yè)
計(jì)算器交互式Java程序設(shè)計(jì)_第3頁(yè)
計(jì)算器交互式Java程序設(shè)計(jì)_第4頁(yè)
計(jì)算器交互式Java程序設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、網(wǎng)絡(luò)交互式-計(jì)算器程序: -蘭州大學(xué) Java課程設(shè)計(jì)1.要求: 利用GUI編程技術(shù)、網(wǎng)絡(luò)編程技術(shù)、輸入輸出流技術(shù),編寫一個(gè)基于C/S的計(jì)算器軟件。前臺(tái)為類似于圖1的圖形界面,主要解決輸入界面問(wèn)題,可以解決簡(jiǎn)單計(jì)算問(wèn)題,將復(fù)雜函數(shù)計(jì)算如sin、開平方根等操作交給后臺(tái)服務(wù)器計(jì)算,計(jì)算后結(jié)果返回客戶端顯示,并將客戶端的IP地址,發(fā)來(lái)的計(jì)算請(qǐng)求記錄在一個(gè)日志文件calculate.log。2.實(shí)現(xiàn): 主類: 服務(wù)器類:CalculatorServer 客戶端類:CalculatorClient 輔類: 查看日志類:lookJFrame 圖形面板類:mydraws & mycanvas/客戶端

2、import .*;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;/數(shù)字private JButton

3、jia,jian,cheng,chu,xy,cos,sin,ln,ce,equ,bfh,point,zf,sqrt;/加減乘除各種運(yùn)算按鈕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

4、 JButton buttonconnect;private JMenuItem close,version,look,edit,word,background,caltuxing;/菜單欄 菜單項(xiàng)設(shè)置public CalculatorClient() super("馬康-計(jì)算器客戶端"); JMenuBar mb=new JMenuBar();/菜單欄 JMenu filemenu=new JMenu("計(jì)算器"); JMenu bigmenu=new JMenu("放大"); JMenu calmenu=new JMenu(&qu

5、ot;畫板"); JMenu vermenu=new JMenu("關(guān)于"); JMenu editmenu=new JMenu("編輯"); look=new JMenuItem("查看日志"); close=new JMenuItem("退出"); version=new JMenuItem("作者"); background=new JMenuItem("背景"); caltuxing=new JMenuItem("圖形畫板"); word

6、=new JMenuItem("放大"); 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

7、); close.addActionListener(this); background.addActionListener(this); word.addActionListener(this); version.addActionListener(this); caltuxing.addActionListener(this); socket=new Socket(); Image image = getToolkit().getImage(getClass().getResource("calculator.jpg"); setIconImage(image);/左上

8、角圖片設(shè)置 setSize(360, 250); setLocation(400, 300); setBackground(Color.LIGHT_GRAY);/背景設(shè)置 setLayout(new FlowLayout(FlowLayout.CENTER); setResizable(false); show=new JTextField(31); show.setText(""); show.setHorizontalAlignment(SwingConstants.RIGHT);/文本框內(nèi)容右對(duì)齊 show.setEditable(false); add(show);

9、 dispMain=new Panel(); add(dispMain); buttonconnect=new JButton("連接服務(wù)器"); labelconnect=new JLabel(""); add(buttonconnect); add(labelconnect); dispMain.setLayout(new GridLayout(1, 2, 10, 10); dispLeft=new Panel(); dispMain.add(dispLeft); dispLeft.setLayout(new GridLayout(4, 3, 3,

10、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數(shù)字按鈕 bl=new JButton(String.valueOf(l); bl.setForeground(Color.BLUE); dispLeft.add(bl); bl.addActionListener(this); /監(jiān)聽設(shè)置 bl.setFocusPainted(false); /去掉焦

11、點(diǎn)框 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");/清

12、除 equ=new JButton("=");/等號(hào) bfh=new JButton("%");/求模 point=new JButton(".");/小數(shù)點(diǎn) zf=new JButton(" +/- ");/正負(fù)號(hào) sqrt=new JButton("");/根號(hào) jia.setFocusPainted(false); /去掉焦點(diǎn)框 jian.setFocusPainted(false); /去掉焦點(diǎn)框 cheng.setFocusPainted(false); /去掉焦點(diǎn)框 chu.setF

13、ocusPainted(false); /去掉焦點(diǎn)框 xy.setFocusPainted(false); /去掉焦點(diǎn)框 cos.setFocusPainted(false); /去掉焦點(diǎn)框 sin.setFocusPainted(false); /去掉焦點(diǎn)框 ln.setFocusPainted(false); /去掉焦點(diǎn)框 ce.setFocusPainted(false); /去掉焦點(diǎn)框 equ.setFocusPainted(false); /去掉焦點(diǎn)框 bfh.setFocusPainted(false); /去掉焦點(diǎn)框 point.setFocusPainted(false); /

14、去掉焦點(diǎn)框 zf.setFocusPainted(false); /去掉焦點(diǎn)框 sqrt.setFocusPainted(false); /去掉焦點(diǎn)框 buttonconnect.setFocusPainted(false); /去掉焦點(diǎn)框 /右面板 運(yùn)算符按鈕 dispRight.add(chu); dispRight.add(sqrt); dispRight.add(ln); dispRight.add(cheng); dispRight.add(sin); dispRight.add(bfh); dispRight.add(jian); dispRight.add(cos); dispR

15、ight.add(ce); dispRight.add(jia); dispRight.add(xy); dispRight.add(equ); /左面板 正負(fù)號(hào)/小數(shù)點(diǎn)按鈕 dispLeft.add(zf); dispLeft.add(point); /按鈕監(jiān)聽設(shè)置 ce.addActionListener(this); jia.addActionListener(this); jian.addActionListener(this); cheng.addActionListener(this); chu.addActionListener(this); equ.addActionListe

16、ner(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); setDefaultCloseOpe

17、ration(JFrame.EXIT_ON_CLOSE); setVisible(true); d1=0.0;d2=0.0;op=0; 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.setV

18、isible(true); if(e.getSource()=background)/背景設(shè)置 Color color=JColorChooser.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

19、Font("宋體",Font.PLAIN,20);jia.setFont(new Font("宋體",Font.PLAIN,20); jian.setFont(new Font("宋體",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

20、.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); equ.setFont(new Font("宋體",Font.PLAIN,20); bfh.setFont(new Font("宋體",Font.PLAI

21、N,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);/關(guān)閉 /作者 if(e.getSource()=version) JOptionPane.showMessageDialog(null," 交互式計(jì)算器n Java實(shí)驗(yàn)程序作業(yè)n

22、作者: *n蘭州大學(xué)12電子商務(wù)"); /連接服務(wù)器 if(e.getSource()=buttonconnect)tryif(socket.isConnected()elseInetAddress address=InetAddress.getByName("127.0.0.1");/此類表示互聯(lián)網(wǎng)協(xié)議 (IP) 地址。 InetSocketAddress socketAddress=new InetSocketAddress(address.getLocalHost(),4444); /getLocalHost()返回本地主機(jī)socket.connect(so

23、cketAddress);/ connect(SocketAddress endpoint) 將此套接字連接到服務(wù)器 in=new DataInputStream(socket.getInputStream();/socket返回輸入流 out=new DataOutputStream(socket.getOutputStream();/socket返回輸出流 labelconnect.setText("已連接到服務(wù)器"); thread.start(); catch(IOException ee) /正負(fù) if(e.getSource()=zf) if(show.getT

24、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

25、 ee) /小數(shù)點(diǎn) if(e.getSource()=point) int f; String sshow=show.getText(); if(f=sshow.indexOf('.')!=-1)/若已存在小數(shù)點(diǎn) JOptionPane.showMessageDialog(null,"小數(shù)點(diǎn)已存在!"); else /不存在小數(shù)點(diǎn) if(sshow.equals("") show.setText("0."); else if(sshow.equals("-") show.setText("

26、-0."); else show.setText(show.getText()+"."); /清除 if(e.getSource()=ce) show.setText(""); /加法運(yùn)算符 if(e.getSource()=jia) opp="+" try d1=Double.parseDouble(show.getText(); op=0; show.setText(""); catch(Exception ee) /減法運(yùn)算符 if(e.getSource()=jian) opp="-&q

27、uot; try d1=Double.parseDouble(show.getText(); op=1; show.setText(""); catch(Exception ee) /乘法運(yùn)算符 if(e.getSource()=cheng) opp="*" try d1=Double.parseDouble(show.getText(); op=2; show.setText(""); catch(Exception ee) /除法運(yùn)算符 if(e.getSource()=chu) opp="/" try d1=

28、Double.parseDouble(show.getText(); op=3; show.setText(""); catch(Exception ee) / /服務(wù)器運(yùn)算的運(yùn)算符 / /x的y次方 if(e.getSource()=xy) opp="xy" try d1=Double.parseDouble(show.getText(); op=5; show.setText(""); catch(Exception ee) /cos運(yùn)算符 if(e.getSource()=cos) opp="cos" try

29、 d1=Double.parseDouble(show.getText(); op=6; show.setText(""); catch(Exception ee) /sin運(yùn)算符 if(e.getSource()=sin) opp="sin" try d1=Double.parseDouble(show.getText(); op=7; show.setText(""); catch(Exception ee) /ln運(yùn)算符 if(e.getSource()=ln) opp="ln" try d1=Double.

30、parseDouble(show.getText(); op=8; show.setText(""); catch(Exception ee) /百分比運(yùn)算符 if(e.getSource()=bfh) opp="%" try d1=Double.parseDouble(show.getText(); op=9; show.setText(""); catch(Exception ee) /根號(hào)運(yùn)算符 if(e.getSource()=sqrt) opp="" try d1=Double.parseDouble(s

31、how.getText(); op=4; show.setText(""); catch(Exception ee) /等號(hào) if(e.getSource()=equ) int a,b=1; /a的值用于服務(wù)器端的計(jì)算,傳到服務(wù)器端,不同的值代表不同的運(yùn)算符 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) JOp

32、tionPane.showMessageDialog(null,"除數(shù)不能為零,請(qǐng)重輸");b=0; else result=d1/d2; break; case 4: if(d2<=0) JOptionPane.showMessageDialog(null,"根號(hào)數(shù)值不能小于零,請(qǐng)重輸");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.r

33、eadDouble(); catch(IOException e1)labelconnect.setText("與服務(wù)器已斷開"); break; case 6: /cos a=6; out.writeInt(a); out.writeDouble(d2); try result=in.readDouble(); catch(IOException e2)labelconnect.setText("與服務(wù)器已斷開"); break; case 7: /sin a=7; out.writeInt(a); out.writeDouble(d2); try r

34、esult=in.readDouble(); catch(IOException e3)labelconnect.setText("與服務(wù)器已斷開"); break; case 8: /ln a=8; if(d2<=0) JOptionPane.showMessageDialog(null,"數(shù)值需大于零,請(qǐng)重輸");b=0; else out.writeInt(a); out.writeDouble(d2); try result=in.readDouble(); catch(IOException e4)labelconnect.setText

35、("與服務(wù)器已斷開"); break; case 9: /bfh a=9; out.writeInt(a); out.writeDouble(d1); out.writeDouble(d2); try result=in.readDouble(); catch(IOException e5)labelconnect.setText("與服務(wù)器已斷開"); break; default: if(b=1) show.setText(String.valueOf(result); else show.setText(""); a=1; /a

36、=1代表傳到服務(wù)器處進(jìn)行保存到日志中 out.writeInt(a); out.writeDouble(d1); out.writeDouble(d2); out.writeUTF(opp); out.writeDouble(result); catch(Exception ee1) /數(shù)值0-9 if(e.getSource()=b0) if(!(show.getText().equals("0")|show.getText().equals("-0") show.setText(show.getText()+"0"); if(e.

37、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");

38、else if(show.getText().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

39、.setText(show.getText()+"3"); if(e.getSource()=b4) if(show.getText().equals("0") show.setText("4"); else if(show.getText().equals("-0") show.setText("-4"); else show.setText(show.getText()+"4"); if(e.getSource()=b5) if(show.getText().equals

40、("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"); else if(show.getText().equals("-0&q

41、uot;) 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");

42、if(e.getSource()=b8) if(show.getText().equals("0") show.setText("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&quo

43、t;); else if(show.getText().equals("-0") show.setText("-9"); else show.setText(show.getText()+"9"); public static void main(String args) new CalculatorClient(); /查看日志 lookJFrame類 class lookJFrame extends JFrame private JTextArea txt; private Container con; public lookJF

44、rame() super("計(jì)算器日志"); con=this.getContentPane(); final Image image=getToolkit().getImage(getClass().getResource("27.jpg"); /final ImageIcon img=new ImageIcon("H:/星空物語(yǔ)/java實(shí)驗(yàn)/Java圖標(biāo)/19.jpg"); txt=new JTextArea()public void paintComponent(Graphics g) g.drawImage(image, 0

45、, 0, null); super.paintComponent(g); ;/文本域 txt.setOpaque(false);/設(shè)置為透明add(txt);try FileReader file=new FileReader("calculate.log"); BufferedReader in=new BufferedReader(file); String s="calculate.log" txt.append(s+"n"); while(s=in.readLine()!=null) txt.append(s+"n&

46、quot;); in.close(); file.close(); catch(IOException exp) txt.setText("無(wú)此文件"); setSize(600, 400); setLocation(270, 100); setVisible(true);/服務(wù)器端import java.io.*;import .*;import java.util.*;import java.text.*;public class CalculatorServerpublic static void main(String args)ServerSocket serve

47、r=null;ServerThread thread;Socket client=null; System.out.println("n-計(jì)算器 服務(wù)器-n");while(true)tryserver=new ServerSocket(4444);catch(IOException e1)System.out.println("正在監(jiān)聽");tryclient=server.accept();/server返回和客戶端相連接的SocketSystem.out.println("客戶的地址:"+client.getInetAddress();catch(IOException e)System.out.println("正在等待客戶");if(client!=null)new ServerThre

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論