3
我正在创建多人游戏,但是当创建ServerSocket时,我无法关闭在该应用程序中运行的任何JFrame,这里是我的服务器创建代码:在创建ServerSocket(或套接字)后,JFrame无法关闭
private void createServer() {
JFrame frame = new JFrame("Cow Invaders - Server");
JTextArea console = new JTextArea();
frame.setSize(640, 480);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
console.setFont(new Font("Times New Roman", Font.BOLD, 15));
console.setLineWrap(true);
frame.add(console);
frame.setVisible(true);
try {
server = new ServerSocket(Integer.parseInt(port));
System.out.println("No problems here");
} catch (IOException e) {
System.err.println("An error has occurred: " + e.getMessage());
e.printStackTrace();
}
console.append("Started server on port " + port + " and with an IP of " + server.getInetAddress());
try {
socket = server.accept();
} catch (IOException e) {
e.printStackTrace();
System.err.println("An error has occurred: " + e.getMessage());
}
console.append("\n" + "Connection from: " + socket.getInetAddress());
try {
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.err.println("An error has occurred: " + e.getMessage());
e.printStackTrace();
}
try {
out.writeUTF("You have successfully joined the game.");
} catch (IOException e) {
System.err.println("An error has occurred" + e.getMessage());
e.printStackTrace();
}
System.out.println("Successfully send data.");
}
关于是什么原因造成的?