Java 如何制作带有渐变的JProgress条

Java 如何制作带有渐变的JProgress条,java,swing,jprogressbar,tablecellrenderer,Java,Swing,Jprogressbar,Tablecellrenderer,我正在使用swing,希望通过添加与应用程序主题相匹配的软渐变,使我的JProgress条看起来更具吸引力 我的进度条在一个我正在使用的JTable单元格中,但我当前的示例使用渐变来绘制单元格,而不是JProgress栏本身。我想细胞是纯白色,但进度条从浅灰色->到深灰色,因为它得到更完整的 如果有其他东西会起作用,可以不使用JProgress栏。(可能只是使用单元渲染器?) 我的手机渲染器 class JPBRenderer extends JProgressBar implements Ta

我正在使用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);
}
}
我的批处理类包含

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

    //Constructors and getters...
}

提前谢谢

您将需要一个定制的
ProgressBarUI
,它可能来自于插图。您可以使用现有的
paintdefinite()
实现作为指导。此时会看到应用于基础滑动面的
LinearGradientPaint


可选的,考虑<代码> PROCESSICON 所示。

取决于L&F,为什么有Super?PruttCe组件(G);在一个paintComponent中调用两次,然后为什么要切换不透明度,搜索自定义JProgresBars,然后将此逻辑应用于XxxCellRenderer,也许我会使用JLAyer进度条的绘制由UI委托提供,您需要创建自己的…编辑过的帖子,因为这是伪造代码,谢谢。JLayer可能是解决这个问题的好方法。