qq群聊代码java 群聊代码大全可复制恶搞
java的swing高手帮我,设计qq群聊天面板
java 网络编程 ServerSocket serverSocket =new ServerSocket (9000);//
10多年建站经验, 做网站、网站建设客户的见证与正确选择。成都创新互联提供完善的营销型网页建站明细报价表。后期开发更加便捷高效,我们致力于追求更美、更快、更规范。
版面你用 Jbuild 画吧 NetBean 也可以。在对应的事件里添加代码就成了,实现比较麻烦~~~
重点就是Java Socket编成这块。
用java实现QQ群聊
很久前写的代码了,以前能运行。电脑没装jdk,没试试,自己试一下把。。
---------------服务器------------
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();
}
}
java QQ群
java QQ群:126379271
这是一个新群,初学者或者是高手都可加入。
我们期待你的到来。
标题名称:qq群聊代码java 群聊代码大全可复制恶搞
URL标题:http://pwwzsj.com/article/docdepi.html