Function 睡眠功能不能正常工作

Function 睡眠功能不能正常工作,function,netbeans,sleep,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

我正在使用下面的代码制作我的启动屏幕

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秒钟内不在屏幕上。“我的启动屏幕”的图像位于此项目的源程序包中,您正在调用Thread.Sleep,它位于for循环中。换成

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

你观察到的实际行为是什么?我下面的回答解决了你的问题吗?谢谢,我现在不在电脑旁,但我回来后会修改代码