| 正在学习Socket编程,学到了聊天程序,用命令行的方式确实简单实现了,可是自己尝试用GUI做成图形化界面却不知为什么报错?来向即将进黑马学哥学姐求解! 
 实现效果就是QQ那样的聊天界面。
 错误是运行时异常:
 第一次点击按钮无论是发送还是接受数据都没问题,可是第二次点击发送按钮就会提示异常。
 经过检验是ds.send(dp)那里抛了IOEeption,可是为什么第一次没抛,反而之后的每次都抛呢?真的想不通!
 
 程序代码如下:
 
 复制代码import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
class ChattingFrameTest        
{
        public static void main(String[] args)throws Exception{
                DatagramSocket dsSend = new DatagramSocket();
                DatagramSocket dsReceive = new DatagramSocket(10000);
                new ChattingFrame(dsSend,dsReceive);
        }
}
class ChattingFrame
{
        private Frame f;
        private TextArea taSend,taReceive;
        private Panel pSend,pReceive;
        private Button b;
        private DatagramSocket dsSend,dsReceive;
        ChattingFrame(DatagramSocket dsSend,DatagramSocket dsReceive){
                this.dsSend = dsSend;
                this.dsReceive = dsReceive;
                init();
        }
        public void init(){
                f = new Frame("聊天程序");
                
                f.setBounds(400,200,500,420);
                f.setLayout(new BorderLayout(10,10));
                //建立上下主面板
                pSend = new Panel();
                pSend.setPreferredSize(new Dimension(300,100));
                pReceive = new Panel();
                pReceive.setPreferredSize(new Dimension(300,300));
                
                //布局下边的发送面板内的内容
                pSend.setLayout(new BorderLayout(10,10));
                taSend = new TextArea();
                taSend.setPreferredSize(new Dimension(250,60));
                b = new Button("发送");
                b.setPreferredSize(new Dimension(30,20));
                pSend.add(taSend,BorderLayout.CENTER);
                pSend.add(b,BorderLayout.EAST);
                //布局上边接收面板的内容
                pReceive.setLayout(new BorderLayout());
                taReceive = new TextArea();
                pReceive.add(taReceive);
                //把主面板添加进窗体内
                f.add(pSend,BorderLayout.SOUTH);
                f.add(pReceive,BorderLayout.CENTER);
                
                myEvent();
                f.setVisible(true);
        }
        public void myEvent(){
                f.addWindowListener(new WindowAdapter(){
                        public void windowClosing(WindowEvent e){
                                System.exit(0);
                        }
                });
                b.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                                
                                Receive r = new Receive(dsReceive);
                                
                                new Thread(new Send(dsSend,taSend.getText())).start();
                                new Thread(r).start();
                                
                                try{Thread.sleep(1000);}catch(Exception ei){}
                                taReceive.append(r.getReceive());
                                taSend.setText("");
                        }
                });
        }
}
class Send implements Runnable
{
        private DatagramSocket ds;
        private String taSend;
        
        Send(DatagramSocket ds,String taSend){
                this.ds = ds;
                this.taSend = taSend;
        }
        public void run(){
                try
                {
                        if(taSend==null)
                                return;
                        byte[] buf = taSend.getBytes();
                        
                        DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),10000);
                        ds.send(dp);
                        ds.close();
                }
                catch (UnknownHostException e)
                {
                        throw new RuntimeException("未知主机");
                }
                catch(IOException e)
                {
                        throw new RuntimeException("IO异常");
                }
        }
}
class Receive implements Runnable
{
        private DatagramSocket ds;
        private String taReceive;
        
        Receive(DatagramSocket ds){
                this.ds = ds;
        }
        public void run(){
                try
                {
                        byte[] buf = new byte[1024];
                        
                        DatagramPacket dp = new DatagramPacket(buf,buf.length);
                        ds.receive(dp);
                        
                        taReceive = dp.getAddress().getHostName()+": \r\n"+new String(dp.getData(),0,dp.getLength())+"\r\n";
                }
                catch (Exception e)
                {
                        throw new RuntimeException("接收出现错误");
                }
        }
        public String getReceive(){
                return taReceive;
        }
}
 
 
 |