Java聊天室 IO流 Socket流 GUI界面 客户端FunctionView界面详解

Java聊天室 IO流 Socket流 GUI界面 客户端FunctionView界面详解

效果图

欢迎界面

功能界面

头像下拉列表

上下线提醒

获取在线列表

私人聊天频道

公共聊天频道

用户文件发送

切换在线隐身

查询聊天记录

删除聊天记录

下线

代码

  • 定义页面

  • 通过menu设置下拉功能

    JMenuItem j1 = new JMenuItem("切换账号");
    JMenuItem j3 = new JMenuItem("隐身上线");
    JMenuItem j2 = new JMenuItem("退出程序");
    
  • sp = new JSplitPane();设置页面切割 分为左右页面

  • 通过DefaultMutableTreeNode设置左边的下拉选项功能

    DefaultMutableTreeNode gn = new DefaultMutableTreeNode("功能选项");
            DefaultMutableTreeNode gn1 = new DefaultMutableTreeNode("上下线提醒");
            DefaultMutableTreeNode gn2 = new DefaultMutableTreeNode("获取在线列表");
            DefaultMutableTreeNode gn3 = new DefaultMutableTreeNode("私人频道聊天");
            DefaultMutableTreeNode gn4 = new DefaultMutableTreeNode("公共频道聊天");
            DefaultMutableTreeNode gn5 = new DefaultMutableTreeNode("用户文件发送");
            DefaultMutableTreeNode gn6 = new DefaultMutableTreeNode("切换在线隐身");
            DefaultMutableTreeNode gn7 = new DefaultMutableTreeNode("查询聊天记录");
            DefaultMutableTreeNode gn8 = new DefaultMutableTreeNode("删除聊天记录");
            DefaultMutableTreeNode gn9 = new DefaultMutableTreeNode("下线");
    
  • e.getNewLeadSelectionPath().getLastPathComponent();设置选中跳转不同功能页面

  • getTreeCellRendererComponent设置选中样式 及 节点图标

package SanWa.UI;

import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import SanWa.Util.PathUtils;
import SanWa.Util.ScreenUtils;


import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.*;
import java.util.Scanner;


public class FunctionView {
    public static JSplitPane sp;
    public static PublicCompoent publicCompoent = new PublicCompoent();
    public static UserManageCompoent userManageCompoent = new UserManageCompoent();
    public static PrivateCompoent privateCompoent = new PrivateCompoent();
    public static ShangXiaCompoent shangXiaCompoent = new ShangXiaCompoent();
    public static ChaXunCompoent chaXunCompoent = new ChaXunCompoent();
    public static DeleteCompoent deleteCompoent = new DeleteCompoent();
    public static WenJianconpoent wenJianconpoent = new WenJianconpoent();
    public static YinShenCompoent yinShenCompoent = new YinShenCompoent();
    final int WIDTH = 1000;
    final int HEIGHT = 700;
    JFrame jf = new JFrame("弎屲ChatRoom");


    //程序入口
    public static void main(String[] args) {
        new FunctionView().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;
        BackGroundPanel bgPanel1 = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("01.jpg"))));
            bgPanel1 = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("07.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置窗口内容
        JMenuBar jbar = new JMenuBar();
        JMenu j = null;
        if (MainView.ss == null) {
            j = new JMenu(RegisterView.ss);
        } else {
            j = new JMenu(MainView.ss);
        }

        JMenuItem j1 = new JMenuItem("切换账号");
        JMenuItem j3 = new JMenuItem("隐身上线");
        JMenuItem j2 = new JMenuItem("退出程序");
        j1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MainView.ss == null) {
                    RegisterView.cli.stop();
                } else {
                    MainView.cli.stop();
                }

                User us = new User(MainView.ss, null, MsgTypeConfig.EXIT_MSG, System.currentTimeMillis());
                try {
                    MainView.out.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                JOptionPane.showMessageDialog(jf, "聊天室即将退出");
                try {
                    MainView.sk.close();
                    jf.dispose();
                    new MainView().init();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        j2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MainView.ss == null) {
                    RegisterView.cli.stop();
                } else {
                    MainView.cli.stop();
                }

                User us = new User(MainView.ss, null, MsgTypeConfig.EXIT_MSG, System.currentTimeMillis());
                try {
                    MainView.out.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                JOptionPane.showMessageDialog(jf, "聊天室即将退出");
                try {
                    MainView.sk.close();
                    System.exit(0);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        j3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_MSG,System.currentTimeMillis());
                try {
                    MainView.out.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

            }
        });
        j.add(j1);
        j.add(j3);
        j.add(j2);

        jbar.add(j);
        jf.setJMenuBar(jbar);

        //设置分割页面
        sp = new JSplitPane();
        //连续布局
        sp.setContinuousLayout(true);
        sp.setDividerLocation(1);
        sp.setDividerSize(10);
        DefaultMutableTreeNode gn = new DefaultMutableTreeNode("功能选项");
        DefaultMutableTreeNode gn1 = new DefaultMutableTreeNode("上下线提醒");
        DefaultMutableTreeNode gn2 = new DefaultMutableTreeNode("获取在线列表");
        DefaultMutableTreeNode gn3 = new DefaultMutableTreeNode("私人频道聊天");
        DefaultMutableTreeNode gn4 = new DefaultMutableTreeNode("公共频道聊天");
        DefaultMutableTreeNode gn5 = new DefaultMutableTreeNode("用户文件发送");
        DefaultMutableTreeNode gn6 = new DefaultMutableTreeNode("切换在线隐身");
        DefaultMutableTreeNode gn7 = new DefaultMutableTreeNode("查询聊天记录");
        DefaultMutableTreeNode gn8 = new DefaultMutableTreeNode("删除聊天记录");
        DefaultMutableTreeNode gn9 = new DefaultMutableTreeNode("下线");
        gn.add(gn1);
        gn.add(gn2);
        gn.add(gn3);
        gn.add(gn4);
        gn.add(gn5);
        gn.add(gn6);
        gn.add(gn7);
        gn.add(gn8);
        gn.add(gn9);
        Color color = new Color(208, 208, 208);
        Color color1 = new Color(151, 151, 152);
        JTree jTree = new JTree(gn);
        jTree.setFont(new java.awt.Font("楷体", 1, 15));
        jTree.setBackground(color);
        Myrend my = new Myrend();
        my.setBackgroundNonSelectionColor(color);
        my.setBackgroundSelectionColor(color1);
        jTree.setCellRenderer(my);

        //默认选中
        jTree.setSelectionRow(0);
        jTree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                Object l = e.getNewLeadSelectionPath().getLastPathComponent();
                if (gn1.equals(l)) {

                    sp.setRightComponent(shangXiaCompoent);
                    sp.setDividerLocation(200);

                }
                if (gn2.equals(l)) {
                    sp.setRightComponent(userManageCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn3.equals(l)) {
                    sp.setRightComponent(privateCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn4.equals(l)) {
                    sp.setRightComponent(publicCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn5.equals(l)) {
                    sp.setRightComponent(wenJianconpoent);
                    sp.setDividerLocation(200);
                }
                if (gn6.equals(l)) {
                    sp.setRightComponent(yinShenCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn7.equals(l)) {
                    sp.setRightComponent(chaXunCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn8.equals(l)) {
                    sp.setRightComponent(deleteCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn9.equals(l)) {
                    if (MainView.ss == null) {
                        RegisterView.cli.stop();
                    } else {
                        MainView.cli.stop();
                    }

                    User us = new User(MainView.ss, null, MsgTypeConfig.EXIT_MSG, System.currentTimeMillis());
                    try {
                        MainView.out.writeObject(us);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(jf, "聊天室即将退出");
                    try {
                        MainView.sk.close();
                        System.exit(0);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }

                }
            }
        });
        sp.setRightComponent(new TiShi());

        sp.setLeftComponent(jTree);
        jf.add(sp);

        jf.setVisible(true);
    }

    //自定义绘制节点器
    private class Myrend extends DefaultTreeCellRenderer {
        private ImageIcon r = null;
        private ImageIcon r1 = null;
        private ImageIcon r2 = null;
        private ImageIcon r3 = null;
        private ImageIcon r4 = null;
        private ImageIcon r5 = null;
        private ImageIcon r6 = null;
        private ImageIcon r7 = null;
        private ImageIcon r8 = null;
        private ImageIcon r9 = null;

        public Myrend() {
            InputStream inputStream;
            r = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r1 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r2 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r3 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r4 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r5 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r6 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r7 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r8 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r9 = new ImageIcon(PathUtils.getRealPath("11.jpg"));

        }

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            //使用默认绘制
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            ImageIcon image = null;
            switch (row) {
                case 0:
                    image = r;
                    break;
                case 1:
                    image = r1;
                    break;
                case 2:
                    image = r2;
                    break;
                case 3:
                    image = r3;
                    break;
                case 4:
                    image = r4;
                    break;
                case 5:
                    image = r5;
                    break;
                case 6:
                    image = r6;
                    break;
                case 7:
                    image = r7;
                    break;
                case 8:
                    image = r8;
                    break;
                case 9:
                    image = r9;
                    break;

            }
            this.setIcon(image);
            return this;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值