一个Client端一个server端,先启动server端进行监听响应client端,然后启动client端进行聊天
//时间关系,粗略的做了一下,一个模拟UDP协议的测试,图标,IP,以及端口都可以设成
//发送者端的电脑参数!
import?java.awt.BorderLayout;
import?java.awt.Color;
import?java.awt.GridLayout;
import?java.awt.event.KeyAdapter;
import?java.awt.event.KeyEvent;
import?java.awt.event.MouseAdapter;
import?java.awt.event.MouseEvent;
import?java.io.IOException;
import?java.io.Serializable;
import?java.net.DatagramPacket;
import?java.net.DatagramSocket;
import?java.net.InetAddress;
import?java.net.SocketException;
import?java.net.UnknownHostException;
import?java.text.SimpleDateFormat;
import?java.util.Date;
import?javax.swing.ImageIcon;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JMenu;
import?javax.swing.JMenuBar;
import?javax.swing.JMenuItem;
import?javax.swing.JPanel;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
private?JTextArea?txtRece,?txtSend;//?接受与发送文本域!
private?JScrollPane?jsp;
private?JLabel?jab;//?标签
private?JButton?jb;//?按钮
private?JMenuBar?jmb;//?窗体状态栏
private?JMenuItem?a=null,b=null;
private?DatagramSocket?dsend?=?null,?drece;
private?String[][][]?menArr=?{{{"语言"},{"中文","英文"}},{{"字体"},{"隶书","彩云",}},{{"辅助"},{"放大","缩小"}},{{"功能"},{"计算","闹钟"}}};
this.setTitle("山寨QQ测试");//?窗体标题
this.setResizable(false);
init();
this.setJMenuBar(jmb);
this.setVisible(true);
}
private?void?init()?{
jmb?=?new?JMenuBar();
for(int?i=0;imenArr.length;i◆◆)?{
for(int?j=0;jmenArr[i].length;j◆◆)?{
for(int?x=0;xmenArr[i][j].length;x◆◆)?{
if(j==0)?{
a=new?JMenu(menArr[i][j][x]);
}else?{
b=new?JMenuItem(menArr[i][j][x]);
a.add(b);
jmb.add(a);
jp1?=?new?JPanel();
jp1.setLayout(new?BorderLayout());
txtRece?=?new?JTextArea();
jsp?=?new?JScrollPane(txtRece);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(jsp);
txtSend?=?new?JTextArea();
jab?=?new?JLabel();
jab.setHorizontalAlignment(JLabel.CENTER);//?标签文本居中
jb?=?new?JButton("发送");
myeve();
private?void?myeve()?{
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//?窗体可关闭
try?{
dsend?=?new?DatagramSocket();//?发送端套接字
}?catch?(SocketException?e)?{
e.printStackTrace();
re();//?启动无限循环侦听!
txtSend.addKeyListener(new?KeyAdapter()?{
public?void?keyPressed(KeyEvent?k)?{
if?(k.isControlDown()?k.getKeyCode()?==?KeyEvent.VK_ENTER)?{
se();
if?(k.isAltDown()?k.getKeyCode()?==?KeyEvent.VK_S)?{
});
jb.addMouseListener(new?MouseAdapter()?{
public?void?mouseClicked(MouseEvent?arg0)?{
txtSend.requestFocus();
private?void?se()?{
by1?=?txtSend.getText().getBytes();
dsend.send(dp1);//?推送数据
txtSend.setText(null);
}?catch?(UnknownHostException?e)?{
}?catch?(IOException?e)?{
private?void?re()?{
new?Thread()?{
public?void?run()?{
while?(true)?{
String?time?=?new?SimpleDateFormat("MM-dd/HH:mm").format(new?Date());
txtRece.append("用户IP:"?◆?ip?◆?"?时间:");
txtRece.append(time?◆?":?"?◆?System.getProperty("line.separator"));
txtRece.setCaretPosition(txtRece.getText().length());
}.start();
public?static?void?main(String[]?args)?{
类似QQ的聊天工具?有一种叫luma的QQ程序,是用java编写的,而且代码开放,适用于各种安装了java平台的计算机.
不过好像已经不跟新了,可惜了.
腾讯官方现在也提供了java版本的qq现在
①.、swing的界面可以直接用netbeans画出来嘛.
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.*;
public?class?ChatRoom?extends?JFrame?implements?MouseListener?{
/**
*?
*/
private?static?final?long?serialVersionUID?=?1L;
new?ChatRoom();
private?JFrame?frame;
private?JTextArea?viewArea;
private?JTextField?viewField;
private?JButton?button1;
private?JLabel?jlable;
private?JTextField?MyName;
public?ChatRoom(){
frame?=?new?JFrame("Chat?Room");
jlable=?new?JLabel();
jlable.setText("在线");
button1?=?new?JButton("Send");
MyName?=?new?JTextField();
MyName.setText("飞翔的企鹅?");
JPanel?panel?=?new?JPanel();
panel.add(jlable);
panel.add(MyName);
panel.add(button1);
JScrollPane?sp?=?new?JScrollPane(viewArea);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add("Center",sp);
frame.add("East",panel);
frame.add("South",viewField);
frame.setVisible(true);
button1.addMouseListener((MouseListener)?this);
public?void?mouseClicked(MouseEvent?evt){
String?message?=?"";
message=MyName.getText()◆viewField.getText();
if(evt.getSource()==button1){
viewArea.setText(viewArea.getText()◆message◆?"\n")?;
message?=?"退出";
viewArea.setText(message);
viewField.setText("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public?void?mousePressed(MouseEvent?evt){?}
public?void?mouseReleased(MouseEvent?evt){?}
public?void?mouseEntered(MouseEvent?e){?}
public?void?mouseExited(MouseEvent?e){?}
简单得很的那种要不要?就像用来应对考试一样.
import?java.io.*;
import?java.net.*;
import?java.util.*;
public?class?ChatServer?{
boolean?started?=?false;
ServerSocket?ss?=?null;
ListClient?clients?=?new?ArrayListClient();
new?ChatServer().start();
public?void?start()?{
started?=?true;
}?catch?(BindException?e)?{
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
}?
try?{?
while(started)?{
Socket?s?=?ss.accept();
Client?c?=?new?Client(s);
?????System.out.println("a?client?connected!");
new?Thread(c).start();
clients.add(c);
}?finally?{
ss.close();
class?Client?implements?Runnable?{
private?Socket?s;
private?DataInputStream?dis?=?null;
private?DataOutputStream?dos?=?null;
private?boolean?bConnected?=?false;
public?Client(Socket?s)?{
this.s?=?s;
dis?=?new?DataInputStream(s.getInputStream());
dos?=?new?DataOutputStream(s.getOutputStream());
bConnected?=?true;
public?void?send(String?str)?{
dos.writeUTF(str);
while(bConnected)?{
String?str?=?dis.readUTF();
????????System.out.println(str);
for(int?i=0;?iclients.size();?i◆◆)?{
Client?c?=?clients.get(i);
c.send(str);
}?catch?(EOFException?e)?{
System.out.println("Client?closed!");
if(dis?!=?null)?dis.close();
if(dos?!=?null)?dos.close();
if(s?!=?null)?{
s.close();
//s?=?null;
}?catch?(IOException?e1)?{
e1.printStackTrace();
public?class?ChatClient?extends?Frame?{
Socket?s?=?null;
DataOutputStream?dos?=?null;
DataInputStream?dis?=?null;
TextField?tfTxt?=?new?TextField();
TextArea?taContent?=?new?TextArea();
Thread?tRecv?=?new?Thread(new?RecvThread());
new?ChatClient().launchFrame();?
public?void?launchFrame()?{
add(tfTxt,?BorderLayout.SOUTH);
add(taContent,?BorderLayout.NORTH);
pack();
this.addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?arg0)?{
disconnect();
tfTxt.addActionListener(new?TFListener());
setVisible(true);
connect();
tRecv.start();
public?void?connect()?{
System.out.println("connected!");
public?void?disconnect()?{
dos.close();
dis.close();
private?class?TFListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?str?=?tfTxt.getText().trim();
tfTxt.setText("");
dos.flush();
private?class?RecvThread?implements?Runnable?{
taContent.setText(taContent.getText()?◆?str?◆?'\n');
System.out.println("bye!");
以上就是土嘎嘎小编为大家整理的类似QQ聊天程序java源代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!