也就是下面的代码 怎么把接收的消息显示在jlist里面package UDPchat;
import java.awt.Color;
import java.awt.Container;import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class chat extends JFrame implements ActionListener{
public JTextField tip,txx;
public JLabel la1,la2;//la1输入ip,la2输入消息
public JList l1;
public JButton b1;
public DatagramSocket ds1;//接受和发送数据报包的套接字
public DatagramPacket dp1,dp2;//数据报包
public chat(){
try{
ds1=new DatagramSocket(3000);

}catch(Exception e){
e.printStackTrace();

}
new Thread(new Runnable()   //接受信息

public void run(){
while(true){
try{
byte[] bug=new byte[1024];
dp2=new DatagramPacket(bug,bug.length);
ds1.receive(dp2);

}catch(Exception e){
e.printStackTrace();
}
}
}

}).start();

l1 =new JList();
l1.setBounds(10, 10,400, 300);
la1 =new JLabel("IP");
la1.setBounds(10,350, 30, 20);
tip =new JTextField("");
tip.setBounds(70, 350,100 , 20);
la2 =new JLabel("输入消息框");
la2.setBounds(190,350, 80, 20);
txx =new JTextField("",20);
txx.setBounds(280, 350, 100, 20);
txx.addActionListener(this);
b1=new JButton("发送");
b1.setBounds(200, 400, 80, 50);
Container rq=this.getContentPane();
rq.setLayout(null);
rq.add(l1);
rq.add(la1);
rq.add(la2);
rq.add(tip);
rq.add(txx);
rq.add(b1);
this.setSize(420,500);
this.setVisible(true);


}
 public static void main(String[] args){
 chat c=new chat();
 }
     public void actionPerformed(ActionEvent e){
      if(e.getSource()==b1){
      try{                   //发送信息
      byte[] bug;
      bug=txx.getText().getBytes();
      dp1 =new DatagramPacket(bug,bug.length,InetAddress.getByName(tip.getText()),3000);
      ds1.send(dp1);
      }catch(Exception ex){
      ex.printStackTrace();
      }
      }
      
      
      
      
      
     }
}

解决方案 »

  1.   

    首先,你的button没有添加监听
    b1.addActionListener(this);//添加监听
    其次,list
    l1.setListData(new Object[]{txx.getText()});
    不过这样每次只能更新数据,会覆盖原来的
      

  2.   

    有什么方法 能显示吗   好像awt包的list可以显示
      

  3.   

    我是想把  dp2=new DatagramPacket(bug,bug.length);
                    ds1.receive(dp2);
    从ds1接收的字节转换成字符显示在l1里面。package UDPchat;
    import java.awt.Color;
    import java.awt.Container;import javax.swing.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    public class chat extends JFrame implements ActionListener{
    public JTextField tip,txx;
    public JLabel la1,la2;//la1输入ip,la2输入消息
    public JList l1;
    public JButton b1;
    public DatagramSocket ds1;//接受和发送数据报包的套接字
    public DatagramPacket dp1,dp2;//数据报包
    public chat(){
    try{
    ds1=new DatagramSocket(3000);

    }catch(Exception e){
    e.printStackTrace();

    }
    new Thread(new Runnable()   //接受信息

    public void run(){
    while(true){
    try{
    byte[] bug=new byte[1024];
    dp2=new DatagramPacket(bug,bug.length);
    ds1.receive(dp2);
    String s=new String(bug);
    l1.add(s,0);//------这样写有问题啊 

    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }

    }).start();

    l1 =new JList();
    l1.setBounds(10, 10,400, 300);
    la1 =new JLabel("IP");
    la1.setBounds(10,350, 30, 20);
    tip =new JTextField("");
    tip.setBounds(70, 350,100 , 20);
    la2 =new JLabel("输入消息框");
    la2.setBounds(190,350, 80, 20);
    txx =new JTextField("",20);
    txx.setBounds(280, 350, 100, 20);

    b1=new JButton("发送");
    b1.setBounds(200, 400, 80, 50);
    b1.addActionListener(this);
    Container rq=this.getContentPane();
    rq.setLayout(null);
    rq.add(l1);
    rq.add(la1);
    rq.add(la2);
    rq.add(tip);
    rq.add(txx);
    rq.add(b1);
    this.setSize(420,500);
    this.setVisible(true);


    }
     public static void main(String[] args){
     chat c=new chat();
     }
         public void actionPerformed(ActionEvent e){
          if(e.getSource()==b1){
          try{                   //发送信息
          byte[] bug;
          bug=txx.getText().getBytes();
          dp1 =new DatagramPacket(bug,bug.length,InetAddress.getByName(tip.getText()),3000);
          ds1.send(dp1);
          }catch(Exception ex){
          ex.printStackTrace();
          }
          }
          
          
          
          
          
         }
    }
    上面的l1。add不行啊 
      

  4.   


    DefaultListModel listModel = new DefaultListModel();
    l1.setModel(listModel);
    listModel.add(0, s);
      

  5.   


    public class chat extends JFrame implements ActionListener{
        public JTextField tip,txx;
        public JLabel la1,la2;//la1输入ip,la2输入消息
        public JList l1;
        public JButton b1;
        public DatagramSocket ds1;//接受和发送数据报包的套接字
        public DatagramPacket dp1,dp2;//数据报包
        
        //添加
        private DefaultListModel listModel=new DefaultListModel();;    
        public chat(){
            try{
                ds1=new DatagramSocket(3000);
                
            }catch(Exception e){
                e.printStackTrace();
                
            }
            
            
            l1 =new JList();
            l1.setBounds(10, 10,400, 300);
            la1 =new JLabel("IP");
            la1.setBounds(10,350, 30, 20);
            tip =new JTextField("");
            tip.setBounds(70, 350,100 , 20);
            la2 =new JLabel("输入消息框");
            la2.setBounds(190,350, 80, 20);
            txx =new JTextField("",20);
            txx.setBounds(280, 350, 100, 20);
            txx.addActionListener(this);
            b1=new JButton("发送");
            b1.addActionListener(this);
            b1.setBounds(200, 400, 80, 50);
            Container rq=this.getContentPane();
            rq.setLayout(null);
            rq.add(l1);
            rq.add(la1);
            rq.add(la2);
            rq.add(tip);
            rq.add(txx);
            rq.add(b1);
            this.setSize(420,500);
            this.setVisible(true);
            l1.setModel(listModel);//添加
            new Thread(new Runnable()   //接受信息
            { 
                public void run(){
                    while(true){
                        try{
                    byte[] bug=new byte[1024];
                    dp2=new DatagramPacket(bug,bug.length);
                    ds1.receive(dp2);
                    //
                    listModel.add(0, new String(bug));
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }
                
            }).start();
            
        }
         public static void main(String[] args){
             chat c=new chat();
         }
         public void actionPerformed(ActionEvent e){
             if(e.getSource()==b1){
                 try{                   //发送信息
                     byte[] bug;
                     bug=txx.getText().getBytes();
                     dp1 =new DatagramPacket(bug,bug.length,InetAddress.getByName(tip.getText()),3000);
                     ds1.send(dp1);
                 }catch(Exception ex){
                     ex.printStackTrace();
                 }
             }       
         }
    }