Java聊天室 IO流 Socket流 GUI界面 服务端主界面代码及功能详解

Java聊天室 IO流 Socket流 GUI界面 服务端主界面代码及功能详解

主页面界面图




主界面详解

package SanWa.UI;

import SanWa.Util.PathUtils;
import SanWa.Util.ScreenUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

public class Server {
    public static JTextArea infortextArea1;
    public static Socket sk;
    public static ArrayList<String> info = new ArrayList<>();
    final int WIDTH = 600;
    final int HEIGHT = 600;
    JFrame jf = new JFrame("弎屲服务端");
    JTextField d2Field;

    //程序入口
    public static void main(String[] args) {

        new Server().init();


    }

    //组装视图
    public void init() {
        //设置窗口的相关属性  进行页面的绘制
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        //获取电脑屏幕的大小
        jf.setVisible(false);
        try {
            jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("04.JPG"))));
            //设置背景
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置窗口内容
        BackGroundPanel bgPanel = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //组装登录相关的元素
        Box vBox = Box.createVerticalBox();
        Box dkBox = Box.createHorizontalBox();
        JLabel d1Label = new JLabel("请 输 入 您 的 端 口 号:");
        //d2Field = new JTextField("请输入您的端口号!", 23);
        d2Field = new JTextField("8888", 23);
        d2Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
/*
鼠标点击与鼠标离开
输入框中显示的信息
*/
                if (d2Field.getText().equals("请输入您的端口号!")) {
                    d2Field.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (d2Field.getText().equals("")) {
                    d2Field.setText("请输入您的端口号!");
                }
            }
        });
        dkBox.add(d1Label);
        dkBox.add(Box.createHorizontalStrut(25));
        dkBox.add(d2Field);
        Box u1Box = Box.createHorizontalBox();
        u1Box.setPreferredSize(new Dimension(250, 350));
        infortextArea1 = new JTextArea();
        infortextArea1.setBackground(new Color(217, 217, 217));
        JScrollPane scrollPane21 = new JScrollPane();
        scrollPane21.setViewportView(infortextArea1);
        u1Box.add(scrollPane21);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("启动服务器");
        //服务器启动按钮的响应与交互 进行流的创建与服务器的开启
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                HashMap<String, Socket> hm = new HashMap<>();
                int dk = Integer.parseInt(d2Field.getText().trim());

                ServerSocket ss = null;
                try {
                    ss = new ServerSocket(dk);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                infortextArea1.append("服务器已经开启,等待连接。。。"+"\n");
                JOptionPane.showMessageDialog(null, "服务器已开启!");

                //jf.dispose();
                int i = 1;
                while (true) {
                    try {
                        sk = ss.accept();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
               infortextArea1.append((i++) + " 个客户端已经连接"+"\n");
                    new RegisterUserThread(sk, hm).start();
                }
            }
        });
        JButton clcBtn = new JButton("关闭服务器");
        clcBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });


        //退出按钮
        Box closeBox = Box.createHorizontalBox();
        JButton coBtn = new JButton("退出");
        coBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        closeBox.add(coBtn);


        JButton coBtn1 = new JButton("重置");
        coBtn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d2Field.setText("请输入您的端口号!");
            }
        });

        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(coBtn1);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(clcBtn);
        vBox.add(Box.createVerticalStrut(35));
        vBox.add(dkBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(u1Box);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(closeBox);

        bgPanel.add(vBox);
        jf.add(bgPanel);
        jf.setVisible(true);
    }
}

  • new Server().init();开启服务端界面
 jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setVisible(false);
        try {
            jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("04.JPG"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置窗口内容
        BackGroundPanel bgPanel = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
  • 主页面的搭建
  JLabel d1Label = new JLabel("请 输 入 您 的 端 口 号:");
        //d2Field = new JTextField("请输入您的端口号!", 23);
        d2Field = new JTextField("8888", 23);
        d2Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

                if (d2Field.getText().equals("请输入您的端口号!")) {
                    d2Field.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (d2Field.getText().equals("")) {
                    d2Field.setText("请输入您的端口号!");
                }
            }
        });
        dkBox.add(d1Label);
        dkBox.add(Box.createHorizontalStrut(25));
        dkBox.add(d2Field);
        Box u1Box = Box.createHorizontalBox();
        u1Box.setPreferredSize(new Dimension(250, 350));
        infortextArea1 = new JTextArea();
        infortextArea1.setBackground(new Color(217, 217, 217));
        JScrollPane scrollPane21 = new JScrollPane();
        scrollPane21.setViewportView(infortextArea1);
        u1Box.add(scrollPane21);

  • 端口号的输入框的提示
   Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("启动服务器");
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                HashMap<String, Socket> hm = new HashMap<>();
                int dk = Integer.parseInt(d2Field.getText().trim());

                ServerSocket ss = null;
                try {
                    ss = new ServerSocket(dk);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                infortextArea1.append("服务器已经开启,等待连接。。。"+"\n");
                JOptionPane.showMessageDialog(null, "服务器已开启!");

                //jf.dispose();
                int i = 1;
                while (true) {
                    try {
                        sk = ss.accept();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
               infortextArea1.append((i++) + " 个客户端已经连接"+"\n");
                    new RegisterUserThread(sk, hm).start();
                }
            }
        });
       
  • 服务器的启动按钮
  • 开启输入框中的端口号的流 并循环开始监听
  • 显示服务器已连接 并开启RegisterUserThread线程 进行客户端的登录验证

黑窗口版本

/*
package SanWa.UI;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

public class Server {
    public static HashMap<String, Socket> hm=new HashMap<>();
    public static Socket sk;
    public static ArrayList<String> info=new ArrayList<>();

    public static void main(String[] args) throws IOException {
        HashMap<String, Socket> hm = new HashMap<>();
        ServerSocket ss = new ServerSocket(8888);
        System.out.println("服务器已经开启,等待连接。。。");
        int i = 1;
        while (true) {
            sk = ss.accept();
            System.out.println((i++) + " 个客户端已经连接");
            new RegisterUserThread(sk,hm).start();
        }
    }
}
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值