Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java swing下载条_Java_Swing_Download_Progress Bar - Fatal编程技术网

java swing下载条

java swing下载条,java,swing,download,progress-bar,Java,Swing,Download,Progress Bar,我的代码中有一个小错误,在谷歌上搜索了几个小时后,我仍然没有找到答案。问题是进度条上方的文本不会更新。这是我的密码: public class ProgressGlassPane extends JComponent { private static final long serialVersionUID = -2488095988836592321L; private static final int BAR_WIDTH = 200; private static f

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

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,非常感谢你!!它成功了。

1。ProgressGlassPane类中的setProgress不应该包含新线程(new Runnable(){,这是相反的,只是为了设置一个值和调用repaint,2.public void calculateProgress()中的计时器应该是Swing Timer,而不是java.util.Timer.3。(即使这段代码也打破了Swing中一致性的所有好习惯)它在Java6、7和8中也能在Win10_64b、4中正常工作。请参阅有关初始线程的说明。放置在GlassPane中的标准JProgressBar比此代码更好,只需在UIManager中设置颜色这可能会解决您的问题
public void setNewProgress(int progress){this.progress=progress;repaint();}