简体   繁体   English

如何预加载Swing组件

[英]How to preload Swing components

I know this is sort of a vague question, but I will try to make it as clear as possible. 我知道这是一个模糊的问题,但我会尽力使之尽可能清楚。 When my Java app launches for the first time, it does some checking to see if files and directories exist and checks for an internet connection. 当我的Java应用程序首次启动时,它会进行一些检查以查看文件和目录是否存在,并检查Internet连接。

The app then allows the user to move on to the dashboard, which requires loading and adding many Swing components to the Background Panel. 然后,该应用程序允许用户继续前进至仪表板,这需要加载许多“ Swing”组件并将其添加到“背景面板”中。 This takes time. 这需要时间。

I was wondering how I could make it so that during the loading process at the start, the app loads all of the Swing components Images etc. so that they appear instantly when the user executes the command to do so. 我想知道如何做到这一点,以便在开始加载过程中,该应用程序加载所有Swing组件图像等,以便在用户执行命令时立即显示它们。

I can load all of the components like this: 我可以像这样加载所有组件:

JButton = new JButton("blah"); 

but I'm not sure that's enough to make the components appear instantly, wouldn't adding several image filled Swing components at the same time still lag the UI thread, even if it was already "loaded" as seen above? 但是我不确定是否足以使这些组件立即显示,即使同时已经如上所示加载了多个图像填充的Swing组件,也不会同时滞后于UI线程吗?

Thanks! 谢谢!

on the components use 关于组件使用

setVisible(false)

for example 例如

public class myParentPanel extends JPanel{
     public myParentPanel{

     //add all the child components
     //Do what ever takes along time
     setVisible(false); //this then makes in invisible but still 
                        //allows all the set up code to run
     }

     public void showParent(){
         setVisible(true);
         invalidate();
     }
}

and then create a method to make them visible when required. 然后创建一种方法,使它们在需要时可见。 Hence all your setting in the constructors ca be called then when you call your method say: 因此,可以调用构造函数中的所有设置,然后在调用方法时说:

drawWindow()

it then only has to call setVisible(true) and invalidate the screen to call their painting methods :). 然后只需调用setVisible(true)并使屏幕无效即可调用其绘画方法:)。

An advancement on this would be to * run your setups ie your checking and loading the panels methods on a separate threads *so you don't have to wait for a sequential loading. 在此方面的一项进步将是* 运行您的设置,即在单独的线程上检查和加载panel方法,因此您不必等待顺序加载。 So on your loading you may to use an anonymous class 因此,在加载时,您可以使用匿名类

 SwingUtilities.invokeLater(new Runnable(){ public void run(){

     //do my display set up

}});

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

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