-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient.java
205 lines (181 loc) · 5.96 KB
/
Client.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.DropMode;
public class Client extends JFrame {
private final String IP="192.168.1.107";//"10.20.179.1";//"10.40.140.14";////"10.20.160.19";////"172.30.42.6";
private final int PORT=12000;
private JPanel contentPane;
private JTextField account;
// private JTextField password;
JPasswordField password;
private ButtonGroup bg=new ButtonGroup();
private Socket socket;
private PrintWriter pw;
private BufferedReader br;
private ClientChargeThread cct;
private ClientDoctorThread cdt;
private ClientStoreThread cst;
private ClientAdminThread cat;
private ClientPresidentThread cpt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Client frame = new Client();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Client() {
try {
socket=new Socket(IP,PORT);
pw= new PrintWriter(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
//与服务器连接失败
e.printStackTrace();
}
this.setTitle("登录客户端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 200, 650, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
account = new JTextField();
account.setBounds(290, 171, 104, 21);
contentPane.add(account);
account.setColumns(10);
JLabel label = new JLabel("\u8D26\u53F7\uFF1A");
label.setBounds(232, 174, 48, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("\u5BC6\u7801\uFF1A");
label_1.setBounds(232, 202, 48, 15);
contentPane.add(label_1);
password = new JPasswordField();
//password = new JTextField();
password.setBounds(290, 202, 104, 21);
password.setColumns(10);
contentPane.add(password);
JRadioButton doctor = new JRadioButton("\u533B\u751F");
doctor.setBounds(186, 242, 66, 23);
contentPane.add(doctor);
JRadioButton charge = new JRadioButton("\u6536\u8D39\u4EBA\u5458",true);
charge.setBounds(282, 242, 104, 23);
contentPane.add(charge);
JRadioButton store = new JRadioButton("\u836F\u5E08");
store.setBounds(398, 242, 88, 23);
contentPane.add(store);
JRadioButton admin = new JRadioButton("\u7BA1\u7406\u5458");
admin.setBounds(282, 267, 90, 23);
contentPane.add(admin);
JRadioButton president = new JRadioButton("\u9662\u957F");
president.setBounds(186, 267, 79, 23);
contentPane.add(president);
bg.add(doctor);
bg.add(store);
bg.add(president);
bg.add(charge);
bg.add(admin);
JButton button = new JButton("\u767B\u5F55");
button.setBounds(325, 313, 93, 23);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(account.getText().equals("")||password.getText().equals("")){
JOptionPane.showMessageDialog(contentPane,"请输入账号密码");
return ;
}
if(charge.isSelected()){
login("charge");
}
else if(doctor.isSelected()){
login("doctor");
}
else if(store.isSelected()){
login("store");
}
else if(admin.isSelected()){
login("admin");
}
else if(president.isSelected()){
login("president");
}
}
});
contentPane.add(button);
JLabel lblNewLabel = new JLabel("\u533B\u9662\u7CFB\u7EDF\u767B\u5F55\u754C\u9762");
lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setBounds(165, 32, 321, 82);
contentPane.add(lblNewLabel);
JButton btnNewButton = new JButton("\u9000\u51FA");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnNewButton.setBounds(196, 313, 93, 23);
contentPane.add(btnNewButton);
}
public void login(String type){
String acc=account.getText();
String pas=password.getText();
pw.println(type+","+acc+","+pas);
System.out.println(type+","+acc+","+pas);
pw.flush();
try {
String t=br.readLine();
System.out.println(t);
String[] sign=t.split(",");
System.out.println("sign:"+sign[0]);
if(sign[0].equals("OK")){
Point p=this.getLocation();
setVisible(false);
switch(type){
case "charge":new Charge(p).setVisible(true);cct=new ClientChargeThread(socket);cct.start();break;
case "doctor":new Doctor(sign[1],sign[2],p).setVisible(true);cdt=new ClientDoctorThread(socket);cdt.start();break;
case "store":new Store(p).setVisible(true);cst =new ClientStoreThread(socket);cst.start();break;
case "admin":new Admin(p).setVisible(true);cat =new ClientAdminThread(socket);cat.start();break;
case "president":new Charge(p).setVisible(true);cpt =new ClientPresidentThread(socket);cpt.start();break;
default :break;
}
}
else{
if(account.getText().equals("")||password.getText().equals("")){
JOptionPane.showMessageDialog(contentPane,"账号或密码错误");
return ;
}
}
} catch (IOException e) {
//连接失败
e.printStackTrace();
}
}
}