找回密码
 入驻
搜索
查看: 335|回复: 2

java程序设计

[复制链接]
发表于 2010-3-27 20:11:41 | 显示全部楼层 |阅读模式
客户端代码:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
        Socket s = null;
        DataOutputStream dos = null;
        public static void main(String[] args) {
                new ChatClient().launchFrame();
        }
       
        TextField tfTxt = new TextField();
        TextArea taContent = new TextArea();
       
        public void launchFrame() {
                this.setLocation(400, 300);
                setSize(300,300);
                add(tfTxt,BorderLayout.SOUTH);
                add(taContent,BorderLayout.NORTH);
                pack();
                this.addWindowListener(new WindowAdapter() {

                        @Override
                        public void windowClosing(WindowEvent e) {
                                disconnect();
                                System.exit(0);
                        }                       
                });
                tfTxt.addActionListener(new TFListener());       
                this.setVisible(true);
                connect();
        }
       
        public void connect() {
                try {
                        s = new Socket("172.16.42.30",8888);
                        dos = new DataOutputStream(s.getOutputStream());
System.out.println("connected!");
                } catch (UnknownHostException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
       
        public void disconnect() {
                try {
                        dos.close();
                        s.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
               
        }
       
        private class TFListener implements ActionListener {

                @Override
                public void actionPerformed(ActionEvent e) {
                        String str = tfTxt.getText().trim();
                        taContent.setText(str);
                        tfTxt.setText("");
                        try {
                                 
                                dos.writeUTF(str);
                                dos.flush();
                                //dos.close();
                        } catch (IOException e1) {
                                e1.printStackTrace();
                        }
                       
                }
               
        }
       
}
发表于 2010-5-21 20:42:32 | 显示全部楼层
好东西~~收藏起来~~
回复

使用道具 举报

发表于 2010-6-15 12:23:14 | 显示全部楼层

看不懂。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 入驻

本版积分规则

QQ|Archiver|手机版|小黑屋|思明论坛

GMT+8, 2024-5-3 01:30 , Processed in 0.026419 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表