A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 田旭阳 黑马帝   /  2012-9-21 13:13  /  1778 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

写了毕老师讲课的两个程序关于GUI和udp聊天程序的,但不知道怎样把它俩结合在一起,即形成一个图形化界面聊天工具!
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

class Send  implements Runnable{
private DatagramSocket ds;
public Send(DatagramSocket ds){
  this.ds =ds;
}
public void run(){
  try{
   BufferedReader bufr =
     new BufferedReader(new InputStreamReader(System.in));
   String line =null;
   
   while((line =bufr.readLine())!=null){
    if("886".equals(line))
      break;
    byte[] buf =line.getBytes();
     
    DatagramPacket dp =
      new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002);
    ds.send(dp);
  }}catch(Exception e){
   throw new RuntimeException("发送端异常");
  }
}  
}
class Rece  implements Runnable{
private DatagramSocket ds;
public Rece( DatagramSocket ds){
this.ds =ds;
}
public void run(){
  while(true){
   byte[] buf =new byte[1024];
   DatagramPacket dp= new DatagramPacket(buf,buf.length);
   try {
    ds.receive(dp);
   } catch (IOException e) {
    throw new RuntimeException("接受端异常");
   }
   String ip = dp.getAddress().getHostAddress();
   String data =new String(dp.getData(),0,dp.getLength());
   int port=dp.getPort();
  
   System.out.println(ip+": :"+data+": :"+port);
   }
}  
}
public class chatTest {
public static void main(String[] args) throws Exception{
  
   DatagramSocket sendSocket= new DatagramSocket();
   DatagramSocket receSocket= new DatagramSocket(10002);
   
   new Thread(new Send(sendSocket)).start();
   new Thread(new Rece(receSocket)).start();
  }}
另一个关于界面的
import java.awt.*;
import java.awt.event.*;
class myWindow{
  private Frame f;
  private TextArea ta;
  private TextField tf;
  private Button b;
  myWindow(){
   init();
  }
  public void init(){
  f= new Frame("my awt");
  f.setSize(500,400);
  f.setLocation(300,200);
  f.setLayout(new FlowLayout());
  ta=new TextArea(15, 50);
  tf=new TextField(50);
  b =new Button("发送");
  f.add(ta);
  f.add(tf);
  f.add(b);
  
  myEvent();
  f.setVisible(true);
  }

private void myEvent(){
f.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
   System.exit(0);
   }});
   //怎样在外面访问内部类的对象Text?例如不能在这里访问System.out.println(Text);
b.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    String text=tf.getText();
    ta.setText(text);
    tf.setText("");
   }
  });

}
public static void main(String[] args){
  new myWindow();
}
}
我的思路是把聊天天程序的发送端和接收端都写入界面程序的内部类new ActionListener,可这样尝试了却没能成功

多线程怎么写入内部类呢,可能我思路错了吧!

希望大家在我代码的基础上合并改写一下;不胜感激!

最好写下你们的思想,控制台程序转化成图形界面的的技巧有吗。。

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 通过UI组建触发事件

查看全部评分

2 个回复

倒序浏览
外表调用内部的方法需要在外部实例化在内部类 如果这个内部类是静态的可以直接调用
回复 使用道具 举报
田旭阳 来自手机 黑马帝 2012-9-21 19:44:54
藤椅
这个很难吗?亲们!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马