zoukankan      html  css  js  c++  java
  • 有图形界面的聊天程序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    package socket;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
      
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
       
    public class GUIServer {
       
        public static void main(String[] args) throws Exception {
               
            JFrame f = new JFrame();
            f.setTitle("server");
               
            f.setSize(400300);
            f.setLocation(100200);
            f.setLayout(null);
               
            JButton b = new JButton("send");
            b.setBounds(10108030);
            f.add(b);
              
            final JTextField tf = new JTextField();
            tf.setBounds(101108030);
            f.add(tf);
               
            final JTextArea ta = new JTextArea();
            ta.setBounds(110,10200300);
            f.add(ta);
               
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
     
            ServerSocket ss = new ServerSocket(8888);
             
            System.out.println("listenning on port:8888");
            final Socket s =  ss.accept();       
            new Thread() {
                public void run() {
                    while (true) {
                        try {
                            DataInputStream dis = new DataInputStream(
                                    s.getInputStream());
                            String text = dis.readUTF();
                            ta.append(text+" ");
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
      
                }
            }.start();
              
            b.addActionListener(new ActionListener() {
                  
                @Override
                public void actionPerformed(ActionEvent e) {
                      
                    String text = tf.getText();
                    ta.append(text+" ");
                      
                    try {
                        DataOutputStream dos = new DataOutputStream(
                                s.getOutputStream());
                        dos.writeUTF(text);
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
              
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    package socket;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
      
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
       
    public class GUIClient {
       
        public static void main(String[] args) throws Exception {
               
            JFrame f = new JFrame();
            f.setTitle("client");
               
            f.setSize(400300);
            f.setLocation(600200);
            f.setLayout(null);
               
            JButton b = new JButton("send");
            b.setBounds(10108030);
            f.add(b);
              
            final JTextField tf = new JTextField();
            tf.setBounds(101108030);
            f.add(tf);
               
            final JTextArea ta = new JTextArea();
            ta.setBounds(110,10200300);
            f.add(ta);
               
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
               
            final Socket s = new Socket("127.0.0.1"8888);
              
            new Thread() {
                public void run() {
                    while (true) {
                        try {
                            DataInputStream dis = new DataInputStream(
                                    s.getInputStream());
                            String text = dis.readUTF();
                            ta.append(text+" ");
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
      
                }
            }.start();
              
            b.addActionListener(new ActionListener() {
                  
                @Override
                public void actionPerformed(ActionEvent e) {
                      
                    String text = tf.getText();
                    ta.append(text+" ");
                      
                    try {
                        DataOutputStream dos = new DataOutputStream(
                                s.getOutputStream());
                        dos.writeUTF(text);
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
              
        }
    }
  • 相关阅读:
    1-29反射
    1-28Map简介
    1-27TreeSet简介
    1-26HashSet简介
    1-25泛型
    1-24List三个子类的特点
    1-23集合概述
    Java接口
    1-22日期类型
    简易计算器的实现
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10195125.html
Copyright © 2011-2022 走看看