哪位高手用JAVA写个QQ? 哪位高手用JAVA写个QQ? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像以前有个人在本版上发过一个Java写的QQ界面,你搜搜 主要是两方面的内容:SWING和SOCKET,看看Manning SWING那本书的上的例子,可能对你有用。 楼上说的对,主要是这两个,SOCKET里有TCP和UDP两种,不过最好还是用UDP,好做。 我做的不是qq 。是一个即使聊天的 和 qq 差不多 我写过的,下载地址。http://download.csdn.net/source/757301 貌似udp确实要简单一些,不过聊天内容发送的话还是tcp要安全一些吧 不是太难,做个一个,源码还在邮箱里,lichujing1986要就给我发邮件说下 你可以看一下这个例子:http://topic.csdn.net/u/20080914/17/e73e3a77-b177-4fef-ad26-92a6afe865c4.html 这个QQ我下了,但是我这里运行不了如果你要可以加我Q307575240 实现qq那些简单功能没问题,重要的要有一个强大的服务器,要编qq,網絡看socket編程,界面推薦JFace 我也正在用java做QQ,我们可以互相交流啊。 不是应该 而是必须udp.................... package software;import java.io.*;import java.net.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class LANChatV12 { public static void main(String args[]) { LoginFrame lf = new LoginFrame("即时通"); lf.show(); }}class LoginFrame extends JFrame { JButton BOK; JLabel LdesAddr, Lport; JTextField TFdesAddr, TFport; String desAddr, port; LoginFrame() { } LoginFrame(String title) { super(title); Frame t = this; BOK = new JButton("确定"); LdesAddr = new JLabel("目标IP:"); Lport = new JLabel(" 端口:"); TFdesAddr = new JTextField(desAddr, 12); TFport = new JTextField(port, 12); // TFpassword.setEchoChar('*'); BOK.addActionListener(new BOKListener(t)); setBackground(Color.blue); setBounds(350, 250, 200, 128); setLayout(new FlowLayout(FlowLayout.CENTER, 5, 7)); add(LdesAddr); add(TFdesAddr); add(Lport); add(TFport); add(BOK); setResizable(false); // setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } class BOKListener implements ActionListener { Frame t; BOKListener() { } BOKListener(Frame t) { this.t = t; } public void actionPerformed(ActionEvent e) { desAddr = TFdesAddr.getText(); port = TFport.getText(); t.setVisible(false); Messenger m = new Messenger(desAddr, port); m.start(); } }}class Messenger extends Thread { String desAddr; String port; int iport; TextArea content, send; JButton Bsend; ChatFrame cf; String title; Socket client; ServerSocket ss; OutputStreamWriter osw = null; InputStreamReader isr = null; BufferedReader br; String line; boolean flag;// 端口号是否正确 boolean cbc; // can be client ? int tryTurns = 3; // 客户方式尝试的次数 Messenger() { } Messenger(String desAddr, String port) { super("LANChatMessenger"); content = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY); send = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY); Bsend = new JButton("发送"); Bsend.setEnabled(false); title = "与 " + desAddr + " 聊天"; flag = true; cbc = true; cf = new ChatFrame(title, content, send, Bsend); cf.show(); this.desAddr = desAddr; this.port = port; try { iport = Integer.parseInt(port); } catch (NumberFormatException nfe) { content.append("非法的端口,程序将在4秒后自动关闭。\n"); flag = false; } } public void run() { if (!flag) { try { Thread.sleep(4000); } catch (InterruptedException ie) { System.exit(0); } System.exit(0); } content.append("正在以客户端方式进行连接.....\n"); do { cbc = true; try { client = new Socket(desAddr, iport); } catch (Exception e) { content.append("错误,无法连接地址:" + desAddr + ":" + port + '\n'); content.append("等待 4秒再连接,剩余 [ " + (tryTurns - 1) + " ]次。\n"); cbc = false; try { Thread.sleep(1000); } catch (InterruptedException ie) { System.exit(0); } } --tryTurns; } while ((tryTurns > 0) && !cbc); if (cbc) { content.append("连接成功,可以开始了。" + "\n\n"); cf.setTitle("与 " + desAddr + ":" + port + " 聊天"); send.requestFocus(); } else { content.append("客户端方式失败,现在启动服务器并等待连接。" + '\n'); try { ss = new ServerSocket(iport); } catch (IOException ioe) { content.append("\n无法创建服务,程序将在 4 秒后退出。\n"); try { Thread.sleep(4000); } catch (InterruptedException ie) { System.exit(0); } System.exit(0); } try { cf.setTitle("等待连接中....在端口:" + port); client = ss.accept(); } catch (Exception e) { content.append("\nss.accept() 方法失败,程序将在 4 秒后退出。\n"); try { Thread.sleep(4000); } catch (InterruptedException ie) { System.exit(0); } System.exit(0); } content.append("连接成功,可以开始。" + client.getInetAddress().toString() + "\n\n"); cf.setTitle("与 " + client.getInetAddress().toString() + ":" + port + " 聊天"); send.requestFocus(); } try { osw = new OutputStreamWriter(client.getOutputStream()); isr = new InputStreamReader(client.getInputStream()); br = new BufferedReader(isr); // content.append("\nbr created\n"); } catch (IOException ioe) { content.append("创建流错误,程序将在 4 秒后退出。\n"); try { Thread.sleep(4000); } catch (InterruptedException ie) { System.exit(0); } System.exit(0); } Bsend.setEnabled(true); Bsend.addActionListener(new BsendListener(content, send, osw)); try { line = br.readLine(); } catch (IOException ioe) { content.append("流读取错误,程序将在 4 秒后退出。\n"); try { Thread.sleep(4000); } catch (InterruptedException ie) { System.exit(0); } System.exit(0); } while (true) { content.append("他说 : " + line + '\n'); try { Thread.sleep(1000); } catch (InterruptedException ie) { System.exit(0); } try { line = br.readLine(); } catch (IOException ioe) { content.append("流读取错误,程序将在 4 秒后退出。\n"); try { Thread.sleep(4000); } catch (InterruptedException ie) { System.exit(0); } System.exit(0); } } } class BsendListener implements ActionListener { TextArea content; TextArea send; OutputStreamWriter osw; BsendListener(TextArea content, TextArea send, OutputStreamWriter osw) { this.content = content; this.send = send; this.osw = osw; } public void actionPerformed(ActionEvent e) { String input; input = send.getText(); if (input.length() > 0) { content.append("我说 : " + input + '\n'); try { osw.write(input + '\n', 0, input.length() + 1); osw.flush();// 晕 } catch (Exception ee) { content.append("不能发送 \"" + input + "\" , 发生了错误 : " + ee.getMessage() + '\n'); } send.setText(""); send.requestFocus(); } } }}class ChatFrame extends JFrame { TextArea content; TextArea send; JButton Bsend; ChatFrame() { } ChatFrame(final String frameTitle, final TextArea content, final TextArea send, final JButton Bsend) { super(frameTitle); this.content = content; this.send = send; this.Bsend = Bsend; this.send.setEditable(true); setLayout(null); setBounds(300, 100, 510, 500); add(content); content.setBackground(Color.WHITE); content.setBounds(2, 0, 500, 340); content.setFocusable(true); add(send); send.setBounds(2, 355, 500, 70); send.setFocusable(true); add(Bsend); Bsend.setBounds(410, 432, 60, 30); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); }} 写QQ确实难度很大,但是简单的即时聊天就很简单了。我推荐使用RMI机制,这样通讯细节就不用考虑了,可以更加专注于功能的实现。RMI是一种分布式的编程机制,使用很方便,网上有小例子,一看就明白。第一步要实现客户端和服务器的相互通讯,第二步是实现服务器对客户端信息的转发(转发给目的客户端) 关于同步访问,双重检查问题 Java中while循环和try/catch 奇怪的java.lang.NoClassDefFoundError错误 使用jpcap 获得硬件信息 我的代码有错吗 我写的一个测试 但是没有结果阿 高手帮忙 我的这段代码为什么中文会乱码 ? 算法 哪里错了,帮忙看下,先谢了 没有分了也还问 问个画圆的问题!我想画个饼图 JDK问题 在哪里能下载到JMF2.1.1.a啊?
这个QQ我下了,但是我这里运行不了
如果你要可以加我Q307575240
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class LANChatV12 {
public static void main(String args[]) {
LoginFrame lf = new LoginFrame("即时通");
lf.show();
}
}class LoginFrame extends JFrame {
JButton BOK; JLabel LdesAddr, Lport; JTextField TFdesAddr, TFport; String desAddr, port; LoginFrame() {
}
LoginFrame(String title) {
super(title);
Frame t = this;
BOK = new JButton("确定");
LdesAddr = new JLabel("目标IP:");
Lport = new JLabel(" 端口:");
TFdesAddr = new JTextField(desAddr, 12);
TFport = new JTextField(port, 12);
// TFpassword.setEchoChar('*');
BOK.addActionListener(new BOKListener(t));
setBackground(Color.blue);
setBounds(350, 250, 200, 128);
setLayout(new FlowLayout(FlowLayout.CENTER, 5, 7));
add(LdesAddr);
add(TFdesAddr);
add(Lport);
add(TFport);
add(BOK);
setResizable(false);
// setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} class BOKListener implements ActionListener {
Frame t; BOKListener() {
} BOKListener(Frame t) {
this.t = t;
} public void actionPerformed(ActionEvent e) {
desAddr = TFdesAddr.getText();
port = TFport.getText();
t.setVisible(false);
Messenger m = new Messenger(desAddr, port);
m.start();
}
}
}class Messenger extends Thread {
String desAddr; String port; int iport; TextArea content, send; JButton Bsend; ChatFrame cf; String title; Socket client; ServerSocket ss; OutputStreamWriter osw = null; InputStreamReader isr = null; BufferedReader br; String line; boolean flag;// 端口号是否正确 boolean cbc; // can be client ? int tryTurns = 3; // 客户方式尝试的次数 Messenger() {
} Messenger(String desAddr, String port) {
super("LANChatMessenger");
content = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
send = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
Bsend = new JButton("发送");
Bsend.setEnabled(false);
title = "与 " + desAddr + " 聊天";
flag = true;
cbc = true; cf = new ChatFrame(title, content, send, Bsend);
cf.show(); this.desAddr = desAddr;
this.port = port;
try {
iport = Integer.parseInt(port);
} catch (NumberFormatException nfe) {
content.append("非法的端口,程序将在4秒后自动关闭。\n");
flag = false;
}
} public void run() {
if (!flag) {
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
} content.append("正在以客户端方式进行连接.....\n");
do {
cbc = true;
try {
client = new Socket(desAddr, iport);
} catch (Exception e) {
content.append("错误,无法连接地址:" + desAddr + ":" + port + '\n');
content.append("等待 4秒再连接,剩余 [ " + (tryTurns - 1) + " ]次。\n");
cbc = false;
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
System.exit(0);
}
}
--tryTurns;
} while ((tryTurns > 0) && !cbc); if (cbc) {
content.append("连接成功,可以开始了。" + "\n\n");
cf.setTitle("与 " + desAddr + ":" + port + " 聊天");
send.requestFocus();
} else {
content.append("客户端方式失败,现在启动服务器并等待连接。" + '\n');
try {
ss = new ServerSocket(iport);
} catch (IOException ioe) {
content.append("\n无法创建服务,程序将在 4 秒后退出。\n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
try {
cf.setTitle("等待连接中....在端口:" + port);
client = ss.accept();
} catch (Exception e) {
content.append("\nss.accept() 方法失败,程序将在 4 秒后退出。\n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
content.append("连接成功,可以开始。" + client.getInetAddress().toString()
+ "\n\n");
cf.setTitle("与 " + client.getInetAddress().toString() + ":" + port
+ " 聊天");
send.requestFocus();
}
try {
osw = new OutputStreamWriter(client.getOutputStream());
isr = new InputStreamReader(client.getInputStream());
br = new BufferedReader(isr);
// content.append("\nbr created\n");
} catch (IOException ioe) {
content.append("创建流错误,程序将在 4 秒后退出。\n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
Bsend.setEnabled(true);
Bsend.addActionListener(new BsendListener(content, send, osw));
try {
line = br.readLine();
} catch (IOException ioe) {
content.append("流读取错误,程序将在 4 秒后退出。\n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
while (true) {
content.append("他说 : " + line + '\n');
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
System.exit(0);
}
try {
line = br.readLine();
} catch (IOException ioe) {
content.append("流读取错误,程序将在 4 秒后退出。\n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
}
} class BsendListener implements ActionListener {
TextArea content; TextArea send; OutputStreamWriter osw; BsendListener(TextArea content, TextArea send, OutputStreamWriter osw) {
this.content = content;
this.send = send;
this.osw = osw;
} public void actionPerformed(ActionEvent e) {
String input;
input = send.getText();
if (input.length() > 0) {
content.append("我说 : " + input + '\n');
try {
osw.write(input + '\n', 0, input.length() + 1);
osw.flush();// 晕
} catch (Exception ee) {
content.append("不能发送 \"" + input + "\" , 发生了错误 : "
+ ee.getMessage() + '\n');
}
send.setText("");
send.requestFocus();
}
}
}
}class ChatFrame extends JFrame {
TextArea content; TextArea send; JButton Bsend; ChatFrame() {
} ChatFrame(final String frameTitle, final TextArea content,
final TextArea send, final JButton Bsend) {
super(frameTitle);
this.content = content;
this.send = send;
this.Bsend = Bsend; this.send.setEditable(true);
setLayout(null);
setBounds(300, 100, 510, 500);
add(content);
content.setBackground(Color.WHITE);
content.setBounds(2, 0, 500, 340);
content.setFocusable(true);
add(send);
send.setBounds(2, 355, 500, 70);
send.setFocusable(true);
add(Bsend);
Bsend.setBounds(410, 432, 60, 30);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
我推荐使用RMI机制,这样通讯细节就不用考虑了,可以更加专注于功能的实现。
RMI是一种分布式的编程机制,使用很方便,网上有小例子,一看就明白。
第一步要实现客户端和服务器的相互通讯,第二步是实现服务器对客户端信息的转发(转发给目的客户端)