有图形界面的聊天程序

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+"\r\n");
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
  
            }
        }.start();
          
        b.addActionListener(new ActionListener() {
              
            @Override
            public void actionPerformed(ActionEvent e) {
                  
                String text = tf.getText();
                ta.append(text+"\r\n");
                  
                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+"\r\n");
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
  
            }
        }.start();
          
        b.addActionListener(new ActionListener() {
              
            @Override
            public void actionPerformed(ActionEvent e) {
                  
                String text = tf.getText();
                ta.append(text+"\r\n");
                  
                try {
                    DataOutputStream dos = new DataOutputStream(
                            s.getOutputStream());
                    dos.writeUTF(text);
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
          
    }
}

 

posted on 2018-12-29 11:52  我是司  阅读(490)  评论(0编辑  收藏  举报

导航