JFrame不绘制内容,只显示在ActionListener内部可见的白色矩形



我有一个应用程序,在开始显示Jframe时,用户输入必须分析的网页的URL,然后用户单击OK。

当用户单击"确定"时,actionListener应执行以下操作:它应该显示一个新的Jframe,上面写着"请等待,分析网站,这可能需要一点时间"那么它应该启动analyzePage()方法。

问题是,用户单击"确定"按钮后,会显示新的Jframe,但它是空白的,里面只有白色矩形。只有在analyzePage()方法完成后(即数秒后)才会显示Jframe的内容。

    static class OKListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        new WaitFrame();
// mf is main frame (the first frame where url was entered and where OK was clicked)
        mf.setEnabled(false);
        address = mf.getURLtext();
        analyzePage();
    }
}
public class WaitFrame extends JFrame {
public WaitFrame() {
    super();
    JFrame wait = this;
    JLabel jl = new JLabel("Čakajte prosím, analyzujem stránku, môže to chvíľu trvať.");
    JPanel jp = new JPanel();
    jp.add(jl);
    wait.getContentPane().add(jp);
    wait.pack();
    wait.setVisible(true);
}
}

在AWT线程中运行JFrame:

//...
java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        JFrame waitFrame = new JFrame();
        //...
        waitFrame.pack();
        waitFrame.setVisible(true);
    }
});
//...
// Other things to do ...
// dispose Frame after all, if neccessry ...

最新更新