[英]Display frame for certain amount of Time
I Have 3 classes, the main.class, login.class and then a splash-screen.class 我有3个类,分别是main.class,login.class和splash-screen.class
my splash-screen doesn't have a main in it, it only runs when the main.class runs, problem is I want my splash screen to appear only for 3 seconds, then it must disappear and show the login .class (which just has controls to log in) 我的启动画面没有主程序,它仅在main.class运行时运行,问题是我希望启动画面仅出现3秒钟,然后它必须消失并显示登录.class(仅具有登录控件)
How do I do that? 我怎么做?
At the moment when I run the main, both the splashscreen.class and login.class apear 在我运行主程序的那一刻,splashscreen.class和login.class都出现了
Use a Swing Timer : 使用Swing 计时器 :
setRepeats(false)
on this timer. setRepeats(false)
。 Its ActionListener should hide the splash frame, and show the Login frame Code shown below might work for you: 下面显示的代码可能适用于您:
public static void showSplash(int duration) {
SplashScreen splash1 = SplashScreen.getSplashScreen();
if(splash1==null){
File file1=new File("splash.jpg");
String imgfile1=file1.getAbsolutePath();
JWindow splash = new JWindow();
JPanel content = (JPanel)splash.getContentPane();
int width = 655;
int height = 442;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height-height)/2;
splash.setBounds(x,y,width,height);
JLabel label = new JLabel(new ImageIcon(imgfile1));
content.add(label, BorderLayout.CENTER);
splash.setVisible(true);
// Wait a little while, maybe while loading resources
try
{
Thread.sleep(duration);
} catch (Exception e) {}
splash.setVisible(false);
}
}
You can call this method at the first line of your main()
method. 您可以在
main()
方法的第一行调用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.