要求:编写带图形界面的聊天程序,实现让客户可以持续地发送消息给服务器,服务器也可以即时看到客户端发送的消息,并回消息给客户端。 程序运行界面如下:
老师写的:
1、客户端
package Demo02;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
//客户端程序
public class MyClient extends JFrame implements ActionListener {
JButton jb;
JTextArea jta;
JTextField jtf;
JPanel jp;
Socket socket;
DataOutputStream out;
DataInputStream in;
public MyClient() {
jta = new JTextArea();
jtf = new JTextField(15);
jb = new JButton("发送");
jp = new JPanel();
jp.add(jtf);
jp.add(jb);
JScrollPane jsp = new JScrollPane(jta);
this.add(jsp, BorderLayout.CENTER);
this.add(jp, BorderLayout.SOUTH);
jb.addActionListener(this);
jtf.addActionListener(this);
// 展现
this.setTitle("客户端");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
// 建立连接
socket = new Socket("127.0.0.1", 9000);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
while (true) {
// 接受服务器发送数据
String str = in.readUTF();
// 显示在多行文本框
jta.append("服务器说:" + str + "\r\n");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new MyClient();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb || e.getSource() == jtf) {
// 向服务器发送数据
try {
out.writeUTF(jtf.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
// 显示在多行文本框
jta.append("客户端说:" + jtf.getText() + "\r\n");
// 清空单行文本框
jtf.setText("");
}
}
}
2、服务器
package Demo02;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class MyServer extends JFrame implements ActionListener {
JTextArea jta;
JTextField jtf;
JButton jb;
JPanel jp;
ServerSocket ss;
Socket socket;
DataOutputStream out;
DataInputStream in;
public MyServer() {
jta = new JTextArea();
jtf = new JTextField(15);
jb = new JButton("发送");
jp = new JPanel();
jp.add(jtf);
jp.add(jb);
JScrollPane jsp = new JScrollPane(jta);
this.add(jsp, BorderLayout.CENTER);
this.add(jp, BorderLayout.SOUTH);
jb.addActionListener(this);
jtf.addActionListener(this);
// 展现
this.setTitle("服务器");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// 开放端口
try {
ss = new ServerSocket(9000);
System.out.println("等待连接");
socket = ss.accept();
System.out.println("已连接");
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
while (true) {
// 接受客户端发送数据
String str = in.readUTF();
// 显示多行文本框中
jta.append("客户端说:" + str + "\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new MyServer();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb || e.getSource() == jtf) {
// 向客户端发送数据
try {
out.writeUTF(jtf.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
// 向多行文本框输入历史聊天记录信息
jta.append("服务器说:" + jtf.getText() + "\r\n");
// 清空单行文本框内容
jtf.setText("");
}
}
}
我写的:
1、客户端
package Demo01;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Clinet implements ActionListener {
JFrame jf;
JScrollPane jp;
JTextArea ta;
JPanel panel;
JTextField text;
JButton but;
String s;
BufferedReader br;
PrintStream ps;
Clinet() throws IOException {
// 实例化
jf = new JFrame("客户端");
jf.setLayout(new BorderLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
ta = new JTextArea();
jp = new JScrollPane(ta);
text = new JTextField(15);
but = new JButton("发送");
// 窗体加入组件
jf.add(jp, BorderLayout.CENTER);
panel.add(text);
panel.add(but);
jf.add(panel, BorderLayout.PAGE_END);
jf.setBounds(0, 0, 400, 200);
jf.setVisible(true);
// 事件处理
but.addActionListener(this);
text.addActionListener(this);
Socket socket = new Socket("127.0.0.1", 8888);
InputStream is = socket.getInputStream();
OutputStream io = socket.getOutputStream();
br = new BufferedReader(new InputStreamReader(is));
ps = new PrintStream(io);
while (true) {
String line = br.readLine();
ta.append("服务器说:" + line + "\n");
// System.out.println("服务器说:" + line);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but || e.getSource() == text) {
s = text.getText();
ps.println(s);
ta.append("客户端说:" + s + "\n");
// System.out.println("客户端说:" + s);
text.setText("");
}
}
public static void main(String[] args) throws IOException {
new Clinet();
}
}
2、服务器
package Demo01;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Server implements ActionListener {
JFrame jf;
JScrollPane jp;
JTextArea ta;
JPanel panel;
JTextField text;
JButton but;
String s;
BufferedReader br;
PrintStream ps;
Server() throws IOException {
// 实例化
jf = new JFrame("服务器");
jf.setLayout(new BorderLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
ta = new JTextArea();
jp = new JScrollPane(ta);
text = new JTextField(15);
but = new JButton("发送");
// 窗体加入组件
jf.add(jp, BorderLayout.CENTER);
panel.add(text);
panel.add(but);
jf.add(panel, BorderLayout.PAGE_END);
jf.setBounds(0, 0, 400, 200);
jf.setVisible(true);
// 事件处理
but.addActionListener(this);
text.addActionListener(this);
ServerSocket server = new ServerSocket(8888);
Socket socket = server.accept();
InputStream is = socket.getInputStream();
OutputStream io = socket.getOutputStream();
br = new BufferedReader(new InputStreamReader(is));
ps = new PrintStream(io);
while (true) {
String line = br.readLine();
ta.append("客户端说:" + line + "\n");
// System.out.println("客户端说:" + line);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but || e.getSource() == text) {
s = text.getText();
ps.println(s);
ta.append("服务器说:" + s + "\n");
// System.out.println("服务器说:" + s);
text.setText("");
}
}
public static void main(String[] args) throws IOException {
new Server();
}
}
成果展示