您的位置:首页 > 编程语言 > Java开发

Java 入门 之 聊天室 客户端源码

2016-02-26 20:38 423 查看
package View;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import ChatManager.ChatManager;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainWIndow extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextArea txt;
private JTextField ip;
private JTextField sent;
private JButton button_1;

public MainWIndow() {
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 317);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

txt = new JTextArea();
txt.setText("Ready Go !!! ");

ip = new JTextField();
ip.setText("127.0.0.1");
ip.setColumns(10);

JButton button = new JButton("\u8FDE\u63A5\u5230\u670D\u52A1\u5668");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
// 获取服务器的 ip 地址,从而通过 ip 建立服务器链接
//				if (!ip.getText().isEmpty()) {
System.out.println(ip.getText());
System.out.println(3);
System.out.println(2);
System.out.println(1);
ChatManager.GetCM().connect(ip.getText()); // 管理员 读取 ip 输入框中的字符串
//				}
}
});

sent = new JTextField();
sent.setText("\u4F60\u597D \uFF01");
sent.setColumns(10);

button_1 = new JButton("\u53D1\u9001");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
//				if (!sent.getText().isEmpty()) {
System.out.println(sent.getText());
ChatManager.GetCM().send(sent.getText()); // 管理员 得到 sent
// 输入框之中的字符串
AppendText("我 :" + sent.getText()); // 显示框 得到 sent 之中的字符串
sent.setText(""); // 完成输入之后 置空
//				}
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane
.setHorizontalGroup(gl_contentPane
.createParallelGroup(Alignment.TRAILING)
.addGroup(
Alignment.LEADING,
gl_contentPane
.createSequentialGroup()
.addContainerGap()
.addGroup(
gl_contentPane
.createParallelGroup(
Alignment.LEADING)
.addComponent(
txt,
Alignment.TRAILING,
GroupLayout.DEFAULT_SIZE,
404,
Short.MAX_VALUE)
.addGroup(
gl_contentPane
.createSequentialGroup()
.addComponent(
ip,
GroupLayout.PREFERRED_SIZE,
266,
GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(
button,
GroupLayout.DEFAULT_SIZE,
120,
Short.MAX_VALUE))
.addGroup(
Alignment.TRAILING,
gl_contentPane
.createSequentialGroup()
.addComponent(
sent,
GroupLayout.DEFAULT_SIZE,
329,
Short.MAX_VALUE)
.addGap(18)
.addComponent(
button_1)))
.addContainerGap()));
gl_contentPane
.setVerticalGroup(gl_contentPane
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_contentPane
.createSequentialGroup()
.addGroup(
gl_contentPane
.createParallelGroup(
Alignment.BASELINE)
.addComponent(
ip,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(button))
.addPreferredGap(
ComponentPlacement.UNRELATED)
.addComponent(txt,
GroupLayout.DEFAULT_SIZE, 185,
Short.MAX_VALUE)
.addPreferredGap(
ComponentPlacement.UNRELATED)
.addGroup(
gl_contentPane
.createParallelGroup(
Alignment.TRAILING)
.addComponent(button_1)
.addComponent(
sent,
GroupLayout.PREFERRED_SIZE,
39,
GroupLayout.PREFERRED_SIZE))
.addContainerGap()));
contentPane.setLayout(gl_contentPane);
}

public void AppendText(String str) {
txt.append("\n" + str);
}
}


package ChatManager;

import java.awt.EventQueue;

import View.MainWIndow;

public class StartClient {

/**
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWIndow frame = new MainWIndow();
frame.setVisible(true);
ChatManager.GetCM().setMainWindow(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

package ChatManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import View.MainWIndow;

public class ChatManager {

private ChatManager() {
}

private static final ChatManager cm = new ChatManager();

public static ChatManager GetCM() {
return cm;
}

Socket socket;
String IP;
MainWIndow mw;
BufferedReader br;
PrintWriter pw;

public void setMainWindow(MainWIndow mw) {
this.mw = mw;
mw.AppendText("启禀皇上,您的窗体已绑定成功 !");
}

public void connect(String ip) {
this.IP = ip;
new Thread() {
public void run() {
try {
socket = new Socket(IP, 12345);
pw = new PrintWriter(new OutputStreamWriter(
socket.getOutputStream()));

br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
mw.AppendText("收到  :" + line);
}
pw.close();
br.close();

pw = null;
br = null;
}catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}

public void send(String str) {
if (pw != null) {
pw.write(str + "\n");// 有 \n 才能有 输出的刷新
pw.flush();
} else {
mw.AppendText("send 连接中断!");
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub

}

}


实现步骤

1. 开启服务器端程序;

2. 开启客户端程序;

3. 连接网络地址和网络端口;

4. 输入聊天内容,实现聊天。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息