睡眠功能无法正常工作

时间:2014-02-21 20:53:53

标签: function netbeans sleep

我正在使用下面的代码来制作启动画面

package Splashscreentest;

 /*
  * SplashDemo.java
  *
  */

import java.awt.*;
import java.awt.event.*;

public class Splashscreentest extends Frame implements ActionListener {
static void renderSplashFrame(Graphics2D g, int frame) {
    final String[] comps = {"foo", "bar", "baz"};
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(300,140,400,400);
    g.setPaintMode();
    g.setColor(Color.BLACK);
    g.drawString("Loading "+comps[(frame/5)%3]+"...", 120, 150);
}
public Splashscreentest() {
    super("SplashScreen demo");
    setSize(3000, 2000);
    setLayout(new BorderLayout());
    Menu m1 = new Menu("File");
    MenuItem mi1 = new MenuItem("Exit");
    m1.add(mi1);
    mi1.addActionListener(this);
    this.addWindowListener(closeWindow);

    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    mb.add(m1);
    final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
        System.out.println("SplashScreen.getSplashScreen() returned null");
        return;
    }
    Graphics2D g = splash.createGraphics();
    if (g == null) {
        System.out.println("g is null");
        return;
    }
    for(int i=0; i<100; i++) {
        renderSplashFrame(g, i);
        splash.update();
        try {
            Thread.sleep(5000);
        }
        catch(InterruptedException ex) {
        }
    }
    splash.close();
    setVisible(true);
    toFront();
}
@Override
public void actionPerformed(ActionEvent ae) {
    System.exit(0);
}

private static final WindowListener closeWindow = new WindowAdapter(){
    @Override
    public void windowClosing(WindowEvent e){
        e.getWindow().dispose();
    }
};

public static void main (String args[]) {

}
}

在我使用的Thread.sleep命令中,我希望它在5秒内没有停留在屏幕上。我的启动画面的图像位于源程序包

中的此项目中

1 个答案:

答案 0 :(得分:0)

你在for循环中调用Thread.Sleep。将其更改为

for(int i=0; i<100; i++) {
    renderSplashFrame(g, i);
    splash.update();
    try {
        Thread.sleep(50);
    }
    catch(InterruptedException ex) {
    }
}