【问题标题】:java swing downloading barjava swing下载栏
【发布时间】:2015-12-02 10:44:50
【问题描述】:

我的代码中有一个小错误,在谷歌上搜索了几个小时后,我仍然没有找到答案。问题是进度条上方的文本没有更新。这是我的代码:

public class ProgressGlassPane extends JComponent {

    private static final long serialVersionUID = -2488095988836592321L;
    private static final int BAR_WIDTH = 200;
    private static final int BAR_HEIGHT = 10;
    private final Color TEXT_COLOR = new Color(0x333333);
    @SuppressWarnings("unused")
    private final Color BORDER_COLOR = new Color(0x333333);
    private final float[] GRADIENT_FRACTIONS = new float[] { 0.0f, 0.499f,
            0.5f, 1.0f };
    private final Color[] GRADIENT_COLORS = new Color[] { Color.GRAY,
            Color.DARK_GRAY, Color.BLACK, Color.GRAY };
    private final Color GRADIENT_COLOR2 = Color.WHITE;
    private final Color GRADIENT_COLOR1 = Color.GRAY;

    private int progress;
    private String message = "Downloading file(s): ";

    public ProgressGlassPane() {
        setBackground(Color.WHITE);
        setFont(new Font("Default", Font.BOLD, 16));
        this.progress = 0;
    }

    public int getProgress() {
        return progress;
    }

    public void setNewProgress(int progress) {
        this.progress = progress;
    }

    public void setProgress(int progress) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // set new progress
                int oldProgress = getProgress();
                setNewProgress(progress);

                // computes the damaged area
                FontMetrics metrics = getGraphics().getFontMetrics(getFont());
                int w = (int) (BAR_WIDTH * ((float) oldProgress / 100.0f));
                int x = w + (getWidth() - BAR_WIDTH) / 2;
                int y = (getHeight() - BAR_HEIGHT) / 2;
                y += metrics.getDescent() / 2;

                w = (int) (BAR_WIDTH * ((float) progress / 100.0f)) - w;
                int h = BAR_HEIGHT;
                // repaint
                repaint(x, y, w, h);
            }
        }).start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        // enables anti-aliasing
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        // gets the current clipping area
        Rectangle clip = g.getClipBounds();

        // sets a 65% translucent composite
        AlphaComposite alpha = AlphaComposite.SrcOver.derive(0.65f);
        Composite composite = g2.getComposite();
        g2.setComposite(alpha);

        // fills the background
        g2.setColor(getBackground());
        g2.fillRect(clip.x, clip.y, clip.width, clip.height);

        // centers the progress bar on screen
        FontMetrics metrics = g.getFontMetrics();
        int x = (getWidth() - BAR_WIDTH) / 2;
        int y = (getHeight() - BAR_HEIGHT - metrics.getDescent()) / 2;

        // draws the text
        g2.setColor(TEXT_COLOR);
        System.out.println("drawed string: " + message + getProgress() + "%");
        g2.drawString(message + getProgress() + "%", x, y);

        // goes to the position of the progress bar
        y += metrics.getDescent();

        // computes the size of the progress indicator
        int w = (int) (BAR_WIDTH * ((float) progress / 100.0f));
        int h = BAR_HEIGHT;

        // draws the content of the progress bar
        Paint paint = g2.getPaint();

        // bar's background
        Paint gradient = new GradientPaint(x, y, GRADIENT_COLOR1, x, y + h,
                GRADIENT_COLOR2);
        g2.setPaint(gradient);
        g2.fillRect(x, y, BAR_WIDTH, BAR_HEIGHT);

        // actual progress
        gradient = new LinearGradientPaint(x, y, x, y + h, GRADIENT_FRACTIONS,
                GRADIENT_COLORS);
        g2.setPaint(gradient);
        g2.fillRect(x, y, w, h);

        g2.setPaint(paint);

        // draws the progress bar border
        g2.drawRect(x, y, BAR_WIDTH, BAR_HEIGHT);

        g2.setComposite(composite);
    }
}

然后是主要的:

public class Test extends JFrame {

    private ProgressGlassPane glass;
    private int progess = 0;

    public Test() {
        this.setSize(new Dimension(500, 500));
        this.setVisible(true);
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.setLocation(0, 0);

        setGlassPane(glass = new ProgressGlassPane());
        glass.setVisible(true);
    }

    public void calculateProgress() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println("progress: " + progess);
                glass.setProgress(progess);
                progess += 10;
                if (progess > 100) {
                    timer.cancel();
                    System.out.println("download finish");
                }
            }
        }, 0, 300);

    }

    public static void main(String[] args) {
        Test test = new Test();
        test.calculateProgress();

    }
}

感谢您的帮助

【问题讨论】:

  • 1.类 ProgressGlassPane 中的 setProgress 不应该包含 new Thread(new Runnable() {,这是相反的,只是为了设置一个值并调用 repaint,2. 公共 void calculateProgress() { 中的计时器应该是 Swing Timer,而不是java.util.Timer. 3. (即使这段代码打破了 Swing 中 Concurency 的所有好习惯)它在 Win10_64b 中也可以在 Java6、7 和 8 中正常工作,4. 也请参阅关于 Initilial Thread 的描述
  • GlassPane 中放置的标准 JProgressBar 比这段代码好,只是为了在 UIManager 中设置颜色
  • 这可能会解决你的问题public void setNewProgress(int progress) { this.progress = progress; repaint(); }

标签: java swing download progress-bar


【解决方案1】:

首先感谢您的快速回答。 @mKorbel,我必须放入一个线程,因为在显示期间我会从服务器下载文件,如果我不将油漆放入线程中,它不会更新。

@Ashiquzzaman,非常感谢!!它奏效了。

【讨论】:

    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    相关资源
    最近更新 更多