資源簡介
之所以要3分,因為還是挺辛苦的,實現功能包括用戶注冊、登陸、修改密碼、通信界面、上傳下載文件;直接解壓導入.project到eclipse就能用,不需要任何其他包,下載有什么問題可以直接提問,也可以發郵件:x.wang@whu.edu.cn

代碼片段和文件信息
package?Chat;
import?java.awt.BorderLayout;
import?java.awt.Dimension;
import?java.awt.Toolkit;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.PrintWriter;
import?java.net.Socket;
import?java.net.UnknownHostException;
import?javax.swing.JButton;
import?javax.swing.Jframe;
import?javax.swing.JPanel;
import?javax.swing.JScrollPane;
import?javax.swing.JSplitPane;
import?javax.swing.JTextArea;
public?class?Chatframe{
private?PrintWriter?pw;
private?Jframe?frame;
private?JPanel?pane_buttom;
private?JSplitPane?pane_center;
//顯示內容的文本框,輸入內容的文本框發送內容按鈕
private?JScrollPane?pane_showWindow;
private?JScrollPane?pane_inputWindow;
private?JTextArea?area_showWindow;
private?JTextArea?area_inputWindow;
private?JButton?btn_send;
private?Dimension?dimension;//用于設置area_showWindow可拖拉的大小
//初始化
public?Chatframe()?{
frame?=?new?Jframe();
pane_buttom?=?new?JPanel();
pane_showWindow?=?new?JScrollPane();
pane_inputWindow?=?new?JScrollPane();
area_showWindow?=?new?JTextArea();
area_inputWindow?=?new?JTextArea();
pane_center?=?new?JSplitPane(JSplitPane.VERTICAL_SPLIT?false?pane_showWindow?pane_inputWindow);
btn_send?=?new?JButton(“發送“);
dimension?=?new?Dimension(50?300);
}
//調用方法顯示窗口
public?void?showframe(){
initframe();
initChatTextArea();
initButton();
btn_send();
socket();
}
//主窗體
public?void?initframe(){
frame.settitle(“客戶端“);
int?width?=?(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int?height?=?(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setBounds(width?/?2?height?/?2?400?450);
frame.setVisible(true);
}
//內容顯示文本框和輸入內容文本框
private?void?initChatTextArea(){
//取得視圖焦點
pane_showWindow.getViewport().add(area_showWindow);
pane_inputWindow.getViewport().add(area_inputWindow);
//將顯示文本域設置為不可編輯
area_showWindow.setEditable(false);
//設置顯示文本域可拖拉的大小?
pane_showWindow.setMinimumSize(dimension);
frame.add(pane_center?BorderLayout.CENTER);
}
//發送文件,發送內容按鈕
public?void?initButton(){
pane_buttom.add(btn_send);
frame.add(pane_buttom?BorderLayout.SOUTH);
}
private?void?btn_send(){
btn_send.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
String?info?=?area_inputWindow.getText();
area_showWindow.append(“客戶端:?“+info+“\r\n“);
pw.println(info);
area_inputWindow.setText(““);
}
});
}
private?void?socket(){
try?{
@SuppressWarnings(“resource“)
Socket?s?=?new?Socket(“localhost“9988);
InputStreamReader?isr=new?InputStreamReader(s.getInputStream());
BufferedReader?br=new?BufferedReader(isr);
pw=new?PrintWriter(s.getOutputStream()true);
while(true){
//不停地讀取從服務器端發來的信息
String?info=br.readLine();
area_showWindow.
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-02-22?19:33??計算機網絡\
?????文件?????????301??2016-01-09?20:31??計算機網絡\.classpath
?????文件?????????391??2016-01-09?20:31??計算機網絡\.project
?????目錄???????????0??2016-02-22?19:33??計算機網絡\.settings\
?????文件?????????598??2016-01-09?20:31??計算機網絡\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2016-02-22?19:33??計算機網絡\bin\
?????目錄???????????0??2016-02-22?19:33??計算機網絡\bin\Chat\
?????文件????????1226??2016-01-09?20:31??計算機網絡\bin\Chat\Chatfr
?????文件????????4297??2016-01-09?20:31??計算機網絡\bin\Chat\Chatfr
?????文件????????1273??2016-01-09?20:31??計算機網絡\bin\Chat\Chatfr
?????文件????????4318??2016-01-09?20:31??計算機網絡\bin\Chat\Chatfr
?????目錄???????????0??2016-02-22?19:33??計算機網絡\bin\Client\
?????文件????????4963??2016-01-09?20:31??計算機網絡\bin\Client\ChangePassword.class
?????文件????????1501??2016-01-09?20:31??計算機網絡\bin\Client\Client.class
?????文件????????4559??2016-01-09?20:31??計算機網絡\bin\Client\Log.class
?????文件????????2184??2016-01-09?20:31??計算機網絡\bin\Client\MainLog.class
?????文件????????4558??2016-01-09?20:31??計算機網絡\bin\Client\Register.class
?????目錄???????????0??2016-02-22?19:33??計算機網絡\bin\FileTransfer\
?????文件????????2728??2016-01-09?20:31??計算機網絡\bin\FileTransfer\Download.class
?????文件????????3270??2016-01-09?20:31??計算機網絡\bin\FileTransfer\DownLoadListener$1.class
?????文件????????1210??2016-01-09?20:31??計算機網絡\bin\FileTransfer\DownLoadListener.class
?????文件????????1937??2016-01-09?20:31??計算機網絡\bin\FileTransfer\FileTransfer.class
?????文件????????1914??2016-01-09?20:31??計算機網絡\bin\FileTransfer\Upload.class
?????文件????????2958??2016-01-09?20:31??計算機網絡\bin\FileTransfer\UploadListener$1.class
?????文件????????1172??2016-01-09?20:31??計算機網絡\bin\FileTransfer\UploadListener.class
?????目錄???????????0??2016-02-22?19:33??計算機網絡\bin\Server\
?????文件????????7852??2016-01-09?20:31??計算機網絡\bin\Server\Server.class
?????目錄???????????0??2016-02-22?19:33??計算機網絡\src\
?????目錄???????????0??2016-02-22?19:33??計算機網絡\src\Chat\
?????文件????????3403??2016-01-09?20:31??計算機網絡\src\Chat\Chatfr
?????文件????????3469??2016-01-09?20:31??計算機網絡\src\Chat\Chatfr
............此處省略15個文件信息
- 上一篇:二維碼生成jar包地址
- 下一篇:咖啡機代碼原裝的
評論
共有 條評論