• 大小: 51KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: 其他
  • 标签: JAVA  GUI  TCP  Socket  

资源简介

使用GUI和TCP网络编程,实现图形界面化客户端给服务器发送消息,并附带登陆注册功能

资源截图

代码片段和文件信息

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{

    /**
     * 客户端
     */
    Socket socket=null;

    public Client() {
        /**
         * 一、创建窗体
         */
        //1.创建窗体
        frame frame = new frame(“客户端“);
        //2.设置窗体布局
        frame.setLayout(null);
        frame.setBounds(200 200 500 400);
        frame.setLocation(700300);
        frame.setResizable(false);

        //3.创建文本域、设置文本域背景色、设置文本域字体颜色
        TextArea textArea = new TextArea();
        textArea.setBounds(20 30  470 280);
        textArea.setEditable(false);
        textArea.setBackground(Color.DARK_GRAY);
        textArea.setForeground(Color.white);
        //4.创建文本框,用来输入
        TextField textField = new TextField();
        textField.setBounds(20 320 470 30);
        //4.创建发送、退出按钮
        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.给窗体添加关闭事件
        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);
            }
        });

        /**
         * 二、发送数据并显示在文本域中
         */
        //1.与服务端建立连接,设置服务端ip和端口
        try {
            socket = new Socket(“127.0.0.1“9999);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //2.在文本域textAree中提示一句话
        textArea.append(“请输入你要发送的数据:\r\n“);
        //3.给发送按钮添加事件,每点击一次button按钮(SEND)触发一次
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //3.1获取文本狂textField中的数据
                String text = textField.getText();
                //3.2将文本框textField中输入的内容,添加到文本域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

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-05-31 14:57  聊天室小项目\
     目录           0  2019-05-31 14:57  聊天室小项目\.idea\
     目录           0  2019-05-31 14:57  聊天室小项目\.idea\artifacts\
     文件         312  2019-05-29 13:13  聊天室小项目\.idea\artifacts\Client_jar.xml
     文件         312  2019-05-29 13:13  聊天室小项目\.idea\artifacts\Server_jar.xml
     目录           0  2019-05-31 14:57  聊天室小项目\.idea\dictionaries\
     文件          87  2019-05-29 19:16  聊天室小项目\.idea\dictionaries\.xml
     文件         258  2019-05-28 20:21  聊天室小项目\.idea\encodings.xml
     文件         278  2019-05-28 19:58  聊天室小项目\.idea\misc.xml
     文件         575  2019-05-30 13:20  聊天室小项目\.idea\modules.xml
     文件       18305  2019-05-30 16:14  聊天室小项目\.idea\workspace.xml
     目录           0  2019-05-31 14:57  聊天室小项目\out\
     目录           0  2019-05-31 14:57  聊天室小项目\out\artifacts\
     目录           0  2019-05-31 14:57  聊天室小项目\out\artifacts\Client_jar\
     文件       13026  2019-05-29 21:14  聊天室小项目\out\artifacts\Client_jar\客户端Client.jar
     目录           0  2019-05-31 14:57  聊天室小项目\out\artifacts\Server_jar\
     文件        3221  2019-05-29 21:14  聊天室小项目\out\artifacts\Server_jar\服务端Server.jar
     目录           0  2019-05-31 14:57  聊天室小项目\out\production\
     目录           0  2019-05-31 14:57  聊天室小项目\out\production\客户端Client\
     目录           0  2019-05-31 14:57  聊天室小项目\out\production\客户端Client\meta-INF\
     文件          65  2019-05-29 21:13  聊天室小项目\out\production\客户端Client\meta-INF\MANIFEST.MF
     目录           0  2019-05-31 14:57  聊天室小项目\out\production\客户端Client\org\
     目录           0  2019-05-31 14:57  聊天室小项目\out\production\客户端Client\org\westos\
     目录           0  2019-05-31 14:57  聊天室小项目\out\production\客户端Client\org\westos\Client\
     文件         693  2019-05-29 19:47  聊天室小项目\out\production\客户端Client\org\westos\Client\Client$1.class
     文件         720  2019-05-29 19:47  聊天室小项目\out\production\客户端Client\org\westos\Client\Client$2.class
     文件        1861  2019-05-29 19:47  聊天室小项目\out\production\客户端Client\org\westos\Client\Client$3.class
     文件        2251  2019-05-29 19:47  聊天室小项目\out\production\客户端Client\org\westos\Client\Client.class
     文件         687  2019-05-30 16:06  聊天室小项目\out\production\客户端Client\org\westos\Client\Login$1.class
     文件        2655  2019-05-30 16:06  聊天室小项目\out\production\客户端Client\org\westos\Client\Login$2.class
     文件         711  2019-05-30 16:06  聊天室小项目\out\production\客户端Client\org\westos\Client\Login$3.class
............此处省略36个文件信息

评论

共有 条评论