while 循环忽略事件监听器

发布于 2024-08-13 19:17:33 字数 1370 浏览 9 评论 0原文

因此,当我运行此代码尝试更改背景时,GUI 崩溃并陷入无限 while 循环,忽略事件侦听器。这是代码:

private Panel getPanel1() {
        if (panel1 == null) {
            panel1 = new Panel();
            panel1.setLayout(new GridBagLayout());
            while(frame.isVisible()){
                panel1.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent e) {
                        frame.setVisible(false);
                    }
                });
                int r = (int) (Math.random()*255);
                int g = (int) (Math.random()*255);
                int b = (int) (Math.random()*255);
                Color c = new Color(r, g, b);
                panel1.setBackground(c);
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                panel1.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent e) {
                        /*panel1.setVisible(false);
                        frame.setVisible(false);*/
                        System.exit(0);
                    }
                });
            }
        }
        return panel1;
}

它没有退出终止程序或事件更改背景的循环,而是只显示面板而不执行其他操作,我必须强制它退出。我应该怎么办?

so when i run this code to try to change the background the GUI crashes and gets stuck in a infinite while loop ignoring the event listeners. here is the code:

private Panel getPanel1() {
        if (panel1 == null) {
            panel1 = new Panel();
            panel1.setLayout(new GridBagLayout());
            while(frame.isVisible()){
                panel1.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent e) {
                        frame.setVisible(false);
                    }
                });
                int r = (int) (Math.random()*255);
                int g = (int) (Math.random()*255);
                int b = (int) (Math.random()*255);
                Color c = new Color(r, g, b);
                panel1.setBackground(c);
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                panel1.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent e) {
                        /*panel1.setVisible(false);
                        frame.setVisible(false);*/
                        System.exit(0);
                    }
                });
            }
        }
        return panel1;
}

instead of exiting the loop of terminating the program or event changing the background it just displays the panel and does nothing else and i have to force it to quit. what should i do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

对你而言 2024-08-20 19:17:33

通过在循环中调用 sleep,您可以有效地阻止 UI 线程。在该循环中,您还在每次迭代中添加两个侦听器,这非常奇怪。

不要阻塞 UI 线程。让 GUI 框架负责传递事件等。基本上,您需要对 UI 采用基于事件的方法,而不是您当前正在采用的方法,这种方法永远不会让任何事件被发送(如你永远不会将控制权返回给调用者)。

创建面板,添加适当的事件侦听器,然后将其返回给调用者。如果您想每 4 秒更改一次背景颜色,您应该通过一个计时器,这样它就不会阻塞等待 4 秒过去的 UI 线程。

You're effectively blocking the UI thread by calling sleep in a loop. In that loop you're also adding two listeners on every iteration too, which is quite bizarre.

Don't block the UI thread. Let the GUI framework take care of delivering events etc. Basically you need to take an event-based approach to UI, rather than the approach you currently are taking, which will never let any events get despatched (as you're never returning control to the caller).

Create the panel, add the appropriate event listener, and then just return it to the caller. If you want to change the background colour every 4 seconds, you should do that via a timer so that it's not blocking the UI thread waiting for the 4 seconds to elapse.

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