繁体   English   中英

java swing下载栏

[英]java swing downloading bar

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

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

    }
}

谢谢您的帮助

首先感谢您的快速解答。 @mKorbel,我必须插入一个线程,因为在显示过程中,我将从服务器上下载文件,并且如果我不将绘画放到线程中,它将不会更新。

@Ashiquzzaman,非常感谢! 有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM