1

如何在 a 上逐行显示JTextPane并建立关联JProgressBar

我有这个循环:

  int k;
  for (k=0; k<array_TXT.length; k++) {
    if (array_TXT[k] != null) { 
      textPane.append( array_TXT[k] );
    }
 }

我想在几秒钟后一个接一个地附加每一行array_TXT[k](字符串)JTextPane,它必须同步到一个JProgressBar.

这样,每次k都有不同的值,它将打印在文本窗格上,并且进度条将始终与这些打印相关。

我已经阅读过,Thread.sleep(x);但我不知道该把它放在哪里才能达到我的目标。

4

2 回答 2

2

不要阻塞 EDT(事件调度线程)——当这种情况发生时,GUI 将“冻结”。而不是调用实现重复任务Thread.sleep(n)的 Swing或长时间运行的任务。有关更多详细信息,请参阅Swing 中的并发。TimerSwingWorker

于 2013-05-19T10:29:22.933 回答
2

你会想要使用一个SwingWorker. 它将允许您在后台执行更新,关闭事件调度线程。

SwingWorker 具有允许您更新回事件调度线程的publish方法。process

它还允许您触发和监控进度更新

SwingWorker 示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestProgress {

    public static void main(String[] args) {
        new TestProgress();
    }

    public TestProgress() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JProgressBar pb = new JProgressBar();
                JTextArea ta = new JTextArea(10, 20);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(ta));
                frame.add(pb, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                new BackgroundWorker(ta, pb).execute();
            }
        });
    }

    public class BackgroundWorker extends SwingWorker<Void, String> {

        private JProgressBar pb;
        private JTextArea ta;

        public BackgroundWorker(JTextArea ta, JProgressBar pb) {
            this.pb = pb;
            this.ta = ta;
            addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if ("progress".equalsIgnoreCase(evt.getPropertyName())) {
                        BackgroundWorker.this.pb.setValue(getProgress());
                    }
                }

            });
        }

        @Override
        protected void done() {
        }

        @Override
        protected void process(List<String> chunks) {
            for (String text : chunks) {
                ta.append(text);
                ta.append("\n");
            }
        }

        @Override
        protected Void doInBackground() throws Exception {
            for (int index = 0; index < 100; index++) {
                publish("Line " + index);
                setProgress(index);
                Thread.sleep(125);
            }
            return null;
        }
    }
}

摆动计时器示例

正如 Andrew 所建议的,以下是使用SwingTimer. 如果你觉得它有用,请把这个想法归功于 Andrew

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestProgress {

    public static void main(String[] args) {
        new TestProgress();
    }

    private int index = 0;

    public TestProgress() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JProgressBar pb = new JProgressBar();
                final JTextArea ta = new JTextArea(10, 20);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(ta));
                frame.add(pb, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Timer timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        index++;
                        if (index >= 100) {
                            ((Timer)(e.getSource())).stop();
                        }
                        ta.append("Line " + index + "\n");
                        pb.setValue(index);
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
        });
    }
于 2013-05-19T10:09:35.440 回答