I am attempting to use a JProgressBar as a health bar for a simple game. I am trying to get it to be full and visible to start the program. This is my code for the 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.