简体   繁体   中英

Using main class methods in GUI (netbeans)

I'd like to call a method that is in my main class into my GUI. (a text area)

My method that is in the separate class looks like this:

public void DisplayHS() {
  highscore temp;
  for(int i = 0; i<(count-1);i ++) {
    for (int j =(i +1); j<count; j ++) {
      if (HA[i].getScore() > HA[j].getScore()) {
        temp = HA[i];
        HA[i] = HA[j];
        HA[j] = temp;
      }
    }
  }
  for (int i = 0; i<9; i ++) {
    System.out.println((i+1) +"." + HA[i]);
  }
}  

How do I call that method into my GUI text area?

Thank you.

The fact that you call it from a GUI class makes no difference:

  • either the method is static and you can call it with NameOfTheContainingClass.displayHS();
  • or it is not (seems to be your case) and you need an instance of the containing class: NameOfTheContainingClass instance = new NameOfTheContainingClass(); instance.displayHS(); NameOfTheContainingClass instance = new NameOfTheContainingClass(); instance.displayHS();

ps: method names in Java start in small caps

Since your method takes no parameters, it looks like the collection is a field in your class. I'd reccomend you give a reference to the main class in your GUI class, and call the method from your main class field or through the method local reference to the main class object.

You can give the referene in the constructor or through mutator methods.

You said

call that method into my GUI text area?

What do you mean by that? Do you wish to display the text there, just print the text in terminal(assuming you didn't change the printstreams) or call the method when content in the text area?

Are we talking JSP or a desktop application?

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.

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