Java网络编程—实现多人聊天

网络编程—实现多人聊天

ChatWithSocket1


JFrame

  • mainPanel
  • bPanel

mainPanel

  • namelabel(JLabel)
  • nametext (JTextField)
  • passwordlabel (JLabel)
  • passwordtext (JPasswordField)
        JPanel mainPanel = new JPanel();
        Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); //borderfactory是一个产生创建边框的的工厂类
        mainPanel.setBorder(BorderFactory.createTitledBorder(border, "输入登录信息", TitledBorder.CENTER, TitledBorder.TOP)); //标题登录信息,位于中间,上方
        this.add(mainPanel, BorderLayout.CENTER);   //将mainPanel 添加到中间
        mainPanel.setLayout(null);  //自定义布局
        JLabel namelabel = new JLabel("请输入名称");
        namelabel.setBounds(30, 30, 80, 22);
        mainPanel.add(namelabel);

        nametext = new JTextField();    //名称输入域
        nametext.setBounds(115, 30, 165, 22);
        mainPanel.add(nametext);

        JLabel passwordlabel = new JLabel("请输入密码");
        passwordlabel.setBounds(30, 60, 80, 22);
        mainPanel.add(passwordlabel);
        passwordtext = new JPasswordField();    //密码输入域
        passwordtext.setBounds(115, 60, 165, 22);
        mainPanel.add(passwordtext);

bPanel

  • reset (JButton)
  • submit(JButton)
        JPanel bPanel = new JPanel();
        bPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); //流动布局的右对齐
        this.add(bPanel, BorderLayout.SOUTH);
        JButton reset = new JButton("重置");
        bPanel.add(reset);
        JButton submit = new JButton("提交");
        bPanel.add(submit);

具体布局说明:

  • Icon(图标) -> JLabel(Icon) -> JFrame (JLabel,BorderLayout.NORTH)
  • border(边框)->mainPanel.setBorder ->JFrame(mainPanel,BorderLayout.CENTER)
  • bPanel (下面板) ->bPanel(FlowLayout.RIGHT) ->JFrame(bPanel,BorderLayout.SOUTH)
    Java网络编程—实现多人聊天_第1张图片

ChatWithSocket2

JFrame

  • JSplitPane
  • JPanel

bpanel

  • namelabel (JLabel)
  • closeButton (JButton)
  • sendButton (JButton)
        JPanel bPanel = new JPanel();
        bPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.add(bPanel, BorderLayout.SOUTH);
        JLabel namelabel = new JLabel("昵称: " + this.name + " ");
        bPanel.add(namelabel);
        JButton closeButton = new JButton("关闭");
        bPanel.add(closeButton);
        JButton sendButton = new JButton("发送");
        bPanel.add(sendButton);

splitPane

  • logPanel (JScrollPane) contentArea为JTextArea类型
  • sendPanel (JScrollPane) sendArea为JTextArea类型
        contentArea = new JTextArea();
        contentArea.setLineWrap(true);  //允许换行.控制每行显示长度最大不超过界面长度,只允许竖直滚动
        JScrollPane logPanel = new JScrollPane(contentArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        sendArea = new JTextArea();
        sendArea.setLineWrap(true);
        JScrollPane sendPanel = new JScrollPane(sendArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        //分隔面板
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, logPanel, sendPanel);  //可以垂直调整..
        splitPane.setDividerLocation(250);
        this.add(splitPane, BorderLayout.CENTER);

程序运行结果如下图显示…
Java网络编程—实现多人聊天_第2张图片


最终运行结果如下图所示:

Java网络编程—实现多人聊天_第3张图片


代码已成功上传到Github: [https://github.com/Githubforusc2018/java_chat]

你可能感兴趣的:(个人)