gpt4 book ai didi

Java Swing 以饼图形式显示进度

转载 作者:行者123 更新时间:2023-12-04 04:59:22 26 4
gpt4 key购买 nike

我想以饼图格式显示进度。任何人都可以帮忙吗?
我有要显示的数据,但是如何以这种格式显示它。当我们下载文件时,类似于 Google Chrome 中显示的进度。

image

最佳答案

根据@trashgod 提供的链接,这里是一种可以实现“馅饼”效果的方法。这仅处理不确定的进度条,但添加“确定”行为将非常容易。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.plaf.basic.BasicProgressBarUI;

/** @see http://stackoverflow.com/questions/8884297 */
public class ProgressBarUITest extends JPanel {

public ProgressBarUITest() {
super(new BorderLayout());
JProgressBar jpb = new JProgressBar();
jpb.setUI(new MyProgressUI());
jpb.setForeground(Color.GRAY);
jpb.setIndeterminate(true);
this.add(jpb);
}

private static class MyProgressUI extends BasicProgressBarUI {

private static final int ARC_EXTENT = 25;

@Override
protected void installDefaults() {
super.installDefaults();
progressBar.setBorder(null);
}

@Override
protected void paintIndeterminate(Graphics g, JComponent c) {
int angle = (int) (90 - 360.0 * getAnimationIndex() / getFrameCount());
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(progressBar.getForeground());
int size = Math.min(c.getWidth(), c.getHeight());
g2d.fillArc((c.getWidth() - size) / 2, (c.getHeight() - size) / 2, size, size, angle - ARC_EXTENT, ARC_EXTENT);
}

@Override
protected Rectangle getBox(Rectangle r) {
if (r != null) {
r.setBounds(progressBar.getBounds());
return r;
}
return progressBar.getBounds();
}

@Override
public Dimension getPreferredSize(JComponent c) {
return new Dimension(100, 100);
}
}

private void display() {
JFrame f = new JFrame("ProgressBarUITest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new ProgressBarUITest().display();
}
});
}
}

关于Java Swing 以饼图形式显示进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16331331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com