登录Java GUI [英] Logging in Java GUI

查看:392
本文介绍了登录Java GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有 JTextArea 的GUI来显示某些消息。这里可能会出现两种消息:有意写入并打算供用户使用的消息(任务已完成未找到文件请稍候 ...)和开发人员的信息(有关例外,警告,帮助信息的信息......)。

I am developing a GUI with a JTextArea to show certain messages. Two kind of messages might appear here: those written on purpose and intended for the user ("Task completed", "No file found", "Please wait"...) and those for the developer (information about exceptions, warnings, help messages...).


  • 所以我正在考虑两个选项来显示 JTextArea 的信息:使用 setText()方法,或者使用 System.out.print()并将输出流重定向到 JTextArea

  • So far, I am considering two options to show information on the JTextArea: use the setText() method, or use System.out.print() and redirect the output stream to the JTextArea.

哪个选项更好?为什么?

Which option is better? Why?


  • 我如何实现一个系统,允许用户选择哪种消息是
    显示在 JTextArea ?类似于具有不同选项的 JComboBox ,因此用户可以选择他/她是否只想获得应用程序消息或与工作流程相关的消息(警告,异常等)。 )

  • How can I implement a system to allow the user to select which kind
    of messages are to be showed on the JTextArea? Something like a JComboBox with different options, so the User can select if he/she wants to get just Application Messages, or messages related to the workflow (warnings, exceptions, etc.)

推荐答案

如果你不想使用额外的记录库(如果程序的话)很小,或者如果你必须节省空间(例如在applet中)...),你总是可以使用JSE中的日志记录:java.util.logging。
如果是这样,你可以使用Handler写入文本组件:

If you don't want use to an extra logging library (if the program is small, or if you have to save space (for example in an applet)...), you can always use the logging in JSE: java.util.logging. If so, you can use a Handler to write to the text component:

final class TextComponentHandler extends Handler {
  @Nonnull private final JTextArea text;
    TextComponentHandler(@Nonnull JTextArea text) {
      this.text = text;
    }
  @Override
  public void publish(LogRecord record) {
    if (isLoggable(record))
      synchronized(text) {
        text.append(getFormatter().format(record));
      }
  }
  @Override
  public void flush() {/**/}
  @Override
  public void close() throws SecurityException {/**/}
}

并添加:

JTextArea textArea = new JTextArea() {
  @Override
  public void addNotify() {
    super.addNotify();
    for(Handler hh : logger.getHandlers())
      if (hh == h)
        return;
    logger.addHandler(h);
  }
  @Override
  public void removeNotify() {
    super.removeNotify();
    logger.removeHandler(h);
  }
};
Logger logger = Logger.getLogger("my.logger");
Handler h = new TextComponentHandler(textArea);
logger.addHandler(h);

这篇关于登录Java GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆