简体   繁体   中英

Java Swing GUI Console Component

I'm currently creating my first java swing application and as part of the GUI I have a small console in the form of a JTextField component. I would like to be able to print to this console from anywhere in the application using a command like console.print(String) . I believe that I should be using print stream but I can't figure out how to make this work properly from anywhere (ie in another class which doesn't reference the console) .
I would also like to maintain the ability to print out to the eclipse console. Any help on this matter would be much appreciated.

Create a class for the purpose of accepting and distributing console strings; give it a static method to print to your console. Give that class a (static) reference to your console component.

something like:

public MyConsole
{
  private static TextField field;
  public static void setField(TextField givenField)
  {
    field = givenField;
  }

  public static void print(String msg)
  {
    field.append(msg);
  }
}

The other parts of your application can import MyConsole and call MyConsole.print(msg);

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