JFrame 中 java CardLayout 的奇怪问题
你好,我有一个带有 CardLayout 和 3 张卡片的 Jframe。 我在第一张卡的按钮上有一个 ActionListener。
这段代码运行良好:
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, "wait");
}
}
问题是当我添加代码在服务器上进行登录时(我正在开发一个 xmpp 客户端):
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, "wait");
xmppManager = new Xmpp("jabberserver", 5222);
try {
xmppManager.init();
} catch (XMPPException e) {
e.printStackTrace();
}
cl.show(cards, "userList");
}
}
基本上,当用户按下登录按钮时,我需要显示“请等待”卡,登录,然后出示另一张卡。但在这种情况下,“等待”卡不会显示,它会进行登录(大约需要 5 秒)并直接显示最终卡“userList”。
我缺少什么?
hello i have a Jframe with a CardLayout and 3 cards.
I have an ActionListener on a button on the first card.
This code is working well:
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, "wait");
}
}
the problem is when i add code to do the login on the server (i'm developing an xmpp client):
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, "wait");
xmppManager = new Xmpp("jabberserver", 5222);
try {
xmppManager.init();
} catch (XMPPException e) {
e.printStackTrace();
}
cl.show(cards, "userList");
}
}
Basically, i need to show a "please wait" card when the user press the login button, do the login, and then show another card. But in this case, the "wait" card doesn't show, it do the login (it take around 5 seconds) and it show directly the final card "userList".
What i'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有代码都在事件调度线程上执行,这会阻止 GUI 重新绘制自身。您需要对服务器的调用在单独的线程中执行,这样就不会阻塞 EDT。
请阅读 Swing 教程中有关 并发 的部分,了解更多信息建议的解决方案。
All the code is executing on the Event Dispatch Thread which is preventing the GUI from repainting itself. You need the call to the server to execute in a separate Thread so you don't block the EDT.
Read the section from the Swing tutorial on Concurrency for more information and a suggested solution.
也许在显示“请稍候”后需要触发屏幕重绘?它可能不会自动触发。
Perhaps it is necessary to trigger a repaint of the screen after you show the please wait? It might not be triggered automatically.