急求用带jui界面写的java聊天程序!!!

 我来答
bill76554792
2010-10-12 · TA获得超过954个赞
知道小有建树答主
回答量:723
采纳率:50%
帮助的人:592万
展开全部
客户端
------------------------
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

class myframe extends Frame implements ActionListener,WindowListener,Runnable,KeyListener
{
Thread mythread = new Thread(this);
Socket mysocket;
DataInputStream in;
DataOutputStream out;

Label label_ip = new Label("IP");
Label label_port = new Label("Port");
TextField text_ip = new TextField("127.1.1.0",15);
TextField text_port = new TextField("8888",15);
Button button_connect = new Button("连接");

TextArea text_area_show = new TextArea();

TextField text_send = new TextField(45);
Button button_send = new Button("发送");

myframe()
{
Panel panel1 = new Panel();
Panel panel2 = new Panel();

panel1.setLayout(new FlowLayout());
panel1.add(label_ip);
panel1.add(text_ip);
panel1.add(label_port);
panel1.add(text_port);
panel1.add(button_connect);

panel2.setLayout(new FlowLayout());
panel2.add(text_send);
panel2.add(button_send);

add(panel1,BorderLayout.NORTH);
add(text_area_show,BorderLayout.CENTER);
add(panel2,BorderLayout.SOUTH);

text_send.addKeyListener(this);
button_connect.addActionListener(this);
button_send.addActionListener(this);
addWindowListener(this);

this.setTitle("客户端");
setBounds(200,200,400,350);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button_connect)
{
try
{
String ip = null,port = null;
ip = text_ip.getText();
port = text_port.getText();
mysocket = new Socket(ip, Integer.parseInt(port));
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
}
catch (UnknownHostException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
mythread.start();

}
if(e.getSource() == button_send)
{
if(mysocket.isConnected() == true)
{
String temp = null;
temp = text_send.getText();
try
{
out.writeUTF(temp);
text_send.setText(null);
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

public void run()
{
while(true)
{
String temp = null;
try
{
temp = in.readUTF();
}
catch (IOException ex)
{
text_area_show.append("服务器退出\n");
return;
}
temp += "\n";
text_area_show.append(temp);
}
}

public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
String temp = null;
temp = text_send.getText();
try
{
out.writeUTF(temp);
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
public class mywindow
{
public static void main(String argv[])
{
myframe f = new myframe();
}
}

服务器
-------------------------------------
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

class SocketList
{
int length = 0;
Socket socketlist[] = new Socket[10];
SocketList()
{
}
void add(Socket s)
{
if(length == 10)
{
return;
}
socketlist[length] = s;
length++;
}
void Delete(Socket s)
{
int i = 0;
for(i = 0;i < length;i++)
{
if(socketlist[i] == s)
{
break;
}
}
for(int j = i;j < length;j++)
{
socketlist[j] = socketlist[j + 1];
}
length--;
/*
for(int k = 0;k < length;k++)
{
if(socketlist[k].isBound() == true)
{
System.out.print("error");
}
}*/
}
}

class FrameThread extends Thread
{
myframe frame;
FrameThread(myframe f)
{
frame = f;
start();
}
public void run()
{
while(true)
{
InetAddress addr = null;
Socket s = null;
try
{
s = frame.server.accept();
addr = s.getInetAddress();
frame.socketlist.add(s);
for(int i = 0;i < frame.socketlist.length;i++)
{
if(frame.socketlist.socketlist[i] != s )
{
DataInputStream in = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());
DataOutputStream out= new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());
out.writeUTF(addr + "连接到服务器");
}
}
}
catch (IOException ex)
{
Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);
}
frame.text_area_show.append(addr + "连接到服务器\n");
try
{
frame.list.add(InetAddress.getLocalHost().getHostAddress() + "-" + s.getPort());
}
catch (UnknownHostException ex)
{
Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
new ClientThread(s, frame);
}
catch (IOException ex)
{
Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

class ClientThread extends Thread
{
DataInputStream in = null;
DataOutputStream out = null;
Socket mysocket = new Socket();
myframe frame = null;
InetAddress addr = null;
ClientThread(Socket s,myframe f) throws IOException
{
mysocket = s;
addr = mysocket.getLocalAddress();
frame = f;
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
out.writeUTF("成功连接服务器");
start();
}
public void run()
{
while(true)
{
String temp = null;
try
{
temp = in.readUTF();
for(int i = 0;i < frame.socketlist.length;i++)
{
DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());
DataOutputStream out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());
out_temp.writeUTF(addr + "说: " + temp);
}
frame.text_area_show.append(addr + "说: " + temp + "\n");
}
catch (IOException ex)
{
frame.socketlist.Delete(mysocket);
frame.text_area_show.append(addr + "退出服务器\n");
for(int i = 0;i < frame.socketlist.length;i++)
{
try
{
DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());
}
catch (IOException ex1)
{
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);
}
DataOutputStream out_temp = null;
try
{
out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());
}
catch (IOException ex1)
{
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);
}
try
{
out_temp.writeUTF(addr + "退出服务器");
}
catch (IOException ex1)
{
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);
}
}
return;
}
}
}
}

class myframe extends JFrame implements ActionListener,WindowListener,KeyListener
{
List list = new List(16);
TextArea text_area_show = new TextArea(15,40);
TextField text_send = new TextField(50);
Button button_send = new Button("发送");
ServerSocket server = null;
FrameThread thread;
SocketList socketlist = new SocketList();

myframe() throws IOException
{
Panel panel1 = new Panel();
panel1.setLayout(new FlowLayout());
panel1.add(text_area_show);
panel1.add(list);
list.addActionListener(this);

Panel panel2 = new Panel();
panel2.setLayout(new FlowLayout());
panel2.add(text_send);
panel2.add(button_send);

setLayout(new FlowLayout());
add(panel1);
add(panel2);

text_send.addKeyListener(this);
button_send.addActionListener(this);
addWindowListener(this);

server = new ServerSocket(8888);
thread = new FrameThread(this);

setTitle("服务器");
setBounds(200,200,450,330);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == list)
{
text_area_show.append(list.getSelectedItem() + "\n");
}
if(e.getSource() == button_send)
{
for(int i = 0;i < socketlist.length;i++)
{
try
{
DataInputStream in = new DataInputStream(socketlist.socketlist[i].getInputStream());
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
DataOutputStream out = null;
try
{
out = new DataOutputStream(socketlist.socketlist[i].getOutputStream());
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
out.writeUTF(text_send.getText());
text_send.setText(null);
// text_area_show.append(text_send.getText() + "\n");
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
for(int i = 0;i < socketlist.length;i++)
{
try
{
DataInputStream in = new DataInputStream(socketlist.socketlist[i].getInputStream());
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
DataOutputStream out = null;
try
{
out = new DataOutputStream(socketlist.socketlist[i].getOutputStream());
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
out.writeUTF(text_send.getText());
text_area_show.append(text_send.getText() + "\n");
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void keyReleased(KeyEvent e) {}
}
public class mywindow
{
public static void main(String argv[]) throws IOException
{
myframe f = new myframe();
}
}
luhfly
2010-10-11 · TA获得超过341个赞
知道小有建树答主
回答量:695
采纳率:0%
帮助的人:425万
展开全部
群聊,要不?单聊没做,也不会做了.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
忘却度记忆4u
2010-10-11 · TA获得超过1080个赞
知道小有建树答主
回答量:1048
采纳率:50%
帮助的人:562万
展开全部
单聊群聊都有的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式