記念一下個人Java課設
前言: 從上週週日到本週週六下午兩點,其間天天睡眠時間沒超過五小時(我以爲是比較累的,固然也是頗有收穫的一次經歷)
問題描述
**結合JavaSocket編程開發文本處理程序。java
一、自行下載最喜好的小說1部。存到服務器中,格式自定。通常存儲爲文本文檔。要求長篇小說,20萬字以上。舉例說明:下載《三國演義》保存在服務器端。ios
二、該軟件支持從服務器載入小說,對小說中的文本進行分析。舉例說明:服務器端保存《三國演義》,客戶端進行分析。正則表達式
三、首先運行服務器。服務器運行以後,能夠鏈接1個客戶端。編程
四、運行客戶端。用戶可以輸入暱稱,肯定,則鏈接到服務器。鏈接成功,便可出現功能界面。數組
客戶端功能界面以下:服務器
一、功能1:載入小說。可以選擇服務器端的小說。舉例說明:客戶端點擊按鈕,選擇服務器端的文件名,《三國演義》傳輸到客戶端。網絡
二、功能2:任意設置10我的姓名(能夠預設置在客戶端界面上),將這10我的在小說中的存在感進行排名,用柱狀圖表示。如何計算存在感?本身定義。點擊按鈕,存在感排名的柱狀圖能夠保存到服務器端。舉例說明:界面上設置「劉備、曹操、張飛、關羽、趙雲、諸葛亮、呂布、貂蟬、董卓、孫權」,點擊按鈕,出現一個柱狀圖,顯示存在感排名爲:劉備、曹操、張飛、關羽、諸葛亮、趙雲、孫權、呂布、董卓、貂蟬(只是舉例說明)。app
三、每一個人在小說中活躍的位置是不同的。任意輸入10人中的1人,顯示他在小說中出現的密度,畫出密度圖。建議用顏色深淺表示密度。點擊按鈕,密度圖能夠保存到服務器端。舉例說明:輸入「劉備」,在小說中前面部分密度大,後面部分密度小。框架
四、若是兩人在相距較短的一段文字中出現,咱們認爲兩人有關係,距離越短,關係越近;若是屢次在較短篇幅出現,則關係更近。對這10我的,根據他們關係的緊密程度進行歸類,看看能夠歸爲哪幾類?並用圖形形式直觀表示。如何定義關係緊密程度?本身定義。點擊按鈕,緊密程度歸類的圖像內容,能夠保存到服務器端。舉例說明:這10我的,自動分爲「劉備、張飛、關羽、趙雲、諸葛亮」以及「曹操」、「呂布、董卓、貂蟬」、「孫權」(只是舉例)。ssh
五、附加題,不檢查,作了不加分不減分。任意輸入一我的,顯示他最恨誰,最喜歡誰,最喜歡吃什麼,常常去哪裏活動,殺人多很少,是好人仍是壞人?
看與你的直覺是否符合?若是不太符合,說明可能的緣由。
檢查流程,共提三個問題:根據回答打分:檔次爲A+, A-,B+, B-,C。
問題1:功能2展現,以及解釋源代碼。
問題2:功能3展現,以及解釋源代碼。
問題3:功能4展現,以及解釋源代碼。
我的設計思路
簡單來講就是,客戶端,服務器端各一個類,因爲要設計出好看的圖形界面,全部不得不本身編寫一個Mypanel類繼承JPanel類,重寫裏面的paintComponent方法。此外,登陸界面,功能一二三各須寫一個類(面板類,繼承JPanel)功能四核心是本身的思想,圖像界面是次要的,所以須要額外編寫爲一個JFrame類而且實現Serializable藉口以便和服務器端交流,一樣,功能2,3的實現也要額外編寫一個能夠和服務器交流的類。最後須要編寫的就是能夠上傳文件給服務器端的upload函數,利用構造函數重載實現一個類傳三個功能的結果對象的功能,減小代碼量。
很少說,直接上代碼
1.首先是服務器類
package MYDESIGN; import java.net.*; import javax.swing.*; import java.io.*; //該類表明服務器 public class Server extends JFrame { public static ServerSocket ss; public static Socket socket; //服務器鏈接所需成員 private InputStream insClient; private BufferedReader brClient; private OutputStream osClient; private PrintStream psClient; //服務器與客戶端聯繫所需的輸入輸出流 private String order; //記錄命令的字符串 private static String Download = "!@#下載文件"; private static String Continue = "!@#繼續傳輸"; private static String Over = "!@#傳輸完成"; private static String Receive = "!@#接收客戶端文件"; //服務器端的基本指令集 Server(){ super("服務器端"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400,200); this.setVisible(true); try{ ss = new ServerSocket(9999); socket = ss.accept(); //等待客戶端的鏈接 insClient = socket.getInputStream(); osClient = socket.getOutputStream(); brClient = new BufferedReader(new InputStreamReader(insClient)); psClient = new PrintStream(osClient); //與客戶端創建聯繫 String Clientname = brClient.readLine(); this.setTitle(Clientname+"已經鏈接上"); }catch(Exception ex){ ex.printStackTrace(); } } //該構造函數完成的任務是,打開服務器,與客戶端創建聯繫,而且更新本身的標題 void downloadfile(){ File file = new File("D:\\CourseDesign\\WaterMargin.txt"); try{ FileReader fd = new FileReader(file); BufferedReader bfile = new BufferedReader(fd); String line; //文件讀入 String or; //命令讀入 while(true){ line = bfile.readLine(); if(line == null){ psClient.println(this.Over); break;} //傳輸完成就告訴客戶端已經傳輸完成了 psClient.println(line); //不斷傳輸一行信息 or = brClient.readLine(); if(or.equals(this.Continue)){ continue; //只有在收到能夠繼續傳輸的時候纔會繼續傳輸,避免速度不匹配 } } fd.close(); bfile.close(); //傳輸完成,關閉文件相關輸入輸出流 }catch(Exception ex){} } void receivefile(){ //接收客戶端傳來的文件信息 try{ String line = brClient.readLine(); FileOutputStream fos = new FileOutputStream("D:\\CourseDesign\\"+line+".txt"); PrintStream printfile = new PrintStream(fos); while(true){ line = brClient.readLine(); if(line.equals(Server.Over)) break; printfile.println(line); printfile.flush(); psClient.println(Server.Continue); } fos.close(); printfile.close(); }catch(Exception ex){} } //構造函數 void Receiver(){ try{ this.order = brClient.readLine(); //一直處於等待接受客戶端命令的狀態 if(this.order.equals(this.Download)){ downloadfile(); //調用downloadfile函數以異步通訊方式完成傳輸 }else if(this.order.equals(this.Receive)){ receivefile(); } }catch(Exception ex){} }//服務器端的接受命令函數,用於從客戶端接受命令而且作出相應處理 public static void main(String [] args)throws Exception{ Server mine = new Server(); while(true){ mine.Receiver(); //一直處於接受命令的狀態 } } }
-
客戶端類
package MYDESIGN; import java.net.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import MYDESIGN.*; //該類是客戶端,也就是功能端 public class Customer extends JFrame{ private INIT initINT; private FUNCTION function; public static Socket socket; public static InputStream insServer; public static BufferedReader brServer; public static OutputStream osServer; public static PrintStream psServer; public static boolean isdownload = false; Customer(){ initINT = new INIT(this.function);//INIT界面已經包含了讓客戶端與服務器端創建聯繫的操做 } //客戶端構造函數,用於打開登陸界面,鏈接服務器用 public static void main(String [] args){ new Customer(); } }
3.本身手寫的面板類,用於加入背景圖片
package MYDESIGN; import javax.swing.*; import java.awt.*; public class Mypanel extends JPanel{ //Mypanel繼承了JPanel使得其能夠設置背景圖片 private ImageIcon img; Mypanel(String filename){ this.img = new ImageIcon(filename); } //傳入文件路徑設置背景圖片 public void paintComponent(Graphics g){ g.drawImage(img.getImage(),0,0,this.getWidth(),this.getHeight(),this); } }
4.下面是登陸界面類
package MYDESIGN; import java.net.*; import javax.swing.*; import javax.swing.plaf.FontUIResource; import java.awt.*; import java.awt.event.*; import java.io.*; import MYDESIGN.*; //該類是初始登陸界面 public class INIT extends JFrame implements ActionListener{ //初始登錄界面 private Mypanel jpl = new Mypanel("D:\\CourseDesign2\\讀書.jpg"); private JLabel jlb1 = new JLabel("請輸入有效的服務器地址:"); private JTextField jtf1 = new JTextField(20); private JLabel jlb2 = new JLabel("請輸入您的暱稱(不然默認匿名用戶):"); private JTextField jtf2 = new JTextField(20); private JButton jbt1 = new JButton("登陸"); private JButton jbt2 = new JButton("取消"); //以上控件是客戶端登陸界面出現的控件 private String IPAD; public String nickname = "匿名用戶"; //用於存儲有效的界面信息的成員變量 private FUNCTION fun; INIT(FUNCTION funinter){ this.fun = funinter; jbt1.setFont(new Font("楷體",Font.PLAIN,17)); jbt2.setFont(new Font("楷體",Font.PLAIN,17)); jtf1.addActionListener(this); jtf2.addActionListener(this); jbt1.addActionListener(this); jbt2.addActionListener(this); jlb1.setForeground(Color.black); jlb1.setFont(new Font("楷體",Font.PLAIN,17)); jpl.add(jlb1); jpl.add(jtf1); jpl.add(jlb2); jlb2.setForeground(Color.black); jlb2.setFont(new Font("楷體",Font.PLAIN,17)); jpl.add(jtf2); jpl.add(jbt1); jpl.add(jbt2); this.add(jpl); //將這控件加入到初始面板上 this.setSize(300,400); this.setVisible(true); this.setTitle("登陸界面"); this.setLocation(700,300); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbt1){ this.IPAD = jtf1.getText(); this.nickname = jtf2.getText(); if(!IPAD.equals("127.0.0.1")){ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"沒法鏈接到有效服務器請從新輸入。"); jtf1.setText(""); } else{ try{ Customer.socket = new Socket(IPAD,9999); try{ Customer.insServer = Customer.socket.getInputStream(); Customer.osServer = Customer.socket.getOutputStream(); Customer.brServer = new BufferedReader(new InputStreamReader(Customer.insServer)); Customer.psServer = new PrintStream(Customer.osServer); //全局靜態輸入輸出流 }catch (Exception ex){} Customer.psServer.println(this.nickname); this.fun = new FUNCTION(this.nickname); UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"您能夠閱讀其餘書籍,但本功能只支持分析《水滸傳》"); this.dispose(); }catch(Exception ex){} } } else if(e.getSource()==jbt2){ this.dispose(); } } //針對不一樣狀況下不一樣的事件處理 }
5.功能主面板
package MYDESIGN; import javax.swing.*; import javax.swing.plaf.FontUIResource; import java.awt.event.*; import MYDESIGN.*; import java.awt.*; class FUNCTION extends JFrame implements ActionListener{ //功能界面,也是主框架 private JButton jbt1 = new JButton("功能1"); private JButton jbt2 = new JButton("功能2"); private JButton jbt3 = new JButton("功能3"); private JButton jbt4 = new JButton("功能4"); private JButton jbt5 = new JButton("退出"); private Mypanel jpl = new Mypanel("D:\\CourseDesign2\\fengmian.jpg"); private FUN1 fun1 = new FUN1(jpl,this); //功能1所要切換的面板,功能1最終要切換回來 private FUN2 fun2 = new FUN2(jpl,this); private FUN3 fun3 = new FUN3(jpl,this); private FUN4 fun4; FUNCTION(String name){ super("歡迎"+name+"登陸使用"); jbt1.setPreferredSize(new Dimension(100,50)); jbt2.setPreferredSize(new Dimension(100,50)); jbt3.setPreferredSize(new Dimension(100,50)); jbt4.setPreferredSize(new Dimension(100,50)); jbt5.setPreferredSize(new Dimension(100,50)); jbt1.setFont(new Font("宋體",Font.PLAIN,25)); jbt2.setFont(new Font("宋體",Font.PLAIN,25)); jbt3.setFont(new Font("宋體",Font.PLAIN,25)); jbt4.setFont(new Font("宋體",Font.PLAIN,25)); jbt5.setFont(new Font("宋體",Font.PLAIN,25)); jbt1.addActionListener(this); jbt2.addActionListener(this); jbt3.addActionListener(this); jbt4.addActionListener(this); jbt5.addActionListener(this); jpl.add(jbt1); jpl.add(new JPanel()); jpl.add(jbt2); jpl.add(new JPanel()); jpl.add(jbt3); jpl.add(new JPanel()); jpl.add(jbt4); jpl.add(new JPanel()); jpl.add(jbt5); this.add(jpl); this.setSize(800,950); this.setLocation(500,70); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==this.jbt1){ this.setContentPane(fun1); this.revalidate(); }else if(e.getSource()==this.jbt2){ this.setContentPane(fun2); this.revalidate(); }else if(e.getSource()==this.jbt3){ this.setContentPane(fun3); this.revalidate(); UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); JOptionPane.showMessageDialog(null,"本功能實現的是分析人物在書本中出現密度"); }else if(e.getSource()==this.jbt4){ if(Customer.isdownload==false){ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"客戶端暫時還未下載文件,請點擊功能1完成下載"); }else{ fun4 = new FUN4(); } }else if(e.getSource()==jbt5){ this.dispose(); } } }
6.功能1所要切換到的切換面板
package MYDESIGN; import javax.swing.*; import java.awt.event.*; import java.io.*; import MYDESIGN.*; import javax.swing.plaf.*; import java.awt.*; public class FUN1 extends Mypanel implements ActionListener{ //功能1的跳轉面板 public static JTextArea jta = new JTextArea(180,240); //而且能夠實現跳轉回主框架 private JScrollPane jsp = new JScrollPane(jta); private JButton jbtdl = new JButton("選擇讀本"); private JButton jbtex = new JButton("當即閱讀"); private JButton jbtf1 = new JButton("返回"); private BorderLayout bdl = new BorderLayout(30,30); private Mypanel update; private JFrame Father; private JPanel jplfun = new JPanel(); private JPanel gap = new JPanel(); public static int linenum = 0; public static boolean isread = false; FUN1(Mypanel father,JFrame JF){ super("D:\\CourseDesign2\\WaterMargin.jfif"); this.jta.setLineWrap(true); this.update = father; this.Father = JF; this.setLayout(bdl); this.jta.setFont(new Font("楷體",Font.PLAIN,19)); this.jbtf1.addActionListener(this); this.jbtdl.addActionListener(this); this.jbtex.addActionListener(this); this.jbtf1.setPreferredSize(new Dimension(180,80)); this.jbtf1.setFont(new Font("宋體",Font.PLAIN,30)); this.jbtdl.setPreferredSize(new Dimension(220,80)); this.jbtdl.setFont(new Font("宋體",Font.PLAIN,30)); this.jbtex.setPreferredSize(new Dimension(220,80)); this.jbtex.setFont(new Font("宋體",Font.PLAIN,30)); this.jplfun.add(jbtdl); this.jplfun.add(gap); this.jplfun.add(jbtex); this.jplfun.add(new JPanel()); this.jplfun.add(jbtf1); this.add(jplfun,bdl.SOUTH); this.add(jsp,bdl.CENTER); this.add(new JPanel(),BorderLayout.NORTH); this.add(new JPanel(),BorderLayout.EAST); this.add(new JPanel(),BorderLayout.WEST); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbtf1){ Father.setContentPane(this.update); //將要設置父框架的父面板 Father.revalidate(); }else if(e.getSource()==jbtdl){ new DIA(this.Father,"讀本選擇",true); }else if(e.getSource()==jbtex){ File file = new File("D:\\CourseDesign2\\水滸.txt"); try{ FileReader fd = new FileReader("D:\\CourseDesign2\\水滸.txt"); BufferedReader bff = new BufferedReader(fd); String linefile; while(true){ linefile = bff.readLine(); if(linefile == null) break; this.jta.append(linefile); this.jta.append("\n");} jta.setCaretPosition(0); //將光標移到初始位置,符合讀者要求 fd.close(); bff.close(); }catch(Exception ex){} } } } class DIA extends JDialog implements ActionListener{ //功能1附加窗口 private JLabel jlb = new JLabel("請選擇想得到哪本讀物"); private JComboBox jcb = new JComboBox(); private JButton jbt = new JButton("肯定"); private Mypanel jpladd = new Mypanel("D:\\CourseDesign2\\book.jpeg"); private Mypanel jpl = new Mypanel("D:\\CourseDesign2\\choosecover.png"); private FileOutputStream filefos; private PrintStream fileps; //存儲到文件的相關流 DIA(Frame own,String s,boolean bool){ super(own,s,bool); this.jpl.setLayout(new BorderLayout()); this.jpladd.add(jcb); this.jpl.add(jlb,BorderLayout.NORTH); this.jpl.add(jpladd,BorderLayout.CENTER); this.jpl.add(jbt,BorderLayout.SOUTH); jbt.addActionListener(this); this.jcb.addItem("三國演義"); this.jcb.addItem("水滸傳"); this.jcb.addItem("紅樓夢"); this.jcb.addItem("西遊記"); this.jlb.setPreferredSize(new Dimension(450,50)); this.jlb.setFont(new Font("楷體",Font.BOLD,25)); this.jcb.setPreferredSize(new Dimension(450,58)); this.jcb.setFont(new Font("楷體",Font.PLAIN,25)); this.jbt.setPreferredSize(new Dimension(330,50)); this.jbt.setFont(new Font("楷體",Font.PLAIN,27)); this.add(jpl); this.setSize(550,500); this.setLocation(625,300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e){ String s = (String)jcb.getSelectedItem(); if(s.equals("水滸傳")){ try{ Customer.psServer.println("!@#下載文件"); //將傳輸文件的命令發送出去 filefos = new FileOutputStream("D:\\CourseDesign2\\水滸.txt"); fileps = new PrintStream(filefos); String line; while(true){ line = Customer.brServer.readLine(); if(line.equals("!@#傳輸完成")){ break;} fileps.println(line); fileps.flush(); if(FUN1.isread==false) FUN1.linenum++; Customer.psServer.println("!@#繼續傳輸"); } Customer.isdownload = true; //下載完成,告訴客戶端已經下載好能夠訪問了 FUN1.isread = true; //爲後繼不重複計算行數作依據 filefos.close(); fileps.close(); //關閉全部的輸入輸出流,異步通訊結束 this.dispose(); }catch(Exception ex){} }else{ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"很抱歉"+s+"暫時沒有相關資源"); } } }
7.功能2所要切換的面板
package MYDESIGN; import MYDESIGN.*; import java.io.*; import java.awt.*; import javax.swing.*; import javax.swing.plaf.*; import java.awt.event.*; import java.util.regex.*; class graph extends JFrame{ //人物圖像顯示框架類 private Mypanel mp; graph(String name,String path){ super(name); this.mp= new Mypanel(path); this.add(mp); this.setSize(400,400); this.setLocation(700,310); this.setVisible(true); } } public class FUN2 extends Mypanel implements ActionListener{ private JFrame father; private JPanel jplorigin; //切換所需主框架和主面板 private JButton jbt0 = new JButton("人物存在感分析"); private JButton jbt1 = new JButton("宋江"); private JButton jbt2 = new JButton("晁蓋"); private JButton jbt3 = new JButton("武松"); private JButton jbt4 = new JButton("吳用"); private JButton jbt5 = new JButton("李逵"); private JButton jbt6 = new JButton("林沖"); private JButton jbt7 = new JButton("魯智深"); private JButton jbt8 = new JButton("呼延灼"); private JButton jbt9 = new JButton("柴進"); private JButton jbt10 = new JButton("盧俊義"); //所選的十我的物 private JButton jbt11 = new JButton("分析結果查看"); private JButton jbt12 = new JButton("上傳結果"); private JButton jbt13 = new JButton("返回"); private JPanel jpl1 = new JPanel(); private JPanel jpl2 = new JPanel(); private JPanel jpl3 = new JPanel(); private int[] num = new int[10]; //十個數字分別標記以前順序下的十個角色出現次數 private int sum = 0; //爲了求出佔比而須要的輔助的總和記錄 private FileReader fdr; private BufferedReader bfder; public float[] ratio = new float[10]; //記錄這十位角色的佔比 private boolean flag = false; //記錄是第一次調用仍是第二次調用 後期優化的時候再來搞 public String[] ss = {"宋江","晁蓋","武松","吳用","李逵","林沖","魯智深","呼延灼","柴進","盧俊義"}; private Pattern[] p = new Pattern[10];//模式類數組 private Matcher[] m = new Matcher[10]; //匹配類 private graphvis gpv; private void sort(int num,String[] name,float[] ratio){ //對所得比率進行大小排序,按照從低到高的順序 int pos; float temp1; String temp2; for(int i=0;i<num-1;i++){ pos = i; for(int j = i+1;j<num;j++){ if(ratio[j]<ratio[pos]) pos = j; } if(pos == i){ continue; }else{ temp1 = ratio[i]; ratio[i] = ratio[pos]; ratio[pos]=temp1; temp2 = name[i]; name[i] = name[pos]; name[pos] = temp2; //交換實現 } } } FUN2(JPanel jp,JFrame JF){ super("D:\\CourseDesign2\\FUN2.jpg"); for(int i:this.num){ i = 0; //初始化出現次數 } this.father = JF; this.jplorigin = jp; this.setLayout(new BorderLayout(20,20)); jpl1.setBackground(null); jpl1.setOpaque(false); jpl2.setBackground(null); jpl2.setOpaque(false); jpl2.setLayout(new GridLayout(6,3,20,30)); jpl3.setBackground(null); jpl3.setOpaque(false); jpl1.add(jbt0); jpl2.add(jbt1); jpl2.add(jbt2); jpl2.add(jbt3); jpl2.add(jbt4); jpl2.add(jbt5); jpl2.add(jbt6); jpl2.add(jbt7); jpl2.add(jbt8); jpl2.add(jbt9); jpl2.add(jbt10); jpl3.add(jbt11); jpl3.add(jbt12); jpl3.add(jbt13); jbt0.setPreferredSize(new Dimension(600,60)); jbt0.setFont(new Font("楷體",Font.BOLD,50)); jbt0.setContentAreaFilled(false); jbt0.setForeground(Color.yellow); jbt1.setPreferredSize(new Dimension(200,80)); jbt1.setFont(new Font("楷體",Font.BOLD,50)); jbt1.setContentAreaFilled(false); jbt1.setForeground(Color.green); jbt1.addActionListener(this); jbt2.setPreferredSize(new Dimension(200,80)); jbt2.setFont(new Font("楷體",Font.BOLD,50)); jbt2.setContentAreaFilled(false); jbt2.setForeground(Color.green); jbt2.addActionListener(this); jbt3.setPreferredSize(new Dimension(200,80)); jbt3.setFont(new Font("楷體",Font.BOLD,50)); jbt3.setContentAreaFilled(false); jbt3.addActionListener(this); jbt3.setForeground(Color.green); jbt4.setPreferredSize(new Dimension(200,80)); jbt4.setFont(new Font("楷體",Font.BOLD,50)); jbt4.setContentAreaFilled(false); jbt4.setForeground(Color.green); jbt4.addActionListener(this); jbt5.setPreferredSize(new Dimension(200,80)); jbt5.setFont(new Font("楷體",Font.BOLD,50)); jbt5.setContentAreaFilled(false); jbt5.setForeground(Color.green); jbt5.addActionListener(this); jbt6.setPreferredSize(new Dimension(200,80)); jbt6.setFont(new Font("楷體",Font.BOLD,50)); jbt6.setContentAreaFilled(false); jbt6.setForeground(Color.green); jbt6.addActionListener(this); jbt7.setPreferredSize(new Dimension(200,80)); jbt7.setFont(new Font("楷體",Font.BOLD,50)); jbt7.setContentAreaFilled(false); jbt7.setForeground(Color.green); jbt7.addActionListener(this); jbt8.setPreferredSize(new Dimension(200,80)); jbt8.setFont(new Font("楷體",Font.BOLD,50)); jbt8.setContentAreaFilled(false); jbt8.setForeground(Color.green); jbt8.addActionListener(this); jbt9.setPreferredSize(new Dimension(200,80)); jbt9.setFont(new Font("楷體",Font.BOLD,50)); jbt9.setContentAreaFilled(false); jbt9.setForeground(Color.green); jbt9.addActionListener(this); jbt10.setPreferredSize(new Dimension(200,80)); jbt10.setFont(new Font("楷體",Font.BOLD,50)); jbt10.setContentAreaFilled(false); jbt10.setForeground(Color.green); jbt10.addActionListener(this); jbt11.setPreferredSize(new Dimension(210,80)); jbt11.setFont(new Font("楷體",Font.BOLD,25)); jbt11.setContentAreaFilled(false); jbt11.setForeground(Color.red); jbt11.addActionListener(this); jbt12.setPreferredSize(new Dimension(210,80)); jbt12.setFont(new Font("楷體",Font.BOLD,25)); jbt12.setContentAreaFilled(false); jbt12.setForeground(Color.red); jbt12.addActionListener(this); jbt13.setPreferredSize(new Dimension(130,80)); jbt13.setFont(new Font("楷體",Font.BOLD,25)); jbt13.setContentAreaFilled(false); jbt13.setForeground(Color.red); jbt13.addActionListener(this); this.add(jpl1,BorderLayout.NORTH); this.add(jpl2,BorderLayout.CENTER); this.add(jpl3,BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbt1){ new graph("宋江","D:\\CourseDesign2\\songjiang.jpg"); }else if(e.getSource()==jbt2){ new graph("晁蓋","D:\\CourseDesign2\\chaogai.jfif"); }else if(e.getSource()==jbt3){ new graph("武松","D:\\CourseDesign2\\wusong.jpg"); }else if(e.getSource()==jbt4){ new graph("吳用","D:\\CourseDesign2\\wuyong.jpg"); }else if(e.getSource()==jbt5){ new graph("李逵","D:\\CourseDesign2\\likui.jpg"); }else if(e.getSource()==jbt6){ new graph("林沖","D:\\CourseDesign2\\linchong.jpg"); }else if(e.getSource()==jbt7){ new graph("魯智深","D:\\CourseDesign2\\luzhishen.jpeg"); }else if(e.getSource()==jbt8){ new graph("呼延灼","D:\\CourseDesign2\\huyanzhuo.jfif"); }else if(e.getSource()==jbt9){ new graph("柴進","D:\\CourseDesign2\\chaijin.jpg"); }else if(e.getSource()==jbt10){ new graph("盧俊義","D:\\CourseDesign2\\lujunyi.jpg"); }else if(e.getSource()==jbt11){ if(Customer.isdownload == false){ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"客戶端暫時還未下載文件,請點擊功能1完成下載"); }else{ File file = new File("D:\\CourseDesign2\\水滸.txt"); try{ fdr = new FileReader(file); bfder = new BufferedReader(fdr); //文件讀入流 String line; //中轉的字符串 for(int n = 0;n<10;n++) p[n] = Pattern.compile(ss[n]); //創建模式匹配關係 while(true){ try{ line = bfder.readLine(); if(line == null) break; //下面將使用基於處理複雜字符串的正則表達式原理的regex包下的兩個重要類 for(int n = 0;n<10;n++){ m[n] = p[n].matcher(line); //創建對應匹配關係 while(m[n].find()) num[n]++; //各個字符串的匹配,而且記錄到num數組 } }catch(Exception ex){} } //while fdr.close(); bfder.close(); //關閉文件輸入流 for(int i = 0;i<10;i++) sum +=num[i]; for(int i = 0;i<10;i++) ratio[i] = ((float)num[i])/sum;//防止地板除法形成精度損失,獲得每一個人的存在感百分比 for(int i = 0;i<10;i++) ratio[i]=(float)Math.round(ratio[i]*1000)/1000; //將精度控制 sort(10,ss,ratio); //排序 this.gpv = new graphvis(10,ss,ratio); }catch(Exception ex){} } //else情形 } //jbt11處理 else if(e.getSource()==jbt12){ if(Customer.isdownload == false){ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"客戶端暫時還未下載水滸傳,請點擊功能1完成下載"); }else new upload("功能2繪圖",this.gpv).send2(); }//jbt12處理 else if(e.getSource()==jbt13){ this.father.setContentPane(this.jplorigin); this.revalidate(); } } }
8.功能2柱狀圖顯示類
package MYDESIGN; import MYDESIGN.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; import java.awt.event.*; class gpanel extends JPanel implements ActionListener{ String[] names; int num; float[] ratios; JFrame father; private JButton jbt = new JButton("返回"); gpanel(int number,String[] ss,float[] ritio,JFrame f){ //number是將顯示的數量給劃分座標提供條件 this.father = f; this.num = number; this.ratios = ritio; this.names = ss; this.jbt.setPreferredSize(new Dimension(200,50)); this.jbt.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt.addActionListener(this); this.add(jbt); } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setStroke(new BasicStroke(2.0f)); g2.setColor(Color.green); g2.drawLine(150,900,1350,900); g2.drawLine(1340,910,1350,900); g2.drawLine(1340,890,1350,900); g2.drawLine(150,900,150,80); g2.drawLine(140,90,150,80); g2.drawLine(160,90,150,80); //繪製基本座標軸 g2.setStroke(new BasicStroke(0.5f)); g2.setColor(Color.gray); g2.drawLine(150,750,1350,750); g2.drawLine(150,600,1350,600); g2.drawLine(150,450,1350,450); g2.drawLine(150,300,1350,300); g2.drawLine(150,150,1350,150); //獲得比率座標軸的切線方向,便於定位 for(int i = 0;i<num;i++){ //繪製統計圖 g2.setColor(Color.red); g2.fillRect(200+110*i, 900-(int)(this.ratios[i]*1500), 45,(int)(this.ratios[i]*1500)); g2.setColor(Color.black); g2.setFont(new Font("楷體",Font.PLAIN,20)); g2.drawString(names[i], 202+110*i, 930); g2.setFont(new Font("楷體",Font.PLAIN,20)); g2.drawString((ratios[i]*100)+"%",205+110*i,886-(int)(this.ratios[i]*1500)); } g2.setColor(Color.gray); for(int i=0;i<5;i++){ g2.drawString(10*(i+1)+"%",115, 755-150*i); } //繪製基準 } public void actionPerformed(ActionEvent e){ father.dispose(); } } //柱狀圖的顯示面板 public class graphvis extends JFrame implements Serializable{ //能夠進行網絡傳輸 private gpanel gp; graphvis(int number,String[] name,float[] ratios){ this.gp = new gpanel(number,name,ratios,this); this.setTitle("人物存在感分析"); this.add(this.gp); this.setSize(1400,1000); this.setLocation(310,50); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } }
9.功能3所要切換的面板類
package MYDESIGN; import javax.swing.*; import javax.swing.plaf.FontUIResource; import java.io.*; import java.util.regex.Pattern; import java.awt.*; import java.awt.event.*; import MYDESIGN.*; import java.util.regex.*; public class FUN3 extends Mypanel implements ActionListener{ private JFrame JFather; private JPanel jfather; private JTextField jtf = new JTextField(20); private JButton jbt1 = new JButton("肯定"); private JButton jbt2 = new JButton("上傳結果"); private JButton jbt3 = new JButton("返回"); private JPanel jpl = new JPanel(); private float[] num = new float[10]; //分紅10組記錄出現的次數 private densityvis dsv; private boolean canupload = false; FUN3(Mypanel jp,JFrame jf){ super("D:\\CourseDesign2\\FUN3.jpg");//加個圖片 this.JFather = jf; this.jfather = jp; this.setLayout(new BorderLayout()); this.jbt1.setPreferredSize(new Dimension(200,80)); this.jbt1.setFont(new Font("楷體",Font.PLAIN,45)); this.jbt2.setPreferredSize(new Dimension(300,80)); this.jbt2.setFont(new Font("楷體",Font.PLAIN,45)); this.jbt3.setPreferredSize(new Dimension(200,80)); this.jbt3.setFont(new Font("楷體",Font.PLAIN,45)); this.jbt1.addActionListener(this); this.jbt2.addActionListener(this); this.jbt3.addActionListener(this); this.jpl.add(jbt1); this.jpl.add(jbt2); this.jpl.add(jbt3); this.add(jtf,BorderLayout.NORTH); this.add(jpl,BorderLayout.SOUTH); jtf.setFont(new Font("楷體",Font.PLAIN,35)); jtf.setPreferredSize(new Dimension(300,70)); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbt1){ if(Customer.isdownload==false){ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"客戶端暫時還未下載文件,請點擊功能1完成下載"); } else{ this.canupload = true; String jtfname=this.jtf.getText(); String[] ss={"宋江","晁蓋","柴進","呼延灼","盧俊義","李逵","吳用","武松","魯智深","林沖"}; boolean exist = false; for(String s:ss){ if(jtfname.equals(s)) exist = true; } if(exist==false){ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"很抱歉"+jtfname+"不在本次分析範圍內"); this.jtf.setText(""); }else{ try{ FileReader fd = new FileReader("D:\\CourseDesign2\\水滸.txt"); BufferedReader bfr = new BufferedReader(fd); String line; //讀取的每一行 Pattern pname = Pattern.compile(jtfname); Matcher mch; //創建正則表達式下的字符串匹配機制 for(int i=0;i<10;i++) num[i]=0.0f; //初始化num[]數組 for(int i=0;i<10;i++){ //求出每一個部分這個角色出現的次數 for(int j =1;j<=(FUN1.linenum/10);j++){ //讀入一組計算次數 line = bfr.readLine(); if(line==null) break; else{ mch = pname.matcher(line); while(mch.find()) num[i]+=1.0f; }//else }//for }//for fd.close(); bfr.close(); }catch(Exception ex){} dsv = new densityvis(this.num,jtfname); }//當名稱屬於這十我的的時候調用的else } //只有下載完才能夠分析,與Customer的isdownload一致 } //來自jtf的事件處理 if(e.getSource()==jbt2){ if(Customer.isdownload == false){ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"客戶端暫時還未下載水滸傳,請點擊功能1完成下載"); }else { if(this.canupload) new upload("功能3"+jtf.getText()+"出現密度圖",this.dsv).send3(); else{ UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 20))); JOptionPane.showMessageDialog(null,"您暫時尚未分析過某個角色"); } } }//jbt2處理 if(e.getSource()==jbt3){ this.jtf.setText(""); JFather.setContentPane(jfather); JFather.revalidate(); } } }
10.功能3密度圖所要切換的類
package MYDESIGN; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; import java.awt.event.*; class dpanel extends JPanel implements ActionListener{ //密度圖分析 private JButton jbt1 = new JButton("返回"); private float[] num; private float[] ratios=new float[10]; private float sum=0.0f; //畫圖的依據 private JFrame father; private String name; private JPanel jpl = new JPanel(); dpanel(JFrame jf,float numbers[],String character){ this.setLayout(new BorderLayout()); this.father=jf; this.num = numbers; this.name = character; jbt1.setFont(new Font("楷體",Font.PLAIN,35)); jbt1.setPreferredSize(new Dimension(900,60)); this.jpl.add(jbt1); this.add(jpl,BorderLayout.NORTH); for(int i=0;i<10;i++) sum+=num[i]; for(int i =0;i<10;i++) ratios[i] =255.0f*(num[i]/sum); //獲得顏色程度的依據 this.jbt1.addActionListener(this); } public void paint(Graphics g){ Graphics2D g2 =(Graphics2D)g; g2.setStroke(new BasicStroke(2.0f)); g2.setColor(Color.black); g2.drawLine(100,550,700,550); g2.drawLine(690,540,700,550); g2.drawLine(690,560,700,550); g2.setFont(new Font("楷體",Font.PLAIN,40)); g2.drawString("開頭", 80, 600); g2.drawString("結束", 680, 600); for(int i =0 ;i<10;i++){ g2.setColor(new Color(0,140-(int)ratios[i]/2,0)); g2.fillRect(150+50*i,150,50,400); } g2.setColor(new Color(0,80,0)); g2.fillRect(780,120,25,25); g2.setFont(new Font("楷體",Font.PLAIN,20)); g2.setColor(Color.BLACK); g2.drawString("出現密度大",740,180); g2.setColor(new Color(0,230,0)); g2.fillRect(780,270,25,25); g2.setFont(new Font("楷體",Font.PLAIN,20)); g2.setColor(Color.BLACK); g2.drawString("出現密度小",740,330); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbt1){ this.father.dispose(); } } } public class densityvis extends JFrame implements Serializable{ //能夠進行網絡傳輸 private dpanel dp; densityvis(float[] number,String name){ this.dp = new dpanel(this,number,name); this.setTitle(name+"出現密度分佈"); this.add(this.dp); this.setSize(900,680); this.setLocation(520,170); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } }
11.功能4所要切換的面板
package MYDESIGN; import MYDESIGN.*; import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.regex.*; public class FUN4 extends JFrame implements Serializable,ActionListener{ private JButton jbt1 = new JButton("返回"); private JButton jbt2 = new JButton("上傳分組結果"); private JButton jbt3 = new JButton("人物關係分組結果"); private JButton jbt4 = new JButton("機率矩陣"); private JButton jbt5 = new JButton("宋江"); private JButton jbt6 = new JButton("晁蓋"); private JButton jbt7 = new JButton("吳用"); private JButton jbt8 = new JButton("李逵"); private JButton jbt9 = new JButton("柴進"); private JButton jbt10 = new JButton("呼延灼"); private JButton jbt11 = new JButton("盧俊義"); private JButton jbt12 = new JButton("魯智深"); private JButton jbt13 = new JButton("林沖"); private JButton jbt14 = new JButton("武松"); private JPanel jplsouth = new JPanel(); private JPanel jplnorth = new JPanel(); private JPanel jplcenter = new JPanel(); private JPanel jpl1=new JPanel(); private JPanel jpl2 = new JPanel(); private JPanel jpl3 = new JPanel(); private JPanel jpl4 = new JPanel(); private JPanel mjp = new JPanel(); //主界面 private String[] ss = {"宋江","晁蓋","吳用","李逵","柴進","呼延灼","盧俊義","魯智深","林沖","武松"}; private Pattern[] p = new Pattern[10]; private Matcher[] mch = new Matcher[10]; private int[] num = new int[10]; //ss[i]出現的區間總數 private int[][] fdship = new int[10][10]; //在i出現狀況下j出現就記錄親密度矩陣 private float[][] cdlikelyhood = new float[10][10]; //條件機率矩陣 FUN4(){ mjp.setLayout(new BorderLayout()); this.add(mjp); this.jbt1.setPreferredSize(new Dimension(200,55)); this.jbt1.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt2.setPreferredSize(new Dimension(300,55)); this.jbt2.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt3.setPreferredSize(new Dimension(800,50)); this.jbt3.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt3.setContentAreaFilled(false); this.jbt4.setPreferredSize(new Dimension(300,55)); this.jbt4.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt5.setPreferredSize(new Dimension(300,55)); this.jbt5.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt6.setPreferredSize(new Dimension(300,55)); this.jbt6.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt7.setPreferredSize(new Dimension(300,55)); this.jbt7.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt8.setPreferredSize(new Dimension(300,55)); this.jbt8.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt9.setPreferredSize(new Dimension(300,55)); this.jbt9.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt10.setPreferredSize(new Dimension(300,55)); this.jbt10.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt11.setPreferredSize(new Dimension(300,55)); this.jbt11.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt12.setPreferredSize(new Dimension(300,55)); this.jbt12.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt13.setPreferredSize(new Dimension(300,55)); this.jbt13.setFont(new Font("楷體",Font.PLAIN,40)); this.jbt14.setPreferredSize(new Dimension(300,55)); this.jbt14.setFont(new Font("楷體",Font.PLAIN,40)); jbt1.addActionListener(this); jbt2.addActionListener(this); jbt4.addActionListener(this); this.jplnorth.setLayout(new BorderLayout()); this.jplsouth.add(jbt2); this.jplsouth.add(jbt4); this.jplsouth.add(jbt1); this.jplnorth.add(jbt3,BorderLayout.CENTER); mjp.add(jplsouth,BorderLayout.SOUTH); mjp.add(jplnorth,BorderLayout.NORTH); this.setSize(1000,800); this.setLocation(380,150); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); mjp.add(jplcenter,BorderLayout.CENTER); this.jplcenter.setLayout(new GridLayout(4,1)); this.jpl1.add(jbt5); this.jpl1.add(jbt6); this.jpl1.add(jbt7); this.jpl1.add(jbt9); this.jplcenter.add(jpl1); this.jpl2.add(jbt8); this.jpl2.add(jbt10); this.jpl2.add(jbt11); this.jplcenter.add(jpl2); this.jpl3.add(jbt12); this.jpl3.add(jbt13); this.jplcenter.add(jpl3); this.jpl4.add(jbt14); this.jplcenter.add(jpl4); for(int i = 0;i<10;i++){ num[i]=0; p[i]=Pattern.compile(ss[i]); //初始化模式串 for(int j = 0;j<10;j++) fdship[i][j]=0; } //相關變量的初始化操做 } public void getclassification(){ try{ FileReader fr = new FileReader("D:\\CourseDesign2\\水滸.txt"); BufferedReader br = new BufferedReader(fr); boolean[] isshowup = new boolean[10]; boolean flag =false; //while循環輔助標誌,表示已經讀完文件 String line; StringBuffer lines = new StringBuffer(); //記錄區間的容器 while(true){ for(int i =0;i<10;i++) isshowup[i]=false; //每一個斷定區間在開始前初始化是否出現 for(int j = 0;j<20;j++){ //20行做爲一個區間 line = br.readLine(); lines.append(line); if(line==null){ flag =true; break;} } for(int i=0;i<10;i++){ mch[i]=p[i].matcher(lines); while(mch[i].find()) isshowup[i]=true;}//判斷每行ss[i]是否出現 for(int k =0;k<10;k++) if(isshowup[k]) num[k]++; //出現了就把出現區間總數+1 for(int i =0;i<10;i++){ if(isshowup[i]) for(int j=0;j<10;j++){ if(isshowup[j]&&(i!=j)) fdship[i][j]++; //計算在ss[i]出現的條件下ss[j]出現的機率 } } if(flag) break;}//while true 循環 獲得相應的num[]總區間數和相關親密度矩陣 for(int i=0;i<10;i++) for(int j=0;j<10;j++){ cdlikelyhood[i][j]=((float)fdship[i][j])/num[i]; //ss[i]出現的條件下ss[j]出現機率 cdlikelyhood[i][j]=(float)Math.round(cdlikelyhood[i][j]*1000)/1000;} //控制精度 for(int i = 0;i<10;i++){ for(int j =0;j<10;j++) System.out.print(cdlikelyhood[i][j]+" "); System.out.print("\n");} fr.close(); br.close(); }catch(Exception ex){} } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbt1){ this.dispose(); }else if(e.getSource()==jbt2){ new upload("功能4關係分組",this).send4();; } else if(e.getSource()==jbt4){ this.getclassification(); } } }
12.最後是upload函數類
package MYDESIGN; import java.awt.Font; import java.io.*; import java.net.*; import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.plaf.FontUIResource; public class upload { //upload類是一個用於傳輸信息給服務器端的類,和功能2,3,4搭配使用 private ObjectOutputStream oops; //將接收到的類輸出到文件 private BufferedReader bfr; //從文件讀對象 private graphvis gpv; //接收功能2的柱狀圖 private densityvis dvs; private FUN4 fun4; private String path; //告訴服務器端以何種文件名存儲 upload(String ph,graphvis gp){ this.path = ph; this.gpv = gp; } upload(String ph,densityvis ds){ this.path = ph; this.dvs = ds; } upload(String ph,FUN4 Jfun){ this.path = ph; this.fun4 = Jfun; } public void send2(){ try{ FileOutputStream fos = new FileOutputStream("D:\\CourseDesign2\\功能2.txt"); oops = new ObjectOutputStream(fos); bfr = new BufferedReader(new FileReader("D:\\CourseDesign2\\功能2.txt")); oops.writeObject(this.gpv); String fileline; String orderline; Customer.psServer.println("!@#接收客戶端文件"); //向服務器端發送上傳文件指令 Customer.psServer.println(this.path); //向服務器端的receivefile方法發送存儲地址 while(true){ fileline = bfr.readLine(); if(fileline == null){ Customer.psServer.println("!@#傳輸完成"); break;} else{ Customer.psServer.println(fileline); if(Customer.brServer.readLine()=="!@#繼續傳輸") continue; } } fos.close();; oops.close(); bfr.close(); UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); JOptionPane.showMessageDialog(null,"文件上傳已經完成"); }catch(Exception ex){} } public void send3(){ try{ FileOutputStream fos = new FileOutputStream("D:\\CourseDesign2\\功能3.txt"); oops = new ObjectOutputStream(fos); bfr = new BufferedReader(new FileReader("D:\\CourseDesign2\\功能3.txt")); oops.writeObject(this.dvs); String fileline; String orderline; Customer.psServer.println("!@#接收客戶端文件"); //向服務器端發送上傳文件指令 Customer.psServer.println(this.path); //向服務器端的receivefile方法發送存儲地址 while(true){ fileline = bfr.readLine(); if(fileline == null){ Customer.psServer.println("!@#傳輸完成"); break;} else{ Customer.psServer.println(fileline); if(Customer.brServer.readLine()=="!@#繼續傳輸") continue; } } fos.close();; oops.close(); bfr.close(); UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); JOptionPane.showMessageDialog(null,"文件上傳已經完成"); }catch(Exception ex){} } public void send4(){ try{ FileOutputStream fos = new FileOutputStream("D:\\CourseDesign2\\功能4.txt"); oops = new ObjectOutputStream(fos); bfr = new BufferedReader(new FileReader("D:\\CourseDesign2\\功能4.txt")); oops.writeObject(this.fun4); String fileline; String orderline; Customer.psServer.println("!@#接收客戶端文件"); //向服務器端發送上傳文件指令 Customer.psServer.println(this.path); //向服務器端的receivefile方法發送存儲地址 while(true){ fileline = bfr.readLine(); if(fileline == null){ Customer.psServer.println("!@#傳輸完成"); break;} else{ Customer.psServer.println(fileline); if(Customer.brServer.readLine()=="!@#繼續傳輸") continue; } } fos.close();; oops.close(); bfr.close(); UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷體", Font.BOLD, 14))); JOptionPane.showMessageDialog(null,"文件上傳已經完成"); }catch(Exception ex){} } }
結語
這周恰巧遇到java課設,彙編實踐課,高級程序設計實踐,外加科學計算Gauss列主元消去法C語言實現 一塊兒佈置任務,要不斷在面向對象,面向過程和麪向CPU之間切換。總之有點暈乎乎,不過總算把java課設搞完了,這是後話了.......