資源簡(jiǎn)介
使用GUI和TCP網(wǎng)絡(luò)編程,實(shí)現(xiàn)圖形界面化客戶(hù)端給服務(wù)器發(fā)送消息,并附帶登陸注冊(cè)功能

代碼片段和文件信息
package?org.westos.Client;
import?java.awt.*;
import?java.awt.event.*;
import?java.io.DataOutputStream;
import?java.io.IOException;
import?java.io.OutputStream;
import?java.net.Socket;
public?class?Client{
????/**
?????*?客戶(hù)端
?????*/
????Socket?socket=null;
????public?Client()?{
????????/**
?????????*?一、創(chuàng)建窗體
?????????*/
????????//1.創(chuàng)建窗體
????????frame?frame?=?new?frame(“客戶(hù)端“);
????????//2.設(shè)置窗體布局
????????frame.setLayout(null);
????????frame.setBounds(200?200?500?400);
????????frame.setLocation(700300);
????????frame.setResizable(false);
????????//3.創(chuàng)建文本域、設(shè)置文本域背景色、設(shè)置文本域字體顏色
????????TextArea?textArea?=?new?TextArea();
????????textArea.setBounds(20?30??470?280);
????????textArea.setEditable(false);
????????textArea.setBackground(Color.DARK_GRAY);
????????textArea.setForeground(Color.white);
????????//4.創(chuàng)建文本框,用來(lái)輸入
????????TextField?textField?=?new?TextField();
????????textField.setBounds(20?320?470?30);
????????//4.創(chuàng)建發(fā)送、退出按鈕
????????Button?button?=?new?Button(“SEND“);
????????button.setBackground(Color.gray);
????????button.setBounds(340?360?50?30);
????????Button?button1?=?new?Button(“EXIT“);
????????button1.setBackground(Color.gray);
????????button1.setBounds(440?360?50?30);
????????//6.將文本域、文本框和按鈕加入到窗體里
????????frame.add(textArea);
????????frame.add(textField);
????????frame.add(button);
????????frame.add(button1);
????????//7.顯示窗體
????????frame.setVisible(true);
????????//8.給窗體添加關(guān)閉事件
????????frame.addWindowListener(new?WindowAdapter()?{
????????????@Override
????????????public?void?windowClosing(WindowEvent?e)?{
????????????????System.exit(0);
????????????}
????????});
????????//9.給退出按鈕添加事件
????????button1.addActionListener(new?ActionListener()?{
????????????@Override
????????????public?void?actionPerformed(ActionEvent?e)?{
????????????????System.exit(0);
????????????}
????????});
????????/**
?????????*?二、發(fā)送數(shù)據(jù)并顯示在文本域中
?????????*/
????????//1.與服務(wù)端建立連接,設(shè)置服務(wù)端ip和端口
????????try?{
????????????socket?=?new?Socket(“127.0.0.1“9999);
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????????//2.在文本域textAree中提示一句話
????????textArea.append(“請(qǐng)輸入你要發(fā)送的數(shù)據(jù):\r\n“);
????????//3.給發(fā)送按鈕添加事件,每點(diǎn)擊一次button按鈕(SEND)觸發(fā)一次
????????button.addActionListener(new?ActionListener()?{
????????????@Override
????????????public?void?actionPerformed(ActionEvent?e)?{
????????????????//3.1獲取文本狂textField中的數(shù)據(jù)
????????????????String?text?=?textField.getText();
????????????????//3.2將文本框textField中輸入的內(nèi)容,添加到文本域textAree中
????????????????textArea.append(text+“\r\n“);
????????????????//3.3再將文本框置空
????????????????textField.setText(null);
????????????????//3.4獲取通道中的流
????????????????OutputStream?outputStream?=?null;
????????????????try?{
????????????????????outputStream?=?socket.getOutputStream();
????????????????}?catch?(IOException?e1)?{
????????????????????e1.printStackTrace();
????????????????}
????????????????//3.5包裝流
????????????????DataOutputStream
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\.idea\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\.idea\artifacts\
?????文件?????????312??2019-05-29?13:13??聊天室小項(xiàng)目\.idea\artifacts\Client_jar.xm
?????文件?????????312??2019-05-29?13:13??聊天室小項(xiàng)目\.idea\artifacts\Server_jar.xm
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\.idea\dictionaries\
?????文件??????????87??2019-05-29?19:16??聊天室小項(xiàng)目\.idea\dictionaries\.xm
?????文件?????????258??2019-05-28?20:21??聊天室小項(xiàng)目\.idea\encodings.xm
?????文件?????????278??2019-05-28?19:58??聊天室小項(xiàng)目\.idea\misc.xm
?????文件?????????575??2019-05-30?13:20??聊天室小項(xiàng)目\.idea\modules.xm
?????文件???????18305??2019-05-30?16:14??聊天室小項(xiàng)目\.idea\workspace.xm
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\artifacts\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\artifacts\Client_jar\
?????文件???????13026??2019-05-29?21:14??聊天室小項(xiàng)目\out\artifacts\Client_jar\客戶(hù)端Client.jar
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\artifacts\Server_jar\
?????文件????????3221??2019-05-29?21:14??聊天室小項(xiàng)目\out\artifacts\Server_jar\服務(wù)端Server.jar
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\production\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\me
?????文件??????????65??2019-05-29?21:13??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\me
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\
?????目錄???????????0??2019-05-31?14:57??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\
?????文件?????????693??2019-05-29?19:47??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\Client$1.class
?????文件?????????720??2019-05-29?19:47??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\Client$2.class
?????文件????????1861??2019-05-29?19:47??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\Client$3.class
?????文件????????2251??2019-05-29?19:47??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\Client.class
?????文件?????????687??2019-05-30?16:06??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\Login$1.class
?????文件????????2655??2019-05-30?16:06??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\Login$2.class
?????文件?????????711??2019-05-30?16:06??聊天室小項(xiàng)目\out\production\客戶(hù)端Client\org\westos\Client\Login$3.class
............此處省略36個(gè)文件信息
評(píng)論
共有 條評(píng)論