java基础_17.1_gui_Frame

来源:互联网 发布:单相四极电机绕组数据 编辑:程序博客网 时间:2024/06/04 20:26

聊天小程序

package java基础.Socket.myChat1;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java基础.Socket.myChat1.ChatClient.ChatClientFrame;public class ChatServer {    public static void main(String[] args) {        //服务器端建立监听        ServerSocket ss = IoTools.getServerSocket(9999);        try {            Socket s = ss.accept();//服务器已经建立了监听,如果没有监听到客户端的连接,是不会往下执行的,监听到连接后开始往下执行            System.out.println("一个客户端已经和服务器端建立了连接");            //开始通讯            ChatServerFrame c = new ChatServer().new ChatServerFrame();            c.lunch(s);            //通讯结束后,关闭相关资源,断开连接            IoTools.close();        } catch (IOException e) {            e.printStackTrace();        }    }    class ChatServerFrame extends Frame {        StringBuffer sb = new StringBuffer();        TextArea ta = new TextArea();        TextField tf = new TextField();        public ChatServerFrame() {            super("服务器端");        }        public void lunch(final Socket s) {            this.setBackground(Color.blue);            this.setBounds(400, 100, 800, 600);            this.setResizable(true);            this.setVisible(true);            ta.setBackground(Color.blue);            this.add(ta,BorderLayout.CENTER);            this.add(tf,BorderLayout.SOUTH);            tf.requestFocus();            tf.addActionListener(new ActionListener() {                @Override                public void actionPerformed(ActionEvent e) {                    TextField t = (TextField)e.getSource();                    String ss = new ChatServerFrame().getTitle();                    sb.append(ss+"\n"+t.getText());                    ta.setText(sb.toString());                    IoTools.send_buf(s,t.getText());//客户端尝试从Socket管道发送数据给服务器端                    tf.setText("");                }            });            this.addWindowListener(new WindowAdapter() {                @Override                public void windowClosing(WindowEvent e) {                    System.exit(-1);                }            });            String str = IoTools.read_buf_socket(s);            while(!str.equalsIgnoreCase("bye")) {                sb.append("客户端\n"+str+"\n");                ta.setText(sb.toString());                str = IoTools.read_buf_socket(s);            }        }    }}
package java基础.Socket.myChat1;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.net.Socket;import java.util.UUID;import java基础.Socket.myChat1.ChatServer.ChatServerFrame;public class ChatClient {    public static void main(String[] args) {        //与服务器端建立连接        Socket s = IoTools.getClientSokect("localhost", 9999);        System.out.println("与服务器端建立连接");        //开始通讯        ChatClientFrame c = new ChatClient().new ChatClientFrame();        c.lunch(s);        //结束通讯后,关闭相关资源        IoTools.close();    }    class ChatClientFrame extends Frame {        public static final String textname = "客户端";        StringBuffer sb = new StringBuffer();         TextArea ta = new TextArea();        TextField tf = new TextField();        public ChatClientFrame() {            super(textname);        }        public void lunch(final Socket s) {            ta.setBackground(Color.green);            this.add(ta, BorderLayout.CENTER);            this.add(tf, BorderLayout.SOUTH);            tf.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent e) {                    try {                        TextField t = (TextField)e.getSource();                        String ss = new ChatClientFrame().getTitle()+s.getPort();                        sb.append(ss+"\n"+t.getText());                        ta.setText(sb.toString());                        IoTools.send_buf(s,t.getText());//客户端尝试从Socket管道发送数据给服务器端                        tf.setText("");                    } catch (Exception ae) {                        ae.printStackTrace();                    }                }            });            setBounds(300, 30, 800, 600);            this.setVisible(true);            this.setTitle(textname);            tf.requestFocus();            this.addWindowListener(new WindowAdapter() {                @Override                public void windowClosing(WindowEvent e) {                    System.exit(-1);                }            });            String str = IoTools.read_buf_socket(s);            while(!str.equalsIgnoreCase("bye")) {                sb.append("服务器端\n"+str+"\n");                ta.setText(sb.toString());                str = IoTools.read_buf_socket(s);            }        }    }}
1 0