-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient_GUI.java
More file actions
141 lines (124 loc) · 4.17 KB
/
Client_GUI.java
File metadata and controls
141 lines (124 loc) · 4.17 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
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
package GUI_Socket;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Creat_name extends JFrame {
private String name;
Creat_name() {
setTitle("登录");
JPanel panel = new JPanel();
panel.setLayout(null);
Font f=new Font("宋体",Font.BOLD,17);
Font f1=new Font("黑体",Font.PLAIN,15);
JLabel ps=new JLabel("用户名 :");
JLabel ps1=new JLabel("聊天室用户注册");
ps1.setBounds(140, 20, 200, 30);
ps1.setFont(f);
ps1.setForeground(new Color(51, 125, 48));
ps.setBounds(92, 80, 200, 30);
ps.setFont(f1);
panel.add(ps);
panel.add(ps1);
JButton con,exit;
con=new JButton("确认");
exit=new JButton("退出");
con.setBounds(110,160,60,25);
con.setFocusable(false);
exit.setBounds(230,160,60,25);
exit.setFocusable(false);
panel.add(con);
panel.add(exit);
JTextField username=new JTextField();
username.setBounds(170, 80, 120, 26);
username.setFont(f1);
panel.add(username);
exit.addActionListener(e -> {
System.exit(0);
});
// 继续按钮的 Action
con.addActionListener(e->{
name=username.getText();
if(name.isEmpty()){
JOptionPane.showMessageDialog(null,"用户名不能为空,请重新输入","提示",JOptionPane.ERROR_MESSAGE);
}
else{
Client.c_name=name;
new Client_GUI();
dispose();
}
});
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(700, 200, 400, 250);
setVisible(true);
}
public static void main(String[] args) {
new Creat_name1();
}
}
class Client_GUI extends JFrame {
private String message;
Client client=new Client();
Client_GUI() {
setTitle("用户界面");
JPanel panel = new JPanel();
panel.setLayout(null);
Font f=new Font("宋体",Font.BOLD,20);
JLabel ps=new JLabel("聊天窗口");
ps.setBounds(175, 20, 200, 30);
ps.setFont(f);
ps.setForeground(new Color(173, 75, 199));
panel.add(ps);
Font f1=new Font("宋体",Font.PLAIN,15);
JTextPane text=new JTextPane();
text.setBounds(80,50,300,140);
text.setFont(f1);
panel.add(text);
JButton send, exit;
send=new JButton("发送");
exit =new JButton("退出");
send.setBounds(130,210,60,25);
exit.setBounds(270,210,60,25);
send.setFocusable(false);
exit.setFocusable(false);
panel.add(send);
panel.add(exit);
exit.addActionListener(e->{
client.out.println("exit");
try {
client.socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
System.exit(0);
});
client.out.println(Client.c_name);
send.addActionListener(e->{
message=text.getText();
if(message.isEmpty()){
JOptionPane.showMessageDialog(null,"消息不能为空","提示",JOptionPane.ERROR_MESSAGE);
}
else {
Client.c_message =message;
text.setText("");
String out_time;
Date data=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
out_time=formatter.format(data);
client.out.println(Client.c_message);client.out.println(out_time);
try{
String response = client.in.readLine();
JOptionPane.showMessageDialog(null,"服务器相应:"+response,"提示",JOptionPane.INFORMATION_MESSAGE);
}catch (IOException ex){
ex.printStackTrace();
}
}
});
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(770, 200, 450, 300);
setVisible(true);
}
}