Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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-如何使用文档在JTextPane中添加文本_Java_Jtextpane - Fatal编程技术网

Java-如何使用文档在JTextPane中添加文本

Java-如何使用文档在JTextPane中添加文本,java,jtextpane,Java,Jtextpane,我想在Java应用程序中创建一个控制台(JTextPane),以显示我的应用程序在做什么。我尝试了很多次使用不同的方法,但都失败了…下面是我的部分代码 主类 private final Form form; println("Downloading files..."); public void println(String line) { System.out.println(line); form.getConsole().print(line); }

我想在Java应用程序中创建一个控制台(JTextPane),以显示我的应用程序在做什么。我尝试了很多次使用不同的方法,但都失败了…下面是我的部分代码

主类

private final Form form;

println("Downloading files...");

public void println(String line)
{
        System.out.println(line);
        form.getConsole().print(line);
}
表格(图形用户界面)

选项卡控制台

public void print(final String line) {
        if (!SwingUtilities.isEventDispatchThread()) {
          SwingUtilities.invokeLater(new Runnable()
          {
            public void run() {
              TabConsole.this.print(line);
            }
          });
          return;
        }

        Document document = this.console.getDocument();
        JScrollBar scrollBar = getVerticalScrollBar();
        boolean shouldScroll = false;

        if (getViewport().getView() == this.console) {
          shouldScroll = scrollBar.getValue() + scrollBar.getSize().getHeight() + MONOSPACED.getSize() * 4 > scrollBar.getMaximum();
        }
        try
        {
          document.insertString(document.getLength(), line, null);
        } catch (BadLocationException localBadLocationException) {
        }
        if (shouldScroll)
          scrollBar.setValue(2147483647);
      }

我的密码有问题吗?感谢您的帮助。

将BadLocationException作为RuntimeException重新显示,尽管代码看起来是正确的。你犯了什么错误


如果文本未显示,则可能是更新了错误的文档。请确保您正在更新TextArea所使用的文档,或任何用于显示内容的内容。

ya,我以前也尝试过RuntimeException,但我得到了0错误。和。。我使用的是JTextPane而不是JTextArea如何获取对文档的引用?引用是什么意思?这个.console.getDocument()看起来像什么?哦,JTextPane(控制台)中没有文本
public void print(final String line) {
        if (!SwingUtilities.isEventDispatchThread()) {
          SwingUtilities.invokeLater(new Runnable()
          {
            public void run() {
              TabConsole.this.print(line);
            }
          });
          return;
        }

        Document document = this.console.getDocument();
        JScrollBar scrollBar = getVerticalScrollBar();
        boolean shouldScroll = false;

        if (getViewport().getView() == this.console) {
          shouldScroll = scrollBar.getValue() + scrollBar.getSize().getHeight() + MONOSPACED.getSize() * 4 > scrollBar.getMaximum();
        }
        try
        {
          document.insertString(document.getLength(), line, null);
        } catch (BadLocationException localBadLocationException) {
        }
        if (shouldScroll)
          scrollBar.setValue(2147483647);
      }