
求java两人聊天室程序 有图形 源代码,请高手指教
1个回答
2012-05-14 · 知道合伙人软件行家
关注

展开全部
package socketserver;
import socketserver.ReadThread;
import java.io.DataInputStream;
import java.io.IOException;
import javax.swing.JTextArea;
public class ReadThread extends Thread{
JTextArea ta;
DataInputStream dis;
public ReadThread(JTextArea t,DataInputStream d)
{
this.ta=t;
this.dis=d;
}
public void run(){
try{
while(true)
{
ta.append("对方说:"+dis.readUTF());
ta.append("\n");
}
}
catch(IOException e)
{
System.out.println("连接中断!");
}
}
}
---------------------------------------------
package socketserver;
import socketserver.ReadThread;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestServer extends JFrame implements ActionListener{
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea ta;
public TestServer(){
this.setTitle("TT服务器");
JScrollPane jp=new JScrollPane();
ta =new JTextArea(10,10);
Panel p=new Panel();
tf=new JTextField(20);
JButton b = new JButton("发送");
b.addActionListener(this);
tf.addActionListener(this);
p.add(tf);
p.add(b);
jp.setViewportView(ta);
this.getContentPane().add(jp);
this.getContentPane().add("South",p);
this.setSize(350,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
tf.requestFocus();
this.connect();
this.createReadThread();
}
public void connect(){
while(true)
try{
ServerSocket ss=new ServerSocket(911);
Socket s2=ss.accept();
InputStream is=s2.getInputStream();
dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
dos=new DataOutputStream(os);
}
catch(IOException e)
{
System.out.println("连接服务器故障!");
}
}
public void createReadThread(){
ReadThread rt=new ReadThread(this.ta,this.dis);
rt.start();
}
public void actionPerformed(ActionEvent e){
try{
String s=tf.getText();
dos.writeUTF(s);
ta.append("自己说: "+s);
ta.append("\n");
tf.setText("");
tf.requestFocus();
}
catch(IOException e1)
{
e1.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
new TestServer();
}
}
-------------------------------------------------------------
package socketserver;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TextClient extends JFrame implements ActionListener{
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea ta;
String s11,s22;
public TextClient(String s1,String s2){
this.setTitle("TT聊天--作者:扈海涛");
JScrollPane jp=new JScrollPane();
ta =new JTextArea(10,10);
Panel p=new Panel();
tf=new JTextField(20);
JButton b=new JButton("发送");
b.addActionListener(this);
tf.addActionListener(this);
p.add(tf);
p.add(b);
jp.setViewportView(ta);
this.getContentPane().add(jp);
this.getContentPane().add("South",p);
this.setSize(350,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.s11=s1;
this.s22=s2;
this.setVisible(true);
tf.requestFocus();
this.connect();
this.createReadThread();
}
public void connect(){
try{
Socket s2=new Socket(s22,911);
InputStream is=s2.getInputStream();
dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
dos=new DataOutputStream(os);
}catch(IOException e){
System.out.println("连接服务器故障!");
}
}
public void createReadThread(){
ReadThread rt=new ReadThread(this.ta,this.dis);
rt.start();
}
public void actionPerformed(ActionEvent arg0) {
try{
String s=tf.getText();
dos.writeUTF(s11+"说:"+s);
ta.append("自己说:"+s);
ta.append("\n");
tf.setText("");
tf.requestFocus();
}catch(IOException e1)
{
e1.printStackTrace();
}
}
public static void main(String[]args){
new TextClient("","192.168.1.177");
}
}
看不懂了在问我
import socketserver.ReadThread;
import java.io.DataInputStream;
import java.io.IOException;
import javax.swing.JTextArea;
public class ReadThread extends Thread{
JTextArea ta;
DataInputStream dis;
public ReadThread(JTextArea t,DataInputStream d)
{
this.ta=t;
this.dis=d;
}
public void run(){
try{
while(true)
{
ta.append("对方说:"+dis.readUTF());
ta.append("\n");
}
}
catch(IOException e)
{
System.out.println("连接中断!");
}
}
}
---------------------------------------------
package socketserver;
import socketserver.ReadThread;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestServer extends JFrame implements ActionListener{
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea ta;
public TestServer(){
this.setTitle("TT服务器");
JScrollPane jp=new JScrollPane();
ta =new JTextArea(10,10);
Panel p=new Panel();
tf=new JTextField(20);
JButton b = new JButton("发送");
b.addActionListener(this);
tf.addActionListener(this);
p.add(tf);
p.add(b);
jp.setViewportView(ta);
this.getContentPane().add(jp);
this.getContentPane().add("South",p);
this.setSize(350,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
tf.requestFocus();
this.connect();
this.createReadThread();
}
public void connect(){
while(true)
try{
ServerSocket ss=new ServerSocket(911);
Socket s2=ss.accept();
InputStream is=s2.getInputStream();
dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
dos=new DataOutputStream(os);
}
catch(IOException e)
{
System.out.println("连接服务器故障!");
}
}
public void createReadThread(){
ReadThread rt=new ReadThread(this.ta,this.dis);
rt.start();
}
public void actionPerformed(ActionEvent e){
try{
String s=tf.getText();
dos.writeUTF(s);
ta.append("自己说: "+s);
ta.append("\n");
tf.setText("");
tf.requestFocus();
}
catch(IOException e1)
{
e1.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
new TestServer();
}
}
-------------------------------------------------------------
package socketserver;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TextClient extends JFrame implements ActionListener{
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea ta;
String s11,s22;
public TextClient(String s1,String s2){
this.setTitle("TT聊天--作者:扈海涛");
JScrollPane jp=new JScrollPane();
ta =new JTextArea(10,10);
Panel p=new Panel();
tf=new JTextField(20);
JButton b=new JButton("发送");
b.addActionListener(this);
tf.addActionListener(this);
p.add(tf);
p.add(b);
jp.setViewportView(ta);
this.getContentPane().add(jp);
this.getContentPane().add("South",p);
this.setSize(350,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.s11=s1;
this.s22=s2;
this.setVisible(true);
tf.requestFocus();
this.connect();
this.createReadThread();
}
public void connect(){
try{
Socket s2=new Socket(s22,911);
InputStream is=s2.getInputStream();
dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
dos=new DataOutputStream(os);
}catch(IOException e){
System.out.println("连接服务器故障!");
}
}
public void createReadThread(){
ReadThread rt=new ReadThread(this.ta,this.dis);
rt.start();
}
public void actionPerformed(ActionEvent arg0) {
try{
String s=tf.getText();
dos.writeUTF(s11+"说:"+s);
ta.append("自己说:"+s);
ta.append("\n");
tf.setText("");
tf.requestFocus();
}catch(IOException e1)
{
e1.printStackTrace();
}
}
public static void main(String[]args){
new TextClient("","192.168.1.177");
}
}
看不懂了在问我
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询