當前位置:編程學習大全網 - 源碼下載 - JAVA聊天器

JAVA聊天器

太難為我了,我只知道程序是從哪走到哪的,具體的變量有些看不明白

程序是先運行public static void main(String args []) throws Exception

然後運行大括號中的內容,在其中調用UdpChat();

轉到 public UdpChat() throws Exception,這裏面程序比較復雜,可能會出錯

這兩句this.setSize(400,400);this.setVisible(true);設置壹個400*400的界面,並使其可見,

ds = new DatagramSocket(3000);應該是反轉字符串,不知道幹什麽用,

this.setLayout(new BorderLayout());是設置this的布局管理器為BorderLayout()

this.getContentPane().add(jsp,"Center");將取得的組件添加到容器中

JPanel jpl = new JPanel();將對象JPanel創建到jp1中

jpl.setLayout(new BorderLayout());

jpl.add(jtfIp,"West");

jpl.add(jtfData,"East");調用三個函數,但是不知道從哪調用過來的

this.getContentPane().add(jpl,"South");同樣將取得的組件添加到容器中

new Thread(new Runnable()

{

public void run()

{ while(true)

{ try

{

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf,buf.length);

ds.receive(dp);

String str = new String (dp.getData(),0,dp.getLength())+" from"+dp.getAddress().getHostAddress()+":"+dp.getPort()+"\n";

//System.out.println(str);

jta.append(str);

}

catch (Exception e)

{

}

}

}

}

).start();最重要的代碼之壹,簡單聊天工具必備語句--永真循環,每次聊天發送語句是都是靠這句來判斷發送

new Thread(new Runnable()

{

public void run()

{ while(true)

{ try

{

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf,buf.length);

ds.receive(dp);

String str = new String (dp.getData(),0,dp.getLength())+" from"+dp.getAddress().getHostAddress()+":"+dp.getPort()+"\n";

//System.out.println(str);

jta.append(str);

}

catch (Exception e)

{

}

}

}

}

).start();

jtfData.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

byte[] buf;

buf = jtfData.getText().getBytes();

try

{

DatagramPacket dp = new DatagramPacket(buf,0,buf.length,InetAddress.getByName(jtfIp.getText()),3000);

ds.send(dp);

jtfData.setText("");

}

catch (Exception ex)

{

}

}

}

);

有點不明白,可能程序不完整吧,猜想是將聊天語句送給對方

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

ds.close();

System.exit(0);

}

}

);

}當程序運行到這裏的時候就是退出程序的時候

  • 上一篇:c#.net中dataset.table["數據表名稱"].rows.count無法賦值給int型變量
  • 下一篇:simulink仿真
  • copyright 2024編程學習大全網