为了完整性,如果您想显示进度条。引用SplashScreen
javadoc:
当 Swing/AWT 显示第一个窗口时,启动画面窗口会自动关闭。
这意味着它不能使用标准的 AWT/Swing 组件(例如 JProgressBar
),但只能通过 Graphics2D
方法进行绘制(例如 drawText()
、fillRect()
等)。
因此,您需要在一个单独的类中模拟进度条(代码仅作为非常粗略的演示给出。您应该检查 SplashScreen.isVisible()
):
public class SplashProgressBar {
SplashScreen splash;
Graphics2D g;
int x, y, w, h;
int val, max;
String text;
public SplashProgressBar(SplashScreen splash, int x, int y, int w, int h) {
this.splash = splash;
g = splash.createGraphics();
this.x = x;
this.val = 0;
this.max = 100;
}
public void setMaximum(int n) {
max = n;
update();
}
public void setValue(int n) {
val = n;
update();
}
public void setString(String text) {
this.text = text;
update();
}
public void update() {
g.setColor(Color.WHITE);
g.fillRect(x, y, w, h);
g.setColor(Color.DARK_GRAY);
g.drawRect(x, y, w, h);
g.setColor(Color.ORANGE);
g.fill3DRect(x, y, w * val / max, h, true);
if (text != null) {
g.setColor(Color.BLACK);
g.drawString(text, x + (w - g.getFontMetrics().stringWidth(text)) / 2.0f, y + 8 - 2);
}
splash.update();
}
}
然后:
SplashProgressBar spb = new SplashProgressBar(SplashScreen.getSplashScreen(), 10, 100, 300, 15);
spb.setMaximum(processing.size());
for (int i = 0; i < processing.size(); i++) {
spb.setValue(i);
spb.setString("Processing #"+(i+1));
}