它是一个交通灯计划。 GUI是一个服务器。在GUI上,我使用一个按钮启动服务器,但我的gui因为等待客户而冻结。这些人只是回忆。我知道我应该使用线程,但我不知道如何。问题是GUI的冻结。请帮助我
public class Controller {
public TextArea meldingPlass;
private static int portNr = 5555;
public void startServer() throws IOException {
Server cs = new Server();
cs.connectToServer(portNr);
}
}
//这是我的SeverThreads类
公共类ServerThread扩展了线程{
Socket s;
InetAddress ca;
public ServerThread(Socket s)
{
this.s=s;
ca=s.getInetAddress();
}
public void run()
{
try(
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
//BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
)
{
while (true){
System.out.println(ca.getHostAddress());
out.println("Status of Server");
}
// s.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
//这是ServerClass public class Server实现Runnable {
public void connectToServer(int portNr){
try(
ServerSocket ss = new ServerSocket(portNr);
)
{
while (true)
{
System.out.println("In Server - while loop");
ServerThread st = new ServerThread(ss.accept());
System.out.println("Client connected. Starting client");
st.start();
}
}
catch (Exception e){
System.out.println("Exception occurred when trying to listen on port "
+portNr+ " or listening for a connection");
System.out.println(e.getMessage());
}
}
@Override
public void run() {
}
}