简体   繁体   English

框架显示黑屏

[英]frame shows black screen

btnnew.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                System.out.println("Hello");
                packetListener.listener();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

I get a black screen when it runs. 运行时出现黑屏。 But when the packetListener.listener(); 但是当packetListener.listener(); calls in the constructor it shows. 在显示的构造函数中调用。

Can you please explain why this is happening? 您能解释一下为什么会这样吗?

Code that is executed from a listener executes on the EDT. 从侦听器执行的代码在EDT上执行。 I'm guessing that the packetListner.listener() method blocks in which case the GUI will freeze. 我猜想packetListner.listener()方法会阻塞,在这种情况下,GUI将冻结。 You should not be blocking the EDT. 您不应该阻止EDT。

Read the section from the Swing tutorial on Concurrency for a full description of this problem and a solution. 阅读Swing 并发教程中有关此问题和解决方案的完整说明部分。

I think the packetListener.listener(); 我认为packetListener.listener(); method performs some complex operation which blocks your UI. 方法执行一些复杂的操作,这会阻塞您的UI。

Better create a thread for listening the packet. 更好地创建一个线程来监听数据包。 ie,use it like this 即,像这样使用

 try {
    System.out.println("Hello");
    new Thread(new Runnable() {
        public void run() {
packetListener.listener();
            }
    }).start();         

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

Hope this helps you 希望这对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM