簡體   English   中英

啟動畫面進度條未繪制

[英]Splash Screen Progress bar not drawing

我正在嘗試在我的初始屏幕上制作自己的進度欄。 創建我的啟動畫面很容易:

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

我的main方法的第一行稱為:

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();
            }
        }
    }
}

我沒有任何錯誤,但是在它下面確實有一個小盒子:

下方帶有矩形的圖像。

如果將drawRects更改為(0,0,width,height),則沒有區別。

我嘗試使用以下方法調用Swing EDT:

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

但是什么也沒發生。

誰能看到這個問題? 還是知道如何解決?

從其他線程更新GUI時,應使用SwingUtilities.invokeLaterSwingUtilities.invokeAndWait

請參閱Swing中的並發性章節其中解釋了其背后的原因。

教程中的SplashScreen示例在Swing線程內執行Thread.sleep 如果在顯示SplashScreen時不需要其他GUI刷新,也可以。 但是,您的加載代碼應在其他線程中發生。

我建議將setPercent setter添加到您的類中,該setPercent setter創建一個Runnable以通過SwingUtilities.invokeLater更新GUI。 這樣,您甚至不需要輪詢percent變量,並且SwingThread也可以自由呈現其他UI內容。

Bikeshedder是對的(+1),您正在阻止EDT。

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();
    }
}

使用SwingUtilities.invokeAndWait((new Splash())); Runnable放到事件隊列中,這意味着當您輸入while循環和Thread.sleep ,您將阻止事件隊列調度任何新事件,包括重畫請求

您應該使用諸如SwingWorker類的東西來執行您的實際加載(在后台線程中),發布進度結果,並在初始屏幕上顯示該結果。

在此處輸入圖片說明

public class TestSplashScreen {

    public static void main(String[] args) {
        new TestSplashScreen();
    }

    public TestSplashScreen() {
        SplashScreenWorker worker = new SplashScreenWorker();
        worker.execute();
        try {
            worker.get();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("All Done...");
//        Launch main application...
//        SwingUtilities.invokeLater(...);
    }

    public class SplashScreenWorker extends SwingWorker<Void, Float> {

        private SplashScreen splash;

        public SplashScreenWorker() {
            splash = SplashScreen.getSplashScreen();
            if (splash == null) {
                System.out.println("SplashScreen.getSplashScreen() returned null");
                return;
            }
        }

        @Override
        protected void process(List<Float> chunks) {
            Graphics2D g = splash.createGraphics();
            if (g == null) {
                System.out.println("g is null");
                return;
            }
            float progress = chunks.get(chunks.size() - 1);
            int height = splash.getSize().height;
            int width = splash.getSize().width;
            g.setComposite(AlphaComposite.Clear);
            g.fillRect(0, 0, width, height);
            g.setPaintMode();
            g.setColor(Color.WHITE);
            g.drawRect(0, height - 50, width, 50);
            g.setColor(Color.RED);
            int y = height - 50;
            g.fillRect(0, y, (int) (width * progress), 50);
            FontMetrics fm = g.getFontMetrics();
            String text = "Loading Microsoft Windows..." + NumberFormat.getPercentInstance().format(progress);
            g.setColor(Color.WHITE);
            g.drawString(text, (width - fm.stringWidth(text)) / 2, y + ((50 - fm.getHeight()) / 2) + fm.getAscent());
            g.dispose();
            splash.update();
        }

        @Override
        protected Void doInBackground() throws Exception {
            for (int value = 0; value < 1000; value++) {

                float progress = value / 1000f;
                publish(progress);
                Thread.sleep(25);

            }
            return null;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM