|
/** * 基于UDP協(xié)議的聊天程序 * * 2007.9.18 * */ //導(dǎo)入包 import java.awt.*; import java.awt.event.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.net.*; public class Chat extends JFrame implements ActionListener { //廣播地址或者對方的地址 public static final String sendIP = '127.0.0.1'; //發(fā)送端口9527 public static final int sendPort = 8000; JPanel p = new JPanel(); List lst = new List(); //消息顯示 JTextField txtIP = new JTextField(18); //填寫IP地址 JTextField txtMSG = new JTextField(20); //填寫發(fā)送消息 JLabel lblIP = new JLabel('IP地址:'); JLabel lblMSG = new JLabel('消息:'); JButton btnSend = new JButton('發(fā)送'); byte [] buf; //定義DatagramSocket的對象必須進(jìn)行異常處理 //發(fā)送和接收數(shù)據(jù)報(bào)包的套接字 DatagramSocket ds = null; //=============構(gòu)造函數(shù)===================== public Chat() { CreateInterFace(); //注冊消息框監(jiān)聽器 txtMSG.addActionListener(this); btnSend.addActionListener(this); try { //端口:9527 ds =new DatagramSocket(sendPort); } catch(Exception ex) { ex.printStackTrace(); } //============接受消息============ //匿名類 new Thread(new Runnable() { public void run() { byte buf[] = new byte[1024]; //表示接受數(shù)據(jù)報(bào)包 while(true) { try { DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort); ds.receive(dp); lst.add('【消息來自】◆' + dp.getAddress().getHostAddress() + '◆'+'【說】:' + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0); } catch(Exception e) { if(ds.isClosed()) { e.printStackTrace(); } } } } }).start(); //關(guān)閉窗體事件 this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent w) { System.out.println('test'); int n=JOptionPane.showConfirmDialog(null,'是否要退出?','退出',JOptionPane.YES_NO_OPTION); if(n==JOptionPane.YES_OPTION) { dispose(); System.exit(0); ds.close();//關(guān)閉ds對象//關(guān)閉數(shù)據(jù)報(bào)套接字 } } }); } //界面設(shè)計(jì)布局 public void CreateInterFace() { this.add(lst,BorderLayout.CENTER); this.add(p,BorderLayout.SOUTH); p.add(lblIP); p.add(txtIP); p.add(lblMSG); p.add(txtMSG); p.add(btnSend); txtIP.setText(sendIP); //背景顏色 lst.setBackground(Color.yellow); //JAVA默認(rèn)風(fēng)格 this.setUndecorated(true); this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); this.setSize(600,500); this.setTitle('〓聊天室〓'); this.setResizable(false);//不能改變窗體大小 this.setLocationRelativeTo(null);//窗體居中 this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); this.setVisible(true); txtMSG.requestFocus();//消息框得到焦點(diǎn) } //===============================Main函數(shù)=============================== public static void main(String[]args) { new Chat(); } //================================發(fā)送消息=============================== //消息框回車發(fā)送消息事件 public void actionPerformed(ActionEvent e) { //得到文本內(nèi)容 buf = txtMSG.getText().getBytes(); //判斷消息框是否為空 if (txtMSG.getText().length()==0) { JOptionPane.showMessageDialog(null,'發(fā)送消息不能為空','提示',JOptionPane.WARNING_MESSAGE); } else{ try { InetAddress address = InetAddress.getByName(sendIP); DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort); ds.send(dp); } catch(Exception ex) { ex.printStackTrace(); } } txtMSG.setText('');//清空消息框 //點(diǎn)發(fā)送按鈕發(fā)送消息事件 if(e.getSource()==btnSend) { buf = txtMSG.getText().getBytes(); try { DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort); } catch(Exception ex) { ex.printStackTrace(); } txtMSG.setText('');//清空消息框 txtMSG.requestFocus(); } } } 熱心網(wǎng)友 2009-11-30
|
|
|