简体   繁体   English

Java Swing JFrame 在继续之前渲染

[英]Java Swing JFrame to be rendered before moving on

I want to build a "Work in progress..."-JFrame before calling a method that might take some time but the JFrame won't be fully rendered before the method is executed.我想在调用可能需要一些时间但 JFrame 在执行该方法之前不会完全呈现的方法之前构建一个“正在进行的工作......”-JFrame。 I know I'm not the first with that problem, I always find solutions involving threads (do the long working method in a thread and so on).我知道我不是第一个遇到这个问题的人,我总是找到涉及线程的解决方案(在线程中执行长工作方法等等)。 But I don't want to for particular reasons (and it might be that I'm not even able to unless I want the system to go boom).但出于特殊原因我不想这样做(可能是我什至无法做到,除非我想让系统达到 go 繁荣)。

So, is there a possibility to tell Swing to first render a window and then do the rest?那么,是否有可能告诉 Swing 首先渲染 window 然后执行 rest? Something like get rendering the window to be the first in the excecute-queue?比如让 window 成为执行队列中的第一个?

You might be able to do it by displaying the JFrame and then calling您可以通过显示 JFrame 然后调用

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        myLongMethod();
    }
});

But it's really a bad idea.但这确实是个坏主意。 The GUI will be frozen for the whole duration of the method. GUI 将在该方法的整个持续时间内被冻结。

The right way to do it is, indeed, to use a background thread.确实,正确的方法是使用后台线程。 But Swing has all you need to make it painless.但是 Swing 拥有一切你需要让它无痛。 Simply use a SwingWorker .只需使用SwingWorker Its API doc is well-written and has examples.它的 API 文档写得很好并且有例子。 Try doing it, and if you can't, Ask another question and show the code you have tried so that someone helps you.尝试这样做,如果做不到,请提出另一个问题并显示您尝试过的代码,以便有人帮助您。

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

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