从可运行的更新GUI

从可运行的更新GUI

本文介绍了从可运行的更新GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Swing应用程序,其中一部分功能应该是处理和输出一些文字的视觉和听觉(使用Mary TTS)。我需要一些建议的GUI和文本处理类的沟通的最佳方式。

I'm building a Swing application and one part of the functionality should be to process and output some text visually and audibly (using Mary TTS). I need some advice on the best way for the GUI and text processing classes to communicate.

GUI类是JPanel的子类。在其中我有一个类实现Runnable,称为LineProcesser,准备分发到音频播放器的文本。我使用线程执行器保持这个关闭EDT(这可能不是最好的方法,但似乎实现了我的后果)。

The GUI class is a subclass of JPanel. Within that I have a class implementing Runnable, called LineProcesser, which prepares the text to be dispatched to an audio player. I'm using a thread executor to keep this off the EDT (that may not be the best way but it seems to achieve the result I'm after).

我的目的是让LineProcessor运行所有文本,并在每行结尾更新JTextArea。此外,它将需要停止并等待用户在某些点的输入。在用户输入完成后,GUI类应该告诉它恢复处理。

My intention is for LineProcessor to run through all the text and update a JTextArea at the end of each line. Additionally it will need to halt and wait for user input at certain points. After the user input has been completed the GUI class should tell it to resume processing.

以下代码说明了我目前的情况:

The following code illustrates what I currently have:

public class MyPanel extends JPanel {
    ExecutorService lineExecutor = Executors.newSingleThreadExecutor();
    Runnable lineProcessor = new LineProcessor();

    public class LineProcessor implements Runnable {

        private int currentLineNo = 0;

            public LineProcessor() {
            //  ...
            }

            @Override
            public void run() {
                // call getText();
                // call playAudio();
                currentLineNo++;
            }
        }
    }

    private JButton statusLbl = new JLabel();
    private JButton mainControlBtn = new JButton();

    private void mainControlBtnActionPerformed(ActionEvent evt) {

        if (mainControlBtn.getText().equals("Start")) {
                          lineExecutor.submit(lineProcessor);
                          mainControlBtn.setText("Running");
        }
    }
}

LineProcessor如何通知GUI组件他们需要更改,以及如何可以从GUI中暂停和重新启动?我困惑了我是否需要一个Swing Worker,属性/事件侦听器或其他东西?我已经阅读的例子有意义,但我不能看到我如何应用他们的代码我在这里。

How can LineProcessor notify GUI components that they need to change and how can it be paused and restarted from within the GUI? I'm confused as to whether I need a Swing Worker, property/event listeners or something else? The examples I've read sort of make sense but I can't see how I can apply them to the code I have here.

推荐答案

所有你需要做的是在Runnable中封装任何Swing调用,并通过 SwingUtilities.invokeLater(myRunnable); 将它排队到EDT。而已。不需要SwingWorker。

All you need to do is wrap any Swing calls in a Runnable, and queue it on the EDT via SwingUtilities.invokeLater(myRunnable);. That's it. No need for a SwingWorker.

例如,

public class LineProcessor implements Runnable {
  private int currentLineNo = 0;
  Runnable LineProcessor = new LineProcessor();  // won't this cause infinite recursion?

  public LineProcessor() {
     // ...
  }

  @Override
  public void run() {
     // call getText();
     // call playAudio();
     currentLineNo++;

     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           // *** Swing code can go here ***
        }
     });
  }
}

这篇关于从可运行的更新GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:23