简体   繁体   English

Java Swing GUI控制台组件

[英]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. 我目前正在创建我的第一个Java Swing应用程序,作为GUI的一部分,我有一个JTextField组件形式的小型控制台。 I would like to be able to print to this console from anywhere in the application using a command like console.print(String) . 我希望能够使用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. 我还想保持打印到Eclipse控制台的能力。 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); 您应用程序的其他部分可以导入MyConsole并调用MyConsole.print(msg);

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

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