swing:制作一个 JProgressBar 并在其上叠加标签?

发布于 2024-09-06 07:56:35 字数 380 浏览 10 评论 0原文

我想使用 JProgressBar 并对其进行扩充以打印其当前值以及图形栏。

progress bar

我猜最好的方法是覆盖paintComponent:

@Override protected void paintComponent(Graphics g) {
// Let component paint first 
    super.paintComponent(g); 
// paint my contents next....
}

但我不确定...有什么建议吗?

I would like to use a JProgressBar and augment it to print its current value as well as the graphical bar.

progress bar

I'm guessing the best way to do this is to override paintComponent:

@Override protected void paintComponent(Graphics g) {
// Let component paint first 
    super.paintComponent(g); 
// paint my contents next....
}

but I am not sure... any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2024-09-13 07:56:35

我建议你使用它自己的API:

public void setStringPainted(boolean b)

设置 stringPainted 属性的值,该属性确定进度条是否应呈现进度字符串。默认值为 false,表示不绘制任何字符串。有些外观可能不支持进度字符串,或者仅当进度栏处于确定模式时才支持它们。

public void setString(String s)

设置进度字符串的值。默认情况下,该字符串为空,表示使用简单百分比字符串的内置行为。如果您提供了自定义进度字符串并想要恢复为内置行为,请将该字符串设置回 null。
仅当 isStringPainted 方法返回 true 时才会绘制进度字符串。

参考总是一件好事。 :)

请注意,要在运行期间编辑值,您应该考虑将 ChangeListener 附加到进度栏。

I would suggest you to use its own API:

public void setStringPainted(boolean b)

Sets the value of the stringPainted property, which determines whether the progress bar should render a progress string. The default is false, meaning no string is painted. Some look and feels might not support progress strings or might support them only when the progress bar is in determinate mode.

and

public void setString(String s)

Sets the value of the progress string. By default, this string is null, implying the built-in behavior of using a simple percent string. If you have provided a custom progress string and want to revert to the built-in behavior, set the string back to null.
The progress string is painted only if the isStringPainted method returns true.

Reference is always a good thing. :)

Mind that to enable editing the value during its run you should consider having a ChangeListener attached to the progress bar.

停顿的约定 2024-09-13 07:56:35

基于杰克答案的工作解决方案:

final JProgressBar pbar = ...
pbar.setStringPainted(true);
final NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(true);
pbar.setString(nf.format(pbar.getValue()));             
pbar.addChangeListener(new ChangeListener()
{
    @Override public void stateChanged(ChangeEvent event) {
        pbar.setString(nf.format(pbar.getValue()));             
    }           
});

Working solution based on Jack's answer:

final JProgressBar pbar = ...
pbar.setStringPainted(true);
final NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(true);
pbar.setString(nf.format(pbar.getValue()));             
pbar.addChangeListener(new ChangeListener()
{
    @Override public void stateChanged(ChangeEvent event) {
        pbar.setString(nf.format(pbar.getValue()));             
    }           
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文