简体   繁体   English

JProgressBar没有正确显示

[英]JProgressBar not showing up right

I am attempting to use a JProgressBar as a health bar for a simple game. 我试图将JProgressBar用作简单游戏的健康栏。 I am trying to get it to be full and visible to start the program. 我正在尝试使其完整且可见,以启动该程序。 This is my code for the JProgressBar: 这是我的JProgressBar代码:

    progressBar = new JProgressBar();
    progressBar.setMinimum(0);
    progressBar.setMaximum(model.user.getTotalHp());
    progressBar.setValue(model.user.getTotalHp());
    progressBar.setForeground(Color.GREEN);
    progressBar.setString(model.user.getHp() + "/" + model.user.getTotalHp());
    progressBar.setBounds(450 + insets.left, 370 + insets.top,
             size.width+40, size.height + 45);
    add(progressBar);

This is the result: https://gyazo.com/4e6fc1879597ba67495bda183dbbf212 What am I doing wrong, it's not full, green, or have the String inside. 结果是这样的: https : //gyazo.com/4e6fc1879597ba67495bda183dbbf212我在做什么错了,它不完整,绿色或内部没有String。

The string isn't painting because you need to call this first: 该字符串未绘制,因为您需要先调用此字符串:

progressBar.setStringPainted(true);

I used the following code and the progress bar renders fine: 我使用以下代码,进度条呈现良好:

JFrame frame = new JFrame("test");
JProgressBar progressBar = new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setValue(50);
progressBar.setForeground(Color.GREEN);
progressBar.setString("50/100");
progressBar.setStringPainted(true);
frame.add(progressBar);
frame.setVisible(true);

Maybe your total is coming out as 0 or maybe a float being rounded to 0. 也许您的总数显示为0,或者浮点数舍入为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM