有关代码如下:
text2=new TextField("说话");text2.addActionListener(this)
button=new Button("发送");button.addActionListener(this);
TextArea texta=new TextArea("显示");
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==text2||e.getSource()==button)
{byte buffer[]=text2.getText().trim().getBytes();
try    {
InetAddress address=InetAddress.getByName(text1.getText());
DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,6666);
DatagramSocket mail_data=new DatagramSocket();

texta.append("发送"+data_pack.getAddress()+text2.getText()+"\n");
mail_data.send(data_pack);
}
catch(Exception f){System.out.println("erro1");}}
}

解决方案 »

  1.   

    代码不全,不明白你要做什么,但是
    if (e.getSource() == text2 || e.getSource() == button) {
    很明显有问题
    e.getSource()方法返回的是一个Object对象,怎么能==text2呢
    e.getSource().getClass()可以获得产生事件的组件类型
    class java.awt.Button 当返回这样的字符串时说明事件是由Button组件产生的。
    e.getActionCommand() 方法可以返回组件上的文字确定事件诱发组件的方法很多,自己试试吧。
      

  2.   

    哈哈~~~~看错了,对不起
    e.getSource() == text2
    我把text2错看成字符串了