-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJchat.java
144 lines (135 loc) · 3.85 KB
/
Jchat.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.chat;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class JChat {
public static void main(String[] args) {
new ChatGui();
}
}
class ChatGui extends Frame{
private Panel chatArea,buttonArea;
private TextArea record,message;
private TextField usr;
private Button send,close,clear;
private Label target;
ChatGui(){
super("欢迎使用java聊天");
init();
try{
new Thread(new JSocket(10086)).start();
}catch (Exception e){
record.append("未成功构造接收器\r\n");
}
}
private void init(){
chatArea = new Panel(new BorderLayout());
buttonArea = new Panel(new FlowLayout());
usr = new TextField("127.0.0.1",15);
send = new Button("发送");
close = new Button("关闭");
clear = new Button("清屏");
record = new TextArea("",14,1,TextArea.SCROLLBARS_VERTICAL_ONLY);
message = new TextArea("",8,1,TextArea.SCROLLBARS_NONE);
target = new Label("目标用户IP");
chatArea.add(record,BorderLayout.NORTH);
chatArea.add(message,BorderLayout.SOUTH);
buttonArea.add(target);
buttonArea.add(usr);
buttonArea.add(send);
buttonArea.add(close);
buttonArea.add(clear);
add(chatArea,BorderLayout.NORTH);
add(buttonArea,BorderLayout.SOUTH);
setAppearance(454,490);
setEvent();
setVisible(true);
setResizable(false);
}
private void setEvent(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
record.setText("");
}});
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
send();
message.setText("");
}});
message.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
send();
}
}
public void keyReleased(KeyEvent e){
message.setText("");
}
});
}
private void send(){
try{
new JSocket().sendMsg(message.getText(),usr.getText());
}catch (Exception ie){
record.append("未成功构造发送器\r\n\r\n");
}
record.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())+
"我对"+usr.getText()+"说:"+message.getText()+"\r\n\r\n");
}
private void setAppearance(int width,int height){
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(((int)scr.getWidth()-width)/2,((int)scr.getHeight()-height)/2);
setSize(width,height);
setBackground(Color.GRAY);
record.setBackground(Color.WHITE);
Font style = new Font("",Font.PLAIN,15);
record.setFont(style);
message.setFont(style);
usr.setFont(style);
target.setFont(style);
send.setFont(style);
close.setFont(style);
clear.setFont(style);
record.setEditable(false);
}
private class JSocket extends DatagramSocket implements Runnable{
JSocket(int port)throws SocketException{
super(port);
}
JSocket()throws SocketException{}
public void sendMsg(String content,String ip){
byte[] buf = content.getBytes();
try{
DatagramPacket pack = new DatagramPacket(buf,buf.length,InetAddress.getByName(ip),10086);
send(pack);
}catch (Exception e){
record.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())+
content+" 发送失败\r\n\r\n");
}finally{
close();
}
}
public void run(){
DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
try{
while(true){
receive(dp);
record.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())+
dp.getAddress().getHostAddress()+"对我说:"+new String(dp.getData(),0,dp.getLength())+"\r\n\r\n");
}
}catch (Exception e){
record.append("接收包失败\r\n\r\n");
}
}
}
}