我有一个应用程序,一开始显示 Jframe,用户输入需要分析的网页的 URL,然后用户单击“确定”。
当用户单击“确定”时,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 ...
关于java - JFrame 不绘制内容,仅在 ActionListener 内可见时显示白色矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9671748/