Java在线视频聊天(4)
scroll2=new JScrollPane(txt2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
p1.add(scroll);
p1.add(p3);
p2.add(btn1);
p2.add(btn2);
p3.add(jpic,"North");
p3.add(scroll2,"Center");
add(p1,"Center");
add(p2,"South");
setVisible(true);
Font f=new Font("",Font.PLAIN,18);
txt1.setFont(f);
txt1.setForeground(Color.red);
txt2.setFont(f);
txt2.setForeground(Color.blue);
btn1.setBackground=\'#\'" /div>
btn2.setBackground=\'#\'" /div>
btn1.addActionListener(this);
btn2.addActionListener(this);
thread=new Thread(this);
thread.start();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn2)
{
byte buffer[]=txt2.getText().trim().getBytes();
try
{
InetAddress address=InetAddress.getByName("localhost");
DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,888);
DatagramSocket mail_data=new DatagramSocket();
txt1.append("我说:"+txt2.getText()+'\n');
mail_data.send(data_pack);
txt2.setText("");
}
catch(Exception e1)
{
System.out.println("聊天信息发送失败!");
}
}
}
public void run()
{
DatagramPacket pack=null;
DatagramSocket mail_data=null;
byte data[]=new byte[8192];
try
{
pack=new DatagramPacket(data,data.length);
mail_data=new DatagramSocket(666);
}
catch(Exception e){ }
while(true)
{
if(Thread.currentThread()==thread)
{
if(mail_data==null)
{break;}
else
{
try
{
mail_data.receive(pack);
int length=pack.getLength();
String message=new String(pack.getData(),0,length);
txt1.append("某某说:"+message+'\n');
}
catch(Exception e)
{
System.out.println("接收数据失败!");
}
}
}
}// while end
} //run() end
}
本文出自 “技术人生” 博客,请务必保留此出处http://zhaohaiyang.blog.51cto.com/2056753/511639
相关新闻>>
- 发表评论
-
- 最新评论 更多>>