- 相关推荐
java编写即时聊天程序
怎样用java编写一个即时聊天程序呢?下面yjbys小编就为大家分享最新的java即时聊天程序编写方法,一起来看看吧!
import java.awt.*;
import java.awt.event.*;
import java.net.*;
/**
* Sample application using Frame.
*
* @author
* @version 1.00 07/06/13
*/
public class Chatp2pFrame extends Frame {
List dataList=new List(26);
TextField Tfip=new TextField(18);
TextField Tfdata=new TextField(19);
Label Lip=new Label("输入对方的IP地址:");
Label Ldata=new Label("输入聊天的内容:");
DatagramSocket socket=null;
/**
* The constructor.
*/
public Chatp2pFrame() {
try
{
socket=new DatagramSocket(5000);
}
catch(Exception e)
{
e.printStackTrace();
}
Tfip.setSize(150,15);
Tfdata.setSize(150,15);
Tfip.setFocusable(true);
Tfip.setForeground(Color.blue);
Tfdata.setForeground(Color.red);
dataList.setForeground(Color.black);
this.add(dataList,"North");
Panel p1=new Panel();
Panel p2=new Panel();
p1.setLayout(new BorderLayout());
p2.setLayout(new BorderLayout());
this.add(p1,"Center");
this.add(p2,"South");
p1.add(Lip,"West");
p1.add(Ldata,"East");
p2.add(Tfip,"West");
p2.add(Tfdata,"East");
new Thread(new Runnable()
{
public void run()
{
byte[] buf=new byte[1024];
DatagramPacket packet=new DatagramPacket(buf,1024);
while(true)
{
try
{
socket.receive(packet);
dataList.add(new String(buf,0,packet.getLength())+" FROM "+packet.getAddress().getHostAddress()+":"+packet.getPort(),0);
}
catch(Exception e)
{
if(!socket.isClosed())
{
e.printStackTrace();
}
}
}
}
}
).start();
Tfdata.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
byte[] buf=new byte[1024];
buf=Tfdata.getText().getBytes();
DatagramPacket packet=new DatagramPacket(buf,buf.length,
InetAddress.getByName(Tfip.getText()),5000);
socket.send(packet);
dataList.add(new String(buf,0,packet.getLength())+" TO "+packet.getAddress().getHostAddress()+":"+packet.getPort(),0);
}
catch(Exception x)
{
x.printStackTrace();
}
Tfdata.setText("");
}
}
);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("文件");
menuFileExit.setLabel("退出");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Chatp2pFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("我的聊天程序");
setMenuBar(menuBar);
setSize(new Dimension(310, 500));
this.setResizable(false);
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Chatp2pFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
socket.close();
System.exit(0);
}
public static void main(String[] args) {
// Create application frame.
Chatp2pFrame frame = new Chatp2pFrame();
// Show frame
frame.setVisible(true);
}
}
【java编写即时聊天程序】相关文章:
Java与Java web有什么不同01-22
计算机二级Java语言程序设计考试试题09-06
编导专业编写故事的技巧02-17
社交技巧聊天技巧04-25
商务场合的网络聊天礼仪04-22
Java代码的基本知识09-03
Java语言中的流程控制09-05
Java获取当前的系统时间的方法11-03
企业能源审计的程序02-13
旅游投诉的处理程序04-09