GUI - 显示行逐一JTextPane中,并作出相关的JProgressBar(GUI - Di

2019-10-18 03:40发布

如何显示的行逐一上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); 但我不知道在哪里把它为了达到我的目标。

Answer 1:

不要挡住EDT(事件指派线程) - 图形用户界面将“冻结”当这种情况发生。 而不是调用的Thread.sleep(n)实现一个Swing Timer用于重复任务或SwingWorker长时间运行的任务。 见并发Swing中的更多细节。



Answer 2:

你要使用SwingWorker 。 它可以让你在后台执行更新,关闭事件指派线程。

该SwingWorker类有方法可以让你publishprocess更新回到事件指派线程。

它还可以让你火和监控进度更新

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;
        }
    }
}

摆动定时器实例

作为建议由安德鲁,下面是使用的例子SwingTimer 。 如果你觉得有用,请给安德鲁信用为理念

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();
            }
        });
    }


文章来源: GUI - Display lines one by one on a JTextPane and make a related JProgressBar