gpt4 book ai didi

java - 如何在JProgressBar中设置 "string painted"的位置

转载 作者:行者123 更新时间:2023-12-01 12:38:04 25 4
gpt4 key购买 nike

默认情况下,“字符串绘制”出现在 JProgressBar 的中心,但我希望“字符串绘制”应该出现在 JProgressbar 值之后的点,例如,如果 JprogressBar 的值为 0,“字符串绘制”应该出现出现在 JProgressBar 的最左边,请告诉我该怎么做???

最佳答案

您需要子类化 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;
}
}

也许你稍微调整一下字体颜色,我还没找到合适的字体...

enter image description here

关于java - 如何在JProgressBar中设置 "string painted"的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25385700/

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