默认情况下,“绘制的字符串”出现在JProgressBar的中心,但我希望“绘制的字符串”应该出现在JProgressbar的值之后,例如,如果JprogressBar的值为0,则“字符串绘制”应该出现在JProgressBar的最左边,请告诉我该怎么办?
答案 0 :(得分:1)
你需要继承JProgressBar ...
private class CustomProgressBar extends JProgressBar{
private static final long serialVersionUID = 1L;
private boolean isStringToBePainted = false;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(isStringToBePainted ){
Dimension size = CustomProgressBar.this.getSize();
int x = (int)( size.width * CustomProgressBar.this.getPercentComplete() );
int height = g.getFontMetrics().getHeight();
int d = g.getFontMetrics().getDescent();
int y = (size.height + height)/2-d;
String text = getString();
g.setColor(Color.BLACK );
g.drawString(text, x, 12);
}
}
@Override
public void setStringPainted(boolean b) {
// don't do super.setStringPainted(b);
//super.setStringPainted(b);
isStringToBePainted=b;
}
}
也许你玩了一下字体颜色,我还没找到合适的地方......