简体   繁体   中英

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(); 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. I'm guessing that the packetListner.listener() method blocks in which case the GUI will freeze. You should not be blocking the EDT.

Read the section from the Swing tutorial on Concurrency for a full description of this problem and a solution.

I think the packetListener.listener(); method performs some complex operation which blocks your 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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