java39.Socket通信------使用 Java 创建聊天客户端(未完待续。。。)

来源:互联网 发布:方知海外有孤忠 编辑:程序博客网 时间:2024/06/14 04:52

一、大体框架

1.在工程建两个包,如图

这里写图片描述
其中MainWindow.java是新建的一个JFrame(大部分代码自动生成)
注:JFrame的使用前提是eclipse安装了windowbuilder。

2.代码:

1.MyClient类:
package com.jikexueyuan.myjavachatclient.main;import java.awt.EventQueue;import com.jikexueyuan.myjavachatclient.view.MainWindow;public class MyClient {    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    MainWindow frame = new MainWindow();                    frame.setVisible(true);                    ChatManager.getChatManager().setWindow(frame);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }}
2.MainWindow类

代码:

package com.jikexueyuan.myjavachatclient.view;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.LayoutStyle.ComponentPlacement;import javax.swing.border.EmptyBorder;import com.jikexueyuan.myjavachatclient.main.ChatManager;public class MainWindow extends JFrame {    /**     *      */    private static final long serialVersionUID = 1L;    private JPanel contentPane;    JTextArea txt;    private JTextField ip;    private JButton button;    private JTextField send;    private JButton button_1;    /**     * Launch the application.     */    /**     * Create the frame.     */    public MainWindow() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 507, 288);        contentPane = new JPanel();        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));        setContentPane(contentPane);        txt = new JTextArea();        txt.setText("ready...");        ip = new JTextField();        ip.setText("127.0.0.1");        ip.setColumns(10);        button = new JButton("链接到服务器");        button.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                ChatManager.getChatManager().connect(ip.getText());            }        });        button.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {            }        });        send = new JTextField();        send.setText("你好");        send.setColumns(10);        button_1 = new JButton("发送");        button_1.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                ChatManager.getChatManager().send(send.getText());// 发送出去                appendText("我说:" + send.getText());// 要将自己发送的数据也显示在本客户端的文本框内                // 发送之后要将文本框清空                send.setText("");            }        });        GroupLayout gl_contentPane = new GroupLayout(contentPane);        gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)                .addGroup(Alignment.TRAILING,                        gl_contentPane.createSequentialGroup().addContainerGap()                                .addComponent(ip, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE).addGap(56)                                .addComponent(button).addContainerGap())                .addGroup(gl_contentPane.createSequentialGroup().addGap(11)                        .addComponent(send, GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE).addGap(61)                        .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE).addGap(5))                .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup().addGap(11)                        .addComponent(txt, GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE).addGap(11)));        gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)                .addGroup(gl_contentPane.createSequentialGroup()                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false).addComponent(button)                                .addGroup(gl_contentPane.createSequentialGroup().addGap(1)                                        .addComponent(ip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,                                                GroupLayout.PREFERRED_SIZE)                                        .addPreferredGap(ComponentPlacement.RELATED, 1, Short.MAX_VALUE)))                        .addGap(10).addComponent(txt, GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE).addGap(1)                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)                                .addGroup(gl_contentPane.createSequentialGroup().addGap(4).addComponent(send,                                        GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,                                        GroupLayout.PREFERRED_SIZE))                                .addGroup(gl_contentPane.createSequentialGroup().addGap(3).addComponent(button_1)))                        .addGap(2)));        contentPane.setLayout(gl_contentPane);    }    public void appendText(String in) {        txt.append("\n" + in);    }}

MainWindow设计视图:
这里写图片描述

3.ChatManager类
package com.jikexueyuan.myjavachatclient.main;import com.jikexueyuan.myjavachatclient.view.MainWindow;public class ChatManager {    private ChatManager() {    }    private static final ChatManager cm = new ChatManager();    public static ChatManager getChatManager() {        return cm;    }    MainWindow window;    public void setWindow(MainWindow window) {        this.window = window;        window.appendText("文本框已经和ChatManager绑定了。");    }    public void connect(String ip) {    }    public void send(String out) {    }}

3.运行结果

这里写图片描述

二、客户端核心内容

未完待续。。。(代码出了问题,后期补上)

0 0
原创粉丝点击