资源简介
基于UDP协议的java聊天代码 希望对大家有用!
代码片段和文件信息
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Date;
import java.util.*;
import java.text.*;
public class UDPChat implements RunnableActionListener{
JTextArea showArea;
JLabel lbl1lbl2lbl3lbl4;
JTextField msgTextsendPortTextreceivePortTextIPAddressText;
Jframe mainJframe;
JButton sendBtnstartBtn;
private JSplitPane panevpaneh;
JScrollPane scrollpane1scrollpane2;
JPanel pane1pane2;
Container con;
Thread thread=null;
DatagramPacket sendPackreceivePack;
DatagramSocket sendSocketreceiveSocket;
private JRadioButton radiobutton1radiobutton2;
private InetAddress sendIP;
private int sendPortreceivePort; //存储发送端口和接收端口
private byte inBuf[] outBuf[];
public static final int BUFSIZE=1024;
//Date time=new java.util.Date();
SimpleDateFormat format=new SimpleDateFormat(“kk:mm:ss“);
//String timeInfo=format.format(time);
public UDPChat() {
mainJframe=new Jframe(“QQ聊天室“);
con=mainJframe.getContentPane();
showArea=new JTextArea();
showArea.setEditable(false);
scrollpane1=new JScrollPane(showArea);
showArea.setLineWrap(true);
pane2=new JPanel();
lbl1=new JLabel(“接收端口号:“);
lbl2=new JLabel(“发送端口号:“);
lbl3=new JLabel(“对方的地址:“);
lbl4=new JLabel(“性别“);
lbl4.setFont(new Font(“serif“Font.PLAIN30));
lbl4.setForeground(Color.BLACK);
sendPortText=new JTextField();
sendPortText.setColumns(5);
receivePortText=new JTextField();
receivePortText.setColumns(5);
IPAddressText=new JTextField();
IPAddressText.setColumns(8);
startBtn=new JButton(“开始“);
ButtonGroup bg=new ButtonGroup();
radiobutton1=new JRadioButton(“男“);
radiobutton1.setSelected(true);
radiobutton2=new JRadioButton(“女“);
bg.add(radiobutton1);
bg.add(radiobutton2);
startBtn.addActionListener(this);
pane2.setLayout(new FlowLayout());
pane2.add(lbl1);
pane2.add(receivePortText);
pane2.add(lbl2);
pane2.add(sendPortText);
pane2.add(lbl3);
pane2.add(IPAddressText);
pane2.add(startBtn);
pane2.add(lbl4);
pane2.add(radiobutton1);
pane2.add(radiobutton2);
pane1=new JPanel();
msgText=new JTextField();
msgText.setColumns(15);
msgText.setFont(new Font(“serif“Font.PLAIN18));
msgText.setEditable(false);
msgText.addActionListener(this);
sendBtn=new JButton(“发送“);
sendBtn.setEnabled(false);
sendBtn.addActionListener(this);
pane1.setLayout(new FlowLayout());
pane1.add(msgText);
pane1.add(sendBtn);
panev=new JSplitPane(JSplitPane.VERTICAL_SPLITscrollpane1pane1);
panev.setDividerLocation(290);
paneh=new JSplitPane(JSplitPane.HORIZONTAL_SPLITpanevpane2);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5276 2011-01-07 00:05 UDPChat.java
----------- --------- ---------- ----- ----
5276 1
评论
共有 条评论