黑马程序员技术交流社区
标题:
通宵写的一个窗口化聊天对话框,有时间大家一起完善它
[打印本页]
作者:
黄云南
时间:
2012-3-16 23:31
标题:
通宵写的一个窗口化聊天对话框,有时间大家一起完善它
通宵写的一个窗口化聊天的,大家有时间一起把他做完美了啊
import java.awt.event.*;
import java.awt.*;
import java.util.concurrent.locks.*;
import java.net.*;
import java.io.*;
import java.lang.*;
/*class UdpLiaoTian
{
public static void main(String[] aregs)
{
new UdpFaSong().start();
new UdpJieShou().start();
}
}*/
class ArrayTest
{
static Frame f;
static Button b;
static TextField tf;
static TextArea ta;
public static void main(String[] args)
{
f = new Frame("聊天中。。。");
f.setLayout(null);//让布局格式为空
b = new Button("发送");
tf = new TextField();
ta = new TextArea();
f.setBounds(350,300,600,400);
b.setBounds(500,350,60,30);
tf.setBounds(10,350,450,50);
ta.setBounds(10,30,550,300);
ta.enableInputMethods(false);
f.add(b);
f.add(tf);
f.add(ta);
f.setVisible(true);//让窗口显示出来
b.addActionListener(new ButtonEvent());//按钮活动事件
f.addWindowListener(new FrameEvent());//窗口活动事件
tf.addKeyListener(new FileTextEvent());//
ta.addKeyListener(new TextAreaEvent());
new Thread(new UdpJieShou()).start();
}
}
class UdpJieShou extends Thread
{
public void run()
{
try
{
DatagramSocket ds = new DatagramSocket(8001);
byte[] buf =new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
while(true)
{
ds.receive(dp);
ArrayTest.ta.append(dp.getAddress()+new String(dp.getData(),0,dp.getLength() ));
}
}
catch (Exception e)
{
throw new RuntimeException("jieshouchucuo");
}
}
}
class FrameEvent extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class ButtonEvent implements ActionListener
{
Lock lock= new ReentrantLock();
Condition con = lock.newCondition();
private String st="nihao2";
boolean flag=true;
public void actionPerformed(ActionEvent e)
{
String str = ArrayTest.tf.getText();
ArrayTest.ta.append("自己的:\n"+str+"\n");
fasong(str);
ArrayTest.tf.setText(" ");
}
public void fasong(String str)
{
lock.lock();
try
{
st=str;
DatagramSocket ds = new DatagramSocket();
byte[] buf=st.getBytes();
DatagramPacket dp =new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.100"),8000);
ds.send(dp);
//ds.close();
System.out.print("meiyou");
}
catch(Exception a)
{
throw new RuntimeException("fasongyichang");
}
finally
{
lock.unlock();
}
}
}
class FileTextEvent extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==10)
{
ActionEvent a=null;
new ButtonEvent().actionPerformed(a);
}
}
}
class TextAreaEvent extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
e.consume();
}
}
复制代码
作者:
何招俊
时间:
2012-3-16 23:44
厉害 赞一个
作者:
冯林
时间:
2012-3-17 12:47
坏习惯,没注释。想别认看懂你的代码,比让他自己写还辛苦。{:soso_e120:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2