java-如何制作带有渐变的JProgress栏
作者:互联网
我正在使用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,可能是从BasicProgressBarUI
所示的here派生的.您可能可以使用现有的paintDeterminate()实现作为指南.看到一个应用于BasicSliderUI的LinearGradientPaint显示为here.
或者,考虑显示为here的ProgressIcon.
标签:jprogressbar,swing,tablecellrenderer,java 来源: https://codeday.me/bug/20191122/2056764.html