java版QQ 欢迎点评
2010-12-17 18:34 libing64 阅读(225) 评论(0) 编辑 收藏 举报/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
刚写的java版QQ,实现C/S聊天室功能,与在线好友聊天,添加删除好友。选择UDP数据报而不是socket,不区分client和sever,直接通信。
*/
package myqq;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
/**
*
* @author honor school libing
*
*/
public class MyQQ {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
DrawQFrame frame = new DrawQFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//面板类
class DrawQFrame extends JFrame {
//框架类
public DrawQFrame() {
this.setLocation(100, 100);
this.setTitle("360QQ");
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setLayout(new BorderLayout());
DrawQPanel panel = new DrawQPanel();
this.add(panel, BorderLayout.NORTH);
Border etched = BorderFactory.createEtchedBorder();
Border title = BorderFactory.createTitledBorder(etched, "360QQ");//加入边框
panel.setBorder(title);
Border lineborder = BorderFactory.createLineBorder(Color.yellow);
panel.setBorder(lineborder);
// Border matteBorder = BorderFactory.createMatteBorder(30, 30, 30, 30, Color.BLUE);
// panel.setBorder(matteBorder);
}
public static final int DEFAULT_WIDTH = 200;
public static final int DEFAULT_HEIGHT = 400;
}
//面板类
class DrawQPanel extends JPanel {
public DrawQPanel() {
this.setSize(100, 300);
// this.setLayout(new CardLayout(10,1));
this.setLayout(new BorderLayout());
JButton button = new JButton("与神秘陌生人聊天");
this.add(button, new BorderLayout().CENTER);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("你好");
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
});
setLayout(new GridLayout(15, 1));
JTextField textField = new JTextField();
textField.setText("好友昵称或Q号");
JButton searchButton = new JButton("搜索好友");
this.add(textField);
this.add(searchButton);
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
friendList.add(bestFriend);
friendList.add(bestFriend2);
// buttonList.add(new JButton(friendList.get(0).name + "(" + friendList.get(0).nickname + ")" + friendList.get(0).autograph));
// buttonList.add(new JButton(friendList.get(1).name + "(" + friendList.get(1).nickname + ")" + friendList.get(1).autograph));
setLayout(new GridLayout(15, 1));
for (int i = 0; i < friendList.size(); i++) {
buttonList.add(new JButton(friendList.get(i).name + "(" + friendList.get(i).nickname + ")" + friendList.get(i).autograph));
this.add(buttonList.get(i));
final String tempName = friendList.get(i).name;
final String tempNickname = friendList.get(i).nickname;
final String tempAutograph = friendList.get(i).autograph;
buttonList.get(i).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DrawFrame talkFrame = new DrawFrame();
talkFrame.setTitle(tempName + "(" + tempNickname + ")" + tempAutograph);
talkFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
talkFrame.setVisible(true);
}
});
}
JButton addFriendButton = new JButton("添加好友");
this.add(addFriendButton);
addFriendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addFriendFrame addFriendFrame1 = new addFriendFrame();
addFriendFrame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
addFriendPanel panel = new addFriendPanel();
addFriendFrame1.add(panel);
final JTextField friendName = new JTextField(10);
final JTextField friendNickname = new JTextField(10);
final JTextField friendAutograph = new JTextField(10);
JLabel nameLabel = new JLabel("好友姓名");
JLabel nicknameLabel = new JLabel("好友昵称");
JLabel autographLabel = new JLabel("好友签名");
JButton button1 = new JButton("保存");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
friendList.remove(friendList.size() - 1);
friendList.remove(friendList.size() - 1);
buttonList.clear();
DrawQPanel.friendList.add(new MyFriend(friendName.getText(), friendNickname.getText(), friendAutograph.getText()));
DrawQFrame frame = new DrawQFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
});
panel.add(friendName);
panel.add(nameLabel);
panel.add(friendNickname);
panel.add(nicknameLabel);
panel.add(friendAutograph);
panel.add(autographLabel);
panel.add(button1);
addFriendFrame1.setSize(200, 180);
addFriendFrame1.setLocation(500, 400);
addFriendFrame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
addFriendFrame1.setVisible(true);
}
});
}
public class MyFriend {
private MyFriend(String string, String string0, String string1) {
this.name = string;
this.nickname = string0;
this.autograph = string1;
}
public void MyFriend(String name, String nickname, String autograph) {
this.name = name;
this.nickname = nickname;
this.autograph = autograph;
}
private String name;
private String nickname;
private String autograph;
}
public class addFriendFrame extends JFrame {
public void addFriendFrame() {
JFrame addFriendFrame = new JFrame("添加好友");
addFriendFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
addFriendFrame.add(panel);
JButton button1 = new JButton("保存");
panel.add(button1);
addFriendFrame.setSize(200, 180);
}
}
class addFriendPanel extends JPanel {
public addFriendPanel() {
JPanel addFriendPanel = new JPanel();
}
}
public ArrayList getFriendList() {
return this.friendList;
}
public ArrayList getbuttonList() {
return this.buttonList;
}
private static ArrayList<MyFriend> friendList = new ArrayList<MyFriend>(10);
MyFriend bestFriend = new MyFriend("李兵", "火", "Never Give up!");
MyFriend bestFriend2 = new MyFriend("实验学院", "honor school", "Never Give up!");
private static ArrayList<JButton> buttonList = new ArrayList<JButton>(10);
}
class DrawFrame extends JFrame {
public DrawFrame() {
setLocation(100, 100);
setTitle("360QQ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
//添加菜单项……
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("菜单");
menuBar.add(menu);
JMenuItem sset = new JMenuItem("设置");
JMenuItem eexit = new JMenuItem("退出");
menu.add(sset);
menu.addSeparator();
menu.add(eexit);
JMenu video = new JMenu("视频");
menuBar.add(video);
JMenuItem startVideo = new JMenuItem("发起视频通话", new ImageIcon("computer.ico"));
JMenuItem mutiVideo = new JMenuItem("多人视频聊天");
JMenuItem setVideo = new JMenuItem("视频设置");
JMenuItem testVideo = new JMenuItem("视频测试向导");
video.add(startVideo);
video.addSeparator();
video.add(mutiVideo);
video.addSeparator();
video.add(setVideo);
video.addSeparator();
video.add(testVideo);
startVideo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame littleFrame = new JFrame();
JOptionPane.showMessageDialog(littleFrame, "正在连接,请稍后……");
littleFrame.dispose();
}
});
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame littleFrame = new JFrame();
JOptionPane.showMessageDialog(littleFrame, "正在连接,请稍后……");
littleFrame.dispose();
}
}
mutiVideo.addActionListener(new MyListener());
setVideo.addActionListener(new MyListener());
testVideo.addActionListener(new MyListener());
JMenu voice = new JMenu("语音");
menuBar.add(voice);
JMenuItem startVoice = new JMenuItem("发起语音通话");
JMenuItem mutiVoice = new JMenuItem("多人语音通话");
JMenuItem setVoice = new JMenuItem("语音设置");
JMenuItem testVoice = new JMenuItem("语音测试向导");
voice.add(startVoice);
voice.addSeparator();
voice.add(mutiVoice);
voice.addSeparator();
voice.add(setVoice);
voice.addSeparator();
voice.add(testVoice);
startVoice.addActionListener(new MyListener());
mutiVoice.addActionListener(new MyListener());
setVoice.addActionListener(new MyListener());
testVoice.addActionListener(new MyListener());
JMenu file = new JMenu("文件");
menuBar.add(file);
JMenuItem sendFile = new JMenuItem("发送文件");
JMenuItem sendMutiFile = new JMenuItem("发送文件夹");
JMenuItem sendOfflineFile = new JMenuItem("发送离线文件");
JMenuItem setFile = new JMenuItem("发送设置");
file.add(sendFile);
file.addSeparator();
file.add(sendMutiFile);
file.addSeparator();
file.add(sendOfflineFile);
file.addSeparator();
file.add(setFile);
sendFile.addActionListener(new MyListener());
sendMutiFile.addActionListener(new MyListener());
sendOfflineFile.addActionListener(new MyListener());
setFile.addActionListener(new MyListener());
JMenu SMS = new JMenu("短信");
menuBar.add(SMS);
JMenuItem sendSMS = new JMenuItem("给该好友发短信");
JMenuItem sendMutiSMS = new JMenuItem("群发短信");
JMenuItem sendOfflineSMS = new JMenuItem("发送离线短信");
JMenuItem setSMS = new JMenuItem("短信设置");
SMS.add(sendSMS);
SMS.addSeparator();
SMS.add(sendMutiSMS);
SMS.addSeparator();
SMS.add(sendOfflineSMS);
SMS.addSeparator();
SMS.add(setSMS);
sendSMS.addActionListener(new MyListener());
sendMutiSMS.addActionListener(new MyListener());
sendOfflineSMS.addActionListener(new MyListener());
setSMS.addActionListener(new MyListener());
JMenu sue = new JMenu("举报");
menuBar.add(sue);
JMenuItem sueFriend = new JMenuItem("举报该好友");
JMenuItem deleteFriend = new JMenuItem("删除该好友");
JMenuItem blackListFriend = new JMenuItem("将该好友移至黑名单");
sue.add(sueFriend);
sue.addSeparator();
sue.add(deleteFriend);
sue.addSeparator();
sue.add(blackListFriend);
sueFriend.addActionListener(new MyListener());
deleteFriend.addActionListener(new MyListener());
blackListFriend.addActionListener(new MyListener());
JMenu game = new JMenu("休闲娱乐");
menuBar.add(game);
JMenuItem QQLive = new JMenuItem("QQLive网络电视");
JMenuItem cellphoneQQ = new JMenuItem("手机聊QQ");
JMenuItem WebGame = new JMenuItem("网络游戏");
JMenuItem cleverGame = new JMenuItem("益智游戏");
JMenuItem adorn = new JMenuItem("装扮");
game.add(QQLive);
game.addSeparator();
game.add(cellphoneQQ);
game.addSeparator();
game.add(WebGame);
game.addSeparator();
game.add(cleverGame);
game.addSeparator();
game.add(adorn);
QQLive.addActionListener(new MyListener());
cellphoneQQ.addActionListener(new MyListener());
WebGame.addActionListener(new MyListener());
cleverGame.addActionListener(new MyListener());
adorn.addActionListener(new MyListener());
final DrawPanel pan = new DrawPanel();
//添加设置对话框
sset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
final JFrame jf = new JFrame("设置");
jf.setSize(300, 150);
jf.setLocation(200, 200);
jf.setResizable(false);
jf.setVisible(true);
JPanel jp = new JPanel();
final JTextField jt1 = new JTextField(20);
final JTextField jt2 = new JTextField(20);
final JTextField jt3 = new JTextField(20);
jp.add(new JLabel("聊天端口:"));
jf.add(jp);
jp.add(jt1);
jp.add(new JLabel("目标地址:"));
jp.add(jt2);
jp.add(new JLabel("文件端口:"));
jp.add(jt3);
JButton jb1 = new JButton("确定");
JButton jb2 = new JButton("取消");
jp.add(jb1);
jp.add(jb2);
//设置“设置”按钮
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
int cat1 = Integer.parseInt(jt1.getText());
pan.setCat1(cat1);//设置对话端口号
int cat2 = Integer.parseInt(jt3.getText());
pan.setCat2(cat2);//设置文件传输端口号
String s = jt2.getText();
pan.setIP(s);//设置IP地址
pan.ls();
} catch (Exception e) {
e.printStackTrace();
}
jf.dispose();//关闭对话框
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jf.dispose();
}
});
}
});
eexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
add(pan);
pack();
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
}
class DrawPanel extends JPanel {
DatagramSocket ds;
DatagramPacket dp;
DatagramSocket ds1;
DatagramPacket dp1;
Thread thread;
Thread thread1;
TextArea textArea1 = new TextArea(10, 50);
TextArea textArea2 = new TextArea(10, 50);
JTextField textField = new JTextField(50);
static int cat1, cat2;
static String IP;
public void setCat1(int cat) {
cat1 = cat;
}
public void setCat2(int cat) {
cat2 = cat;
}
public void setIP(String ip) {
IP = ip;
}
//重置端口号
public void ls() {
try {
ds = new DatagramSocket(cat1);
ds1 = new DatagramSocket(cat2);
} catch (Exception e) {
e.printStackTrace();
}
thread = new Thread(new Runnable() {
public void run() {
byte buf[] = new byte[1024];//聊天每次发送文字不能超过1024b
DatagramPacket dp = new DatagramPacket(buf, buf.length);
while (true) {
try {
ds.receive(dp);
textArea1.setText(textArea1.getText() + new String(buf, 0, dp.getLength()) + "/r/n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
public DrawPanel() {
setLayout(new BorderLayout());
JTabbedPane card = new JTabbedPane();
textArea1.setEditable(false);
card.add("聊天", textArea1);
add(card, BorderLayout.NORTH);
JPanel center = new JPanel();
center.setLayout(new FlowLayout());
center.add(new JLabel("昵称:"));
center.add(textField);
add(center, BorderLayout.CENTER);
JPanel south = new JPanel();
south.setLayout(new BorderLayout());
south.add(textArea2, BorderLayout.NORTH);
JPanel south2 = new JPanel();
south2.setLayout(new FlowLayout());
JButton sent = new JButton("传送文件");
JButton receive = new JButton("接收文件");
//receive.setEnabled(false);
JButton sentMessage = new JButton("发送");
south2.add(sent);
south2.add(receive);
south2.add(sentMessage);
add(south, BorderLayout.SOUTH);
south.add(south2, BorderLayout.SOUTH);
//发送信息的按钮的监听器:
sentMessage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textArea1.append(textField.getText() + ":" + textArea2.getText() + "/n");
String s;
try {
s = textField.getText() + ":" + textArea2.getText();
byte[] buf = s.getBytes();
dp = new DatagramPacket(buf, buf.length, InetAddress.getByName(IP), cat1);
ds.send(dp);
textArea2.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
//发送文件的按钮的监听器
sent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFrame f = new JFrame("打开");
f.setSize(400, 300);
JFileChooser chooser = new JFileChooser();
f.add(chooser);
int returnVal = chooser.showOpenDialog(f);
if (returnVal == JFileChooser.APPROVE_OPTION) {
textArea1.append("正在传送文件:" + chooser.getCurrentDirectory() + "//"
+ chooser.getSelectedFile().getName() + "/n");
try {
byte[] b = new byte[10248];
FileInputStream fis = new FileInputStream(chooser.getSelectedFile());
DataInputStream dos = new DataInputStream(fis);
dos.read(b);
dp1 = new DatagramPacket(b, b.length, InetAddress.getByName(IP), cat2);
ds1.send(dp1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
//接收文件的按钮的监听器:
receive.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFrame f1 = new JFrame("保存");
f1.setSize(400, 300);
byte buf[] = new byte[10248];
DatagramPacket dp1 = new DatagramPacket(buf, buf.length);
JFileChooser ch = new JFileChooser();
f1.add(ch);
int a = ch.showSaveDialog(f1);
if (a == JFileChooser.APPROVE_OPTION) {
String fileName = ch.getSelectedFile().getPath();
try {
ds1.receive(dp1);
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(buf);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}