不拉丝闪屏进度条 [英] Splash Screen Progress bar not drawing

查看:158
本文介绍了不拉丝闪屏进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使自己的进度条,我的启动画面。创建我的启动画面很简单:

I'm trying to make my own progress bar, on my splash screen. Creating my splash screen was easy:

Java的-splash:EaseMailMain.jpg Main.class
  (在Eclipse)

java -splash:EaseMailMain.jpg Main.class (From Eclipse)

我的主要方法的第一行调用这样的:

The first line of my main method calls this:

new Thread(new Splash()).start();

这是飞溅类:

    public class Splash implements Runnable {
    public volatile static int percent = 0;
    @Override
    public void run() {
        System.out.println("Start");
        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;
        }
        int height = splash.getSize().height;
        int width = splash.getSize().width;
        //g.drawOval(0, 0, 100, 100);
        g.setColor(Color.white);
        g.drawRect(0, height-50, width, 50);
        g.setColor(Color.BLACK);
        while(percent <= 100) {
            System.out.println((width*percent)/100);
            g.drawRect(0, height-50, (int)((width*percent)/100), 50);
            percent += 1;
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

我没有得到任何错误,但我得到一个小盒子,它的下面:

I don't get any errors, but I do get a small box underneath it:

如果我改变drawRects为(0,0,宽度,高度)没有区别。

If I change the drawRects to (0, 0, width, height) it makes no difference.

我试过调用秋千上与美国东部时间:

I've tried invoking on the swing EDT with:

SwingUtilities.invokeAndWait((new Splash()));

但没有任何反应。

But nothing happens.

任何人都可以看到这个问题?或不知道如何解决它?

Can anyone see the problem? Or know how to fix it?

推荐答案

您应该使用<一个href=\"http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29\"相对=nofollow> SwingUtilities.invokeLater 或<一个href=\"http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeAndWait%28java.lang.Runnable%29\"相对=nofollow> SwingUtilities.invokeAndWait 从不同的线程更新界面时。

You should use SwingUtilities.invokeLater or SwingUtilities.invokeAndWait when updating the GUI from a different thread.

请参见摆动章并发这也解释了这背后的原因。

Please see the Chapter Concurrency in Swing which explains the reasons behind this.

借助闪屏为例从教程并在视频下载 Swing线程中。这是好的,太多,如果你不需要任何其他GUI刷新,而闪屏是显示。您加载code然而应该发生在不同的线程。

The SplashScreen Example from the Tutorial does the Thread.sleep inside the Swing thread. This is fine, too if you do not need any other GUI refresh while the SplashScreen is showing. Your loading code however should happen in a different thread.

我会建议增加一个 setPercent 二传手你的类创建一个的Runnable 通过<$更新GUI C $ C> SwingUtilities.invokeLater 。这样,你甚至都不需要轮询百分比变量和 SwingThread 是免费来渲染其他UI的东西,太

I would recommend adding a setPercent setter to your class which creates a Runnable to update the GUI via SwingUtilities.invokeLater. That way you do not even need to poll the percent variable and the SwingThread is free to render other UI stuff, too.

这篇关于不拉丝闪屏进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆