如何用java做一个聊天小程序 要求使用图形用户界面,可以实现一个聊天室中多人聊天,也可以两人私聊,
展开全部
给你一个简单的实现吧,注意一定要先运行MyServer.java
//MyCilent.java
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyClient extends JFrame implements ActionListener{
JTextField tf;
JTextArea tx;
JButton bt;
PrintWriter out;
public MyClient(){
tf=new JTextField(20);
tx=new JTextArea();
tx.setLineWrap(true);
tx.setWrapStyleWord(true);
JPanel pan=new JPanel();
JScrollPane jsp=new JScrollPane(tx);
add(jsp,"Center");
bt=new JButton("SEND");
bt.addActionListener(this);
pan.add(tf);
pan.add(bt);
add(pan,"South");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setTitle("THE CLIENT");
setSize(400,300);
setVisible(true);
try{
Socket socket=new Socket("127.0.0.1",1680);
out=new PrintWriter(socket.getOutputStream(),true);
InputStreamReader in = new InputStreamReader(socket.getInputStream());
BufferedReader sin=new BufferedReader(in);
String s;
while(true){
s=sin.readLine();
tx.append("#Server Said#: "+s+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt){
tx.append("@Client Said@: "+tf.getText()+"\n");
out.println(tf.getText());
tf.setText("");
}
}
public static void main(String[] args){
MyClient mct = new MyClient();
}
}
//MyServer.java
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyServer extends JFrame implements ActionListener{
JTextField tf;
JTextArea tx;
JButton bt;
JScrollPane jsp;
JPanel pan;
PrintWriter out;
public MyServer(){
tx=new JTextArea();
tx.setLineWrap(true);
tx.setWrapStyleWord(true);
jsp=new JScrollPane(tx);
tf=new JTextField(20);
bt=new JButton("SEND");
bt.addActionListener(this);
pan=new JPanel();
pan.add(tf);
pan.add(bt);
add(pan,"South");
add(jsp,"Center");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setTitle("THE SERVER");
setSize(400,300);
setVisible(true);
try{
ServerSocket server = new ServerSocket(1680);
Socket socket = server.accept();
InputStreamReader in = new InputStreamReader(socket.getInputStream());
BufferedReader sin=new BufferedReader(in);
out=new PrintWriter(socket.getOutputStream(),true);
while(true){
String s=sin.readLine();
tx.append("@Client Said@: "+s+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt){
String st = tf.getText();
tx.append("#Server Said#: "+st+"\n");
out.println(st);
tf.setText("");
}
}
public static void main(String[] args){
MyServer msr = new MyServer();
}
}
//MyCilent.java
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyClient extends JFrame implements ActionListener{
JTextField tf;
JTextArea tx;
JButton bt;
PrintWriter out;
public MyClient(){
tf=new JTextField(20);
tx=new JTextArea();
tx.setLineWrap(true);
tx.setWrapStyleWord(true);
JPanel pan=new JPanel();
JScrollPane jsp=new JScrollPane(tx);
add(jsp,"Center");
bt=new JButton("SEND");
bt.addActionListener(this);
pan.add(tf);
pan.add(bt);
add(pan,"South");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setTitle("THE CLIENT");
setSize(400,300);
setVisible(true);
try{
Socket socket=new Socket("127.0.0.1",1680);
out=new PrintWriter(socket.getOutputStream(),true);
InputStreamReader in = new InputStreamReader(socket.getInputStream());
BufferedReader sin=new BufferedReader(in);
String s;
while(true){
s=sin.readLine();
tx.append("#Server Said#: "+s+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt){
tx.append("@Client Said@: "+tf.getText()+"\n");
out.println(tf.getText());
tf.setText("");
}
}
public static void main(String[] args){
MyClient mct = new MyClient();
}
}
//MyServer.java
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyServer extends JFrame implements ActionListener{
JTextField tf;
JTextArea tx;
JButton bt;
JScrollPane jsp;
JPanel pan;
PrintWriter out;
public MyServer(){
tx=new JTextArea();
tx.setLineWrap(true);
tx.setWrapStyleWord(true);
jsp=new JScrollPane(tx);
tf=new JTextField(20);
bt=new JButton("SEND");
bt.addActionListener(this);
pan=new JPanel();
pan.add(tf);
pan.add(bt);
add(pan,"South");
add(jsp,"Center");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setTitle("THE SERVER");
setSize(400,300);
setVisible(true);
try{
ServerSocket server = new ServerSocket(1680);
Socket socket = server.accept();
InputStreamReader in = new InputStreamReader(socket.getInputStream());
BufferedReader sin=new BufferedReader(in);
out=new PrintWriter(socket.getOutputStream(),true);
while(true){
String s=sin.readLine();
tx.append("@Client Said@: "+s+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt){
String st = tf.getText();
tx.append("#Server Said#: "+st+"\n");
out.println(st);
tf.setText("");
}
}
public static void main(String[] args){
MyServer msr = new MyServer();
}
}
追问
你的不错哦,谢了,虽然不是我想要的,但你的程序很值得我学习
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用socket协议
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
晚上发你
追问
好的,在此谢过了,记得发啊
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询