• 有图形界面的聊天程序


    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();
                    }
                }
            });
              
        }
    }
  • 相关阅读:
    C++ 派生类成员的访问属性
    C++ 继承和派生介绍
    CSAPP lab2 二进制拆弹 binary bombs phase_5
    C++ new和delete 堆和栈
    Substrings (C++ find函数应用)
    CSAPP lab2 二进制拆弹 binary bombs phase_4
    CSAPP lab2 二进制拆弹 binary bombs phase_3
    C++ 实验 使用重载运算符实现一个复数类
    C++ 操作符重载
    UVALive 4877 Non-Decreasing Digits 数位DP
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10195125.html
Copyright © 2020-2023  润新知