-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectUI.java
More file actions
190 lines (152 loc) · 5.64 KB
/
Copy pathConnectUI.java
File metadata and controls
190 lines (152 loc) · 5.64 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
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
package net;
import ui.UI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.BindException;
import java.net.InetAddress;
public class ConnectUI extends JPanel {
JFrame frame = null;
JPanel panel = null;
String button1Text = "创建房间";
JButton button1;
public ConnectUI() throws Exception {
initFrame();
initUI();
frame.setVisible(true);
}
public synchronized void initFrame() {
frame = new JFrame("");
frame.setSize(300, 500);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setLocationRelativeTo(UI.frame);
frame.setResizable(false);
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
frame.setVisible(false);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
}
public synchronized void initUI() throws Exception {
panel = new JPanel();
panel.setLayout(new GridLayout(10, 2));
JTextField fieldIP = new JTextField();
JTextField fieldPort = new JTextField("请输入端口号");
fieldIP.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
fieldIP.setText("");
}
@Override
public void focusLost(FocusEvent e) {
}
});
fieldPort.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
fieldPort.setText("");
}
@Override
public void focusLost(FocusEvent e) {
}
});
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton("创建房间");
JRadioButton jRadioButton2 = new JRadioButton("加入房间");
panel.add(jRadioButton1);
panel.add(jRadioButton2);
JLabel jLabel = new JLabel("本机IP:" + InetAddress.getLocalHost());
panel.add(jLabel);
panel.add(fieldIP);
panel.add(fieldPort);
jLabel.setVisible(true);
fieldIP.setVisible(false);
fieldPort.setVisible(true);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
jRadioButton1.setSelected(true);
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "创建房间":
jLabel.setVisible(true);
fieldIP.setVisible(false);
fieldPort.setVisible(true);
fieldIP.setText(null);
fieldPort.setText("请输入端口号");
if (button1 != null) {
button1.setText("创建房间");
}
break;
case "加入房间":
jLabel.setVisible(false);
fieldIP.setVisible(true);
fieldPort.setVisible(true);
fieldIP.setText("请输入IP");
fieldPort.setText("请输入端口号");
if (button1 != null) {
button1.setText("加入房间");
}
break;
}
}
}
ButtonListener b = new ButtonListener();
jRadioButton1.addActionListener(b);
jRadioButton2.addActionListener(b);
button1 = new JButton(button1Text);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (jRadioButton1.isSelected()){
fieldIP.setText("");
if (fieldPort.getText().equals("")||fieldPort.getText().equals("请输入端口号"))
{
new Server(null, frame);
return;
}
int port = Integer.parseInt(fieldPort.getText());
new Server(port, frame);
} else if (jRadioButton2.isSelected()){
String ip = fieldIP.getText();
if (fieldPort.getText().equals("")||fieldPort.getText().equals("请输入端口号"))
{
new Client(ip, null, frame);
return;
}
int port = Integer.parseInt(fieldPort.getText());
new Client(ip, port, frame);
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(UI.frame, "端口不合法");
} catch (IOException ex) {
JOptionPane.showMessageDialog(UI.frame, "该端口已被占用!");
}
}
});
panel.add(button1);
frame.add(panel);
}
}