让JTextPane滚动 [英] getting JTextPane to scroll

查看:413
本文介绍了让JTextPane滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使JTextPane滚动?在此示例中,使用了JScrollPane,但随着运行代码的显示,滚动条可以显示,但不起作用.

How do I get JTextPane to scroll? In this example, JScrollPane is employed but as running the code will reveal, the scroll bar can be displayed but it is not functional.

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.text.*;

public class JTextPaneTester
{
    JTextPane jtp = new JTextPane();
    StyledDocument doc;
    Style style;
    JScrollPane jsp = new JScrollPane(jtp);

    JTextPaneTester()
    {
        doc = (StyledDocument)jtp.getDocument();
        style = doc.addStyle("fancy", null);
        jsp.setVerticalScrollBarPolicy(
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    }

    void append(String s)
    {
        try
        {
            doc.insertString(doc.getLength(), s, style);
        }
        catch (BadLocationException e) { assert false: "problem"; }
    }

    public static void main(String[] args)
    {
        JTextPaneTester thing = new JTextPaneTester();

        for (int i = 0; i < 100; i++)
            thing.append("nouns verbs adjectives \n");

        JFrame f = new JFrame();
        JPanel center = new JPanel();
        f.add(center, BorderLayout.CENTER);

        center.add(thing.jsp);
        f.setSize(400, 400);
        f.setVisible(true);
    }
}

推荐答案

如果放置代码段

for (int i = 0; i < 100; i++) {
      thing.append("nouns verbs adjectives \n");
}

之后

f.setVisible(true);

似乎可行.

这篇关于让JTextPane滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆