移动开发干货店

Java网络编程—多人聊天室

2018-11-30  本文已影响53人  Githubforusc

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

网络编程—实现多人聊天

ChatWithSocket1


JFrame


mainPanel

        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

        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);

具体布局说明:

实现效果如图所示

ChatWithSocket2

JFrame


bpanel

        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

        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);
实现效果如图所示
上一篇下一篇

猜你喜欢

热点阅读