[英]Blank Java Swing Frame
I'm a student who is trying to learn more about Java development and is currently creating a simple client/server IM application using swing. 我是一名学生,正在尝试更多地了解Java开发,目前正在使用swing创建一个简单的客户端/服务器IM应用程序。
Let me try to explain the issue I've got: 让我试着解释一下我得到的问题:
The class Login runs at start-up, and after a successful authentication with the server (which works fine) the class ClientChat gets instantiated. Login类在启动时运行,在成功通过服务器验证后(工作正常),ClientChat类被实例化。
ClientChat creates a new JFrame, a new connection is made with the server and new I/O streams are created. ClientChat创建一个新的JFrame,与服务器建立新连接并创建新的I / O流。 All this background stuff works fine, but the new frame is completely blank.
所有这些背景的东西工作正常,但新框架是完全空白的。
If I run the application without the Login class (ie by running ClientChat directly), this issue is not present. 如果我在没有Login类的情况下运行应用程序(即直接运行ClientChat),则不存在此问题。
Unsuccessful approaches so far: 到目前为止不成功的方法:
From Login(): 从登录():
panel.add(userLabel);
panel.add(passLabel);
panel.add(userEntry);
panel.add(passEntry);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Chat Room Authentication");
frame.setSize(300, 120);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
...
frame.setVisible(false);
ClientChat clientChat = new ClientChat(serverIP);
clientChat.start();
From ClientChat(): 来自ClientChat():
userText = new JTextField();
chatWindow = new JTextArea();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Logged in as "+user.toUpperCase());
frame.add(userText, BorderLayout.SOUTH);
frame.add(new JScrollPane(chatWindow), BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
You've got a classic Swing threading issue where you're running long-running code on the Swing's event thread also known as the E vent
D ispatch
T hread or EDT , and by doing this, you're preventing the EDT from doing its necessary tasks of drawing the GUI and interacting with the user.
你有一个经典的Swing线程问题,你在Swing的事件线程上运行长时间运行的代码,也称为E vent
D ispatch
T hread或EDT ,这样做会阻止EDT执行它绘制GUI并与用户交互的必要任务。
The solution: use a SwingWorker to help you create a background thread that will allow long process to not interfere with the Swing GUI and also to allow it to communicate well with the GUI. 解决方案:使用SwingWorker帮助您创建一个后台线程,该线程允许长进程不干扰Swing GUI并允许它与GUI良好通信。
For more on this, please check out: Lesson: Concurrency in Swing . 有关这方面的更多信息,请查看: 课程:Swing中的并发性 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.