简体   繁体   English

从不同的地方向Swing JTextArea发送消息

[英]Sending messages to a swing JTextArea from different places

I have a JTextArea always visible in my main app window (a Log if you like), and I want to use it to display activity going on in the system (like mock-debug output you'd do with System.out.println() in if conditions or whatever) 我有一个JTextArea在我的主应用程序窗口中始终可见(如果需要,则为Log),并且我想用它来显示系统中正在进行的活动(例如您将对System.out.println( )在条件或其他条件下

I mean high level things the user does, (like "successfully loaded file " or " written to disk", " completed" etc) 我指的是用户所做的高级操作(例如“成功加载文件”或“写入磁盘”,“完成”等)

Thing is such messages can be generated anywhere in my system mainly in another package the classes of which deal with the data and computation, and they're unaware of the GUI. 这样的消息可以在我的系统中的任何地方生成,主要是在另一个包中,这些包的类处理数据和计算,并且它们不知道GUI。

Maybe save the messages to a temp file and the textarea "monitors" that file for changes, how can this be done? 也许将消息保存到临时文件中,然后将textarea“监视器”保存到该文件中以进行更改,该怎么做?

The simplest way is to define a logger interface: 最简单的方法是定义记录器接口:

package com.example.logging;
public interface ActivityLogger {
    void logAction(String message);
}

Then pass it to your non-GUI components so they don't get tied to a specific implementation: 然后将其传递给您的非GUI组件,这样它们就不会与特定的实现挂钩:

public class FileLoader {

    private ActivityLogger logger;
    public FileLoader(ActivityLogger logger){
        this.logger = logger;
    }

    public void loadFile(){
        // load stuff from file
        logger.logAction("File loaded successfully");
    }

}

Now, making an implementation that writes to a text component is simple: 现在,进行写入文本组件的实现很简单:

public class TextComponentLogger implements ActivityLogger{
    private final JTextComponent target;
    public TextComponentLogger(JTextComponent target) {
        this.target = target;
    }

    public void logAction(final String message){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                target.setText(String.format("%s%s%n", 
                                             target.getText(),
                                             message));
            }
        });
    }
}
// Usage:
JTextArea logView = new JTextArea();
TextComponentLogger logger = new TextComponentLogger(logView);
FileLoader fileLoader = new FileLoader(logger);
fileLoader.loadFile();

You can of course also use a standard logging framework (java.util.logging, slf4j, log4j, etc) and write an appender that "writes" to a text component. 当然,您也可以使用标准的日志记录框架(java.util.logging,slf4j,log4j等),并编写一个“写入”文本组件的附加程序。

The design can be rather complicated. 设计可能相当复杂。 Maybe you can have a public access method like updateText() in the class where your TextArea would be. 也许在TextArea所在的类中可以有一个像updateText()这样的公共访问方法。 Then you create a kind of 'resource' or 'shared' class (just a plain class) that would be initialized together when your main() runs. 然后,您将创建一种“资源”或“共享”类(仅是普通类),当您运行main()时将它们一起初始化。 When the class containing your TextArea is created, an instance would be placed into the 'shared' class (this shared class should be a singleton) and so all the other classes call this 'shared' class (maybe a method like updateTextArea()) and what it would do is call the class containing the TextArea via that instance and call the TextArea to update text. 创建包含TextArea的类时,会将一个实例放入“共享”类(此共享类应为单例),因此所有其他类都将其称为“共享”类(也许是诸如updateTextArea()之类的方法)然后通过该实例调用包含TextArea的类,并调用TextArea更新文本。

The Message Console might be what you are looking for. 消息控制台可能就是您想要的。

Java also has a "Logger" API. Java还具有“记录器” API。

You can use EventBus to decouple your GUI from the other parts of your application. 您可以使用EventBus将GUI与应用程序的其他部分分离。 (My blog has another introduction ). (我的博客有另一个介绍 )。 You could do something as follows: 您可以执行以下操作:

public class LogArea extends JTextArea {
    public static final String LOG_TOPIC = "logarea_topic";

    public LogArea() {
        super();
        // Read in the annotations, register self as a listener to the topic
        AnnotationProcessor.process(this);
    }

    @EventTopicSubscriber(topic=LOG_TOPIC)
    public void logEvent(String topic, String text) {
        append(text + "\n");
    }

}

public class DomainClass {

    public void foo() {
        // Send out a notification throughout the system to whichever components
        // are registered to handle this topic.
        EventBus.publish(LogArea.LOG_TOPIC, "some text you want to appear in the log area");
    }

}

In a real system you'd probably want to move the topic declarations to another class so that one can use it without being tied to a specific implementation. 在真实的系统中,您可能希望将主题声明移至另一类,以便一个人可以使用它而不依赖于特定的实现。 Eg you could have a Topics class that just contains the static string constants of the topics. 例如,您可能有一个Topics类,只包含主题的静态字符串常量。 Then you can have multiple classes that listen to those topics and process the messages (eg you could have a standard logging framework which writes out to a log file in addition to the jtextarea component). 然后,您可以有多个类来侦听这些主题并处理消息(例如,除了jtextarea组件之外,您可以拥有一个标准的日志记录框架,该框架可以写出到日志文件中)。

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

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