2

我正在编写一个读取音频、分析这些数据然后实时显示结果的应用程序。目前,我正在使用 SwingWorker 运行启动后台分析的循环,并在每次分析完成时调用循环内的 SwingUtilities.invokeLater 方法来更新 GUI 组件。目前,GUI 似乎在随机更新,有时根本没有。

以下代码显示了我如何尝试完成此操作。TunerListener 是 JPanel 子类的内部类。PrevNote、nextNote、frequency 和 light 变量都是我要更新的 JPanel 子类中的组件:

private class TunerListener implements ActionListener {
    private boolean firstUpdate = true;
    private boolean executing = false;
    private TunerWorker tunerWorker = null;

    private final class TunerWorker extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() {
            while (!this.isCancelled()) {

                // Audio analysis in worker thread
                model.update(firstUpdate);

                // Update components in EDT
                if (!this.isCancelled()) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            prevNote.setText(model.getPrev());
                            currentNote.setText(model.getNote());
                            nextNote.setText(model.getNext());
                            frequency.setText("Frequency: "
                                    + model.getFrequency());
                            switch (model.getOffset()) {
                                case -2:
                                    light_2.setIcon(onRed);
                                    light_1.setIcon(off);
                                    light0.setIcon(offBig);
                                    light1.setIcon(off);
                                    light2.setIcon(off);
                                    break;
                                case -1:
                                    light_2.setIcon(off);
                                    light_1.setIcon(onRed);
                                    light0.setIcon(offBig);
                                    light1.setIcon(off);
                                    light2.setIcon(off);
                                    break;
                                case 0:
                                    light_2.setIcon(off);
                                    light_1.setIcon(off);
                                    light0.setIcon(onGreen);
                                    light1.setIcon(off);
                                    light2.setIcon(off);
                                    break;
                                case 1:
                                    light_2.setIcon(off);
                                    light_1.setIcon(off);
                                    light0.setIcon(offBig);
                                    light1.setIcon(onRed);
                                    light2.setIcon(off);
                                    break;
                                case 2:
                                    light_2.setIcon(off);
                                    light_1.setIcon(off);
                                    light0.setIcon(offBig);
                                    light1.setIcon(off);
                                    light2.setIcon(onRed);
                                    break;
                            }
                            firstUpdate = false;
                        }
                    });
                }

            }

            return null;
        }

        @Override
        protected void done() {
        }

    };

    @Override
    public void actionPerformed(ActionEvent ae) {

        if (ae.getActionCommand().equals("tune")) {
            if (!executing) {
                executing = true;
                firstUpdate = true;
                tune.setText("Stop Tuning");
                tunerWorker = new TunerWorker();
                tunerWorker.execute();
            } else {
                tune.setText("Start Tuning");
                executing = false;
                tunerWorker.cancel(true);
            }
        }

    }
}

编辑 我注意到,当我使用调试器时,有时我会告诉我找不到源,并且在调试窗口中它会说一些关于 FutureTask$Sync.innerRun 的信息。这会缩小范围吗?

4

1 回答 1

1

作为替代方案,使用此处所示的 的实例javax.swing.Timer播放所选的,此处所示。该方法提供异步操作的 a ,并且使得构造 a特别容易。StartStopNoteplay()SourceDataLineenum NoteJComboBox

笔记

final JComboBox combo = new JComboBox();
for (Note note : Note.values()) {
    combo.addItem(note);
}
combo.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(combo.getSelectedItem());
    }
});
this.add(combo);
于 2012-05-05T19:18:34.877 回答