2012-03-12 57 views
0

我有一个应用程序,在显示Jframe的开始处,用户输入必须分析的网页的URL,然后用户单击确定。JFrame不绘制内容,只显示白色矩形,当在ActionListener中可见时

当用户点击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); 
} 
} 

回答

1

运行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 ... 
+0

没有帮助,我认为这个问题是该函数analyzePage()从ActionListener的的方法的actionPerformed(ActionEvent的五) – Marcel 2012-03-12 18:55:48

+0

行内称为最后解决方法是将analyzePage()放入invokeLater方法中,而不是Jframe。谢谢! – Marcel 2012-03-12 19:16:28