gpt4 book ai didi

java - 如何制作带有渐变的进度条

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:07 25 4
gpt4 key购买 nike

我正在使用 swing,并希望通过向其添加与应用程序主题相匹配的柔和渐变,使我的 JProgress 栏看起来更有吸引力。

我的进度条位于我正在工作的 JTable 单元格中,但我当前的示例使用渐变而不是 JProgress 条本身绘制单元格。我希望单元格为纯白色,但进度条会随着它变得更完整而从浅灰色 -> 变为深灰色。

如果其他方法可以解决问题,请不要使用 JProgress 栏。 (也许只是使用单元格渲染器?)

我的单元格渲染器

class JPBRenderer extends JProgressBar implements TableCellRenderer {

Batch batch;

public JPBRenderer() {
super();
setStringPainted(true);
setBorderPainted(false);
setOpaque(false); //EDIT 1
setForeground(UIConfig.backgroundColor);
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
batch = ((BatchTableModel)table.getModel()).getBatchForRow(row);
setMinimum(0);
setMaximum(batch.getQuantityToProduce());
setValue(batch.getQuantityCompleted());
setString(GeneralUtils.formatNumber((batch.getQuantityCompleted()/batch.getQuantityToProduce())*100, 1) + " %");
return this;
}

@Override
protected void paintComponent(Graphics g) {

Color color2 = UIConfig.backgroundColor;
Color color1 = UIConfig.backgroundColor.brighter().brighter();
double value = 1;
if(batch != null){
value = batch.getQuantityCompleted()/batch.getQuantityToProduce();
}
int w = getWidth();
int h = getHeight();
Graphics2D g2d = (Graphics2D) g;

GradientPaint gp = new GradientPaint(0, 0, color1, w, 0, color2);

g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);

super.paintComponent(g);
}
}

我的 Batch 类包含...

public class Batch {
private Integer id;
private BigDecimal length;
private int quantityToProduce;
private int quantityCompleted;

//Constructors and getters...
}

提前致谢!

最佳答案

您需要一个自定义的 ProgressBarUI,可能源自 BasicProgressBarUI图文并茂here .您可以使用现有的 paintDeterminate() 实现作为指南。可以看到应用于 BasicSliderUILinearGradientPaint here .

或者,考虑显示的 ProgressIcon here .

关于java - 如何制作带有渐变的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21998073/

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