-3

Hey I'm having a problem with 3 lines of coding, and I still don't know what the problem is.

     public class HealthBar{
int min = 0;
int max = 100;
    JProgressBar PlayerHealth = new JProgressBar(min,max);
     JProgressBar EnemyHealth = new JProgressBar(min,max);
    PlayerHealth.setStringPainted(true);

    }

1 Answers1

2

You can't call this method:

PlayerHealth.setStringPainted(true);

like you're doing inside the class and not in a constructor or method. Instead, make this call in your class's constructor. And again, next time you ask a similar question, please provide all the information needed including the error message.

i.e.,

public class HealthBar {

   // it's OK To declare and initialize variables here
   int min = 0;
   int max = 100;
   JProgressBar playerHealth = new JProgressBar(min,max);
   JProgressBar enemyHealth = new JProgressBar(min,max);

   // but this is not valid
   // playerHealth.setStringPainted(true);


   // constructor
   public HealthBar() {
      // instead do it here!!!
      playerHealth.setStringPainted(true);  
   }
}

As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter.

Following these suggestions as well as following good code formatting practices will allow others (such as us!) to better understand your code, and more importantly, will allow your future self to better understand just what you were thinking 6 months ago when you wrote the code.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373