Java: I can't run JTextArea multiple times?

前端 未结 1 1570
误落风尘
误落风尘 2020-12-22 05:46

Hello and welcome everyone, this is my first question so I hope that it is a good one. I was exploring the swing API and I came across a question that popped in my head. I b

相关标签:
1条回答
  • 2020-12-22 06:31

    Welcome to blocking the Event Dispatching Thread.

    The EDT is responsible, for among other things, processing repainting requests. You should never performing any operations that block the EDT, instead, you should using something like a SwingWorker

    Take a look at Concurrency in Swing for more details

    Updated with example

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingWorker;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestTextArea03 {
    
        public static void main(String[] args) {
            new TestTextArea03();
        }
    
        public TestTextArea03() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class TestPane extends JPanel {
    
            private JTextArea textArea;
    
            private TestPane() {
                setLayout(new BorderLayout());
                textArea = new JTextArea(10, 10);
                add(new JScrollPane(textArea));
    
                new TextWorker().execute();
            }
    
            public class TextWorker extends SwingWorker<Void, String> {
    
                @Override
                protected void process(List<String> chunks) {
                    for (String text : chunks) {
                        textArea.append(text + "\n");
                    }
                }
    
                @Override
                protected Void doInBackground() throws Exception {
                    Thread.sleep(1000);
                    for (int x = 0; x < 10; x++) {
                        publish(String.valueOf(x));
                        Thread.sleep(250);
                    }
                    return null;
                }
    
            }
    
        }
    
    }
    

    Or a Swing Timer

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    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 TestTextArea03 {
    
        public static void main(String[] args) {
            new TestTextArea03();
        }
    
        public TestTextArea03() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class TestPane extends JPanel {
    
            private JTextArea textArea;
            private int x;
    
            private TestPane() {
                setLayout(new BorderLayout());
                textArea = new JTextArea(10, 10);
                add(new JScrollPane(textArea));
    
                Timer timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        x++;
                        textArea.append(String.valueOf(x) + "\n");
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }        
        }    
    }
    
    0 讨论(0)
提交回复
热议问题