Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在滚动窗格上滚动-Java Swing_Java_Swing_Jscrollpane_Jtextarea - Fatal编程技术网

无法在滚动窗格上滚动-Java Swing

无法在滚动窗格上滚动-Java Swing,java,swing,jscrollpane,jtextarea,Java,Swing,Jscrollpane,Jtextarea,我有一个Swing应用程序,它在将文本区域的内容传递到GUI后立即用字符串更新文本区域的内容 我在文本区域中实时更新字符串,但我现在面临的问题是,当进程仍在运行时,我无法在文本区域中滚动字符串 有人知道这里发生了什么或者有解决办法吗 尝试1- public static void appendToJTextArea(String message){ String currentText = GUI.textLogArea.getText(); String newString =

我有一个Swing应用程序,它在将文本区域的内容传递到GUI后立即用字符串更新文本区域的内容

我在文本区域中实时更新字符串,但我现在面临的问题是,当进程仍在运行时,我无法在文本区域中滚动字符串

有人知道这里发生了什么或者有解决办法吗

尝试1-

public static void appendToJTextArea(String message){
    String currentText = GUI.textLogArea.getText();
    String newString = message;
    String newTextToAppend = currentText + "\n" + newString;
    GUI.textLogArea.setText(newTextToAppend);
    GUI.frame.update(GUI.frame.getGraphics());
}
尝试2-

    public static void appendToJTextArea(String message){
        Runnable runnable = new LogThread(message);
        Thread thread = new Thread(runnable);
        thread.start();

    /**
        String currentText = GUI.textLogArea.getText();
        String newString = message;
        String newTextToAppend = currentText + "\n" + newString;
        GUI.textLogArea.setText(newTextToAppend);
        GUI.frame.update(GUI.frame.getGraphics());
        **/
    }
}

    class LogThread implements Runnable {
        private String test;

        public LogThread(String message){
            test = message;
        }
        public void run() {
            update(test);
        }

    private void update(String message) {
        System.out.println("BBB"+GUI.textLogArea.getText());
        System.out.println("AAA"+message);

        String currentText = GUI.textLogArea.getText();
        String newString = message;
        String newTextToAppend = currentText + "\n" + newString;
        GUI.textLogArea.append(newTextToAppend);
    }
带有滚动窗格的GUI类

JPanel panel_1 = new JPanel();
panel_1.setBounds(361, 40, 390, 305);
contentPane.add(panel_1);
panel_1.setLayout(null);

JLabel lblScraperLogs = new JLabel("Scraper Logs");
lblScraperLogs.setBounds(0, 0, 390, 31);
lblScraperLogs.setFont(new Font("Cooper Black", Font.PLAIN, 13));
lblScraperLogs.setHorizontalAlignment(SwingConstants.CENTER);
panel_1.add(lblScraperLogs);        

textLogArea = new JTextArea();
textLogArea.setEditable(false);
textLogArea.setBounds(10, 23, 380, 282);

JScrollPane scroll = new JScrollPane(textLogArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setBounds(10, 23, 380, 282);
panel_1.add(scroll);
但我现在面临的问题是,当进程仍在运行时,我无法滚动文本区域

听起来您的长时间运行的进程正在
事件调度线程(EDT)
上执行。此线程负责绘制GUI,因此在进程执行完成之前,GUI无法重新绘制自身。不要在EDT上运行代码


相反,您需要为长时间运行的进程创建一个单独的
线程。可以使用
SwingWorker
,它创建一个单独的线程,并允许您在数据可用时“发布”数据。有关EDT和SwingWorker的更多信息,请阅读上Swing教程中的部分。

GUI.frame.getGraphics()的功能是什么?你不应该像我尝试使用SwingWorker类那样直接访问graphics对象,但我似乎无法让它工作。我使用了可运行的线程,但我也无法让它们更新滚动窗格/@user1798578,不确定我能提供什么帮助。本教程提供了一个可供下载和测试的工作示例。您还可以尝试使用
SwingWorker
publish(…)
方法在论坛中搜索其他示例。