客户机 - 服务器Java GUI:读/写导致程序冻结 [英] Client-Server Java GUI: read/write causing program to freeze

查看:132
本文介绍了客户机 - 服务器Java GUI:读/写导致程序冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Java的客户端/服务器程序(包括一个GUI)。
我在客户端有以下代码:

I'm doing a client/server program in Java (including a GUI). I've got the following code in the client:

public class SBListener implements ActionListener{
  public void actionPerformed(ActionEvent e){
     try{
        outToServer.writeUTF(usn.getText().trim());
        System.out.println("sent username to server");
        playerExists = inToClient.readBoolean();
        System.out.println("past getting player");
        System.out.println("player exists = " + playerExists);
     }catch(IOException a){
        System.err.println(a);
     }
     if(playerExists == false){
        JButton submitInfo = new JButton("submit info");
        submitInfo.addActionListener(new SBNewInfoListener());
        init.add(new JLabel(""));//dummy element to get the right alignment
        init.add(new JLabel("First Name:"));
        init.add(fn);
        init.add(new JLabel("Last Name:"));
        init.add(ln);
        init.add(submitInfo);
        add(init, BorderLayout.WEST);
        init.setVisible(true);
        init.revalidate();
        init.repaint();
     }

  }

}

并且在服务器中有以下代码:

And the following code in the Server:

String username = inp.readUTF();
System.out.println(username);
out.writeBoolean(false);

System.out.println("wrote boolean, waiting for fn/ln/un");
fn = inp.readUTF();
System.out.println("got fn");
ln = inp.readUTF();
un = inp.readUTF();

但是当点击调用SBListener的按钮时,程序冻结,服务器正在等待fn / ln / username。我添加了一堆system.out语句进行调试,我得到了一个说写布尔,等待fn / ln / un。

But when the button that calls SBListener is clicked, the program freezes when it gets to the point where the Server is waiting for fn/ln/username. I added a bunch of system.out statements for debugging and I get up to the one that says "wrote boolean, waiting for fn/ln/un".

基本上,我试图更新屏幕后服务器返回false值。具体来说,我想添加两个文本字段的第一&姓。然后我要将这些值发送到服务器。

Basically, I'm trying to update the screen after the Server returns a false value. Specifically, I want to add two text fields for first & last name. I then want to send those values to the server.

任何人都可以告诉我如何解决这个问题?感谢您提供任何帮助!

Can anyone tell me how to fix this? Thanks in advance for any help!

推荐答案

不要在ActionListener中执行客户端/服务器代码。这将导致事件分派线程在等待来自服务器的响应时阻塞。当EDT被阻止时,整个GUI冻结。

Don't execute client/server code in an ActionListener. This will cause the Event Dispatch Thread to block while waiting for a response from the server. When EDT is blocked the whole GUI freezes.

阅读Swing教程中并发以获取更多信息。您需要这样为客户端/服务器代码使用单独的线程。或者您可以使用本教程中讨论的 SwingWorker

Read the section from the Swing tutorial on Concurrency for more information. You need so use a separate Thread for the client/server code. Or you can use a SwingWorker as discussed in the tutorial.

这篇关于客户机 - 服务器Java GUI:读/写导致程序冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆