-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwing.java
More file actions
103 lines (78 loc) · 2.67 KB
/
Swing.java
File metadata and controls
103 lines (78 loc) · 2.67 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package chat.swing;
import chat.network.TCPConnection;
import chat.network.TCPConnectionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Swing extends JFrame implements TCPConnectionListener, ActionListener
{
private static final String IP_ADDRES = "192.168.1.190";
private static final int PORT = 8189;
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
private JTextArea textArea = new JTextArea();
private JTextField writeArea = new JTextField();
private JTextField name = new JTextField();
//private JButton send;
TCPConnection connection;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new Swing();
}
});
}
private Swing(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setAlwaysOnTop(true);
textArea.setEditable(false);
textArea.setLineWrap(true);
add(textArea, BorderLayout.CENTER);
writeArea.addActionListener(this);
add(writeArea, BorderLayout.SOUTH);
add(name, BorderLayout.NORTH);
setVisible(true);
try {
connection = new TCPConnection(this,IP_ADDRES,PORT);
} catch (IOException e) {
printMsg("Exception: " + e);
}
}
@Override
public void actionPerformed(ActionEvent e) {
String msg = writeArea.getText();
if(msg.equals("")) return;
writeArea.setText(null);
connection.sendString(name.getText() + ": " + msg);
}
@Override
public void onConnectionReady(TCPConnection tcpConnection) {
printMsg("Connection ready...");
}
@Override
public void onReceiveString(TCPConnection tcpConnection, String value) {
printMsg(value);
}
@Override
public void onDisconnect(TCPConnection tcpConnection) {
printMsg("Connection close...");
}
@Override
public void onException(TCPConnection tcpConnection, Exception e) {
printMsg("Connection Exception" + e);
}
private synchronized void printMsg(String msg){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
textArea.append(msg + "\n");
textArea.setCaretPosition(textArea.getDocument().getLength());
}
});
}
}