JFrame 中 java CardLayout 的奇怪问题

发布于 2024-10-18 01:34:22 字数 1002 浏览 7 评论 0原文

你好,我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-10-25 01:34:22

所有代码都在事件调度线程上执行,这会阻止 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.

泪痕残 2024-10-25 01:34:22

也许在显示“请稍候”后需要触发屏幕重绘?它可能不会自动触发。

Perhaps it is necessary to trigger a repaint of the screen after you show the please wait? It might not be triggered automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文