我制作了简单的客户端服务器聊天程序,支持多个客户端,但我有一个问题:`
public class Main extends Application {
public Button send;
public TextArea textarea;
public TextArea onlineuser;
public TextField usertext;
public static int maxUser = 100;
public boolean run=true;
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("server.fxml"));
primaryStage.setTitle("Server control ");
primaryStage.setScene(new Scene(root, 749, 494));
primaryStage.show();
}
public static void main(String[] args) throws IOException{
Server sv=new Server(1111);
launch(args);
}
class Server extends Thread {
private ServerSocket sv;
private ThreadServer[] clientconnect = new ThreadServer[maxUser];
public Server(int port) throws IOException {
this.sv = new ServerSocket(port);
start();
}
public void run() {
while (run) {
try {
Socket connect = this.sv.accept();
connectionThread(connect);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void connectionThread(Socket connection) {
for (int a = 0; a < maxUser; a++) {
if (this.clientconnect[a] == null) {
this.clientconnect[a] = new ThreadServer(connection, a);
break;
}
}
}
}
class ThreadServer extends Thread {
final private int userID;
final private Socket threadconnect;
public void run() {
while (run)) {
try {
PrintWriter out = new PrintWriter(threadconnect.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(threadconnect.getInputStream()));
textarea.appendText(in.readLine());
out.println(usertext.getText());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public ThreadServer(Socket connect, int ID) {
this.userID = ID;
this.threadconnect = connect;
}
}
我的客户端部分工作正常,但我不能在main中调用我的服务器类,因为它不是静态但我应该使用main也启动javafx gui部分我该如何解决这个问题?