简体   繁体   English

Java GUI 冻结

[英]Java GUI Freezes

I am trying to create a Simple TCP Client Server Application Interface where user can start or stop the server when they pres the respective buttons I have created a StartServer Button when user presses the button it should connected to the server the problem am facing is when the user clicks the button noting happens and the interface halts我正在尝试创建一个简单的 TCP 客户端服务器应用程序接口,当用户按下相应的按钮时,用户可以在其中启动或停止服务器我创建了一个 StartServer 按钮,当用户按下它应该连接到服务器的按钮时,我面临的问题是用户单击按钮注意到发生并且界面停止

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    DefaultListModel dlm=new DefaultListModel();
    String clientSentence; 
    String capitalizedSentence;
    try {
        welcomeSocket= new ServerSocket(6789);
        dlm.addElement("server started..");            
        dlm.addElement("Server Waiting for Connections on Port 6789");
        jList1.setModel(dlm); 
        displayfull();
         while(true)
        {                
        DataOutputStream outToClient = null;
        try {


            Socket connectionSocket = welcomeSocket.accept();
            //dlm.addElement("Client Connected ");
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            //dlm.addElement("Obtained a handle on Client Input Stream");
            outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            //dlm.addElement("Obtained a handle on Client Output Stream");
            clientSentence = inFromClient.readLine();
            //dlm.addElement("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
            //dlm.addElement("Message Sent");
        } catch (Exception e) {

        } 

    }               

    } catch (Exception e) {
        dlm.addElement(e);
    }
   jList1.setModel(dlm); 
    displayfull();
}   

在此处输入图片说明

As others have hinted at, you are spending precious time on the Event Dispatch Thread .正如其他人所暗示的那样,您正在事件调度线程上花费宝贵的时间。
If you read through the tutorial on how to do Concurrency in Swing you will discover you are given tools like the Swing worker .如果您通读了有关如何在 Swing 中实现并发的教程,您会发现您获得了像Swing worker这样的工具。

There are many questions like this on Stack Overflow, some more helpful than others. Stack Overflow 上有很多这样的问题,有些问题比其他问题更有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM