Java JScrollBar的样式化文档?

Java JScrollBar的样式化文档?,java,swing,document,jtextpane,jscrollbar,Java,Swing,Document,Jtextpane,Jscrollbar,我想自定义滚动条对象的输出。下面的代码适用于JTextPane,JScrollBar的等效代码是什么 private JTextPane textPane; StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet keyWord = new SimpleAttributeSet(); StyleConstants.setForeground(keyWord, Color.RED

我想自定义滚动条对象的输出。下面的代码适用于JTextPane,JScrollBar的等效代码是什么

    private JTextPane textPane;

    StyledDocument doc = textPane.getStyledDocument();

    SimpleAttributeSet keyWord = new SimpleAttributeSet();
    StyleConstants.setForeground(keyWord, Color.RED);
    StyleConstants.setBackground(keyWord, Color.YELLOW);
    StyleConstants.setBold(keyWord, true);

如文件中所述

JScrollPane提供组件的可滚动视图。当屏幕不动产受到限制时,使用滚动窗格显示较大的组件或其大小可以动态更改的组件

。。下面的代码创建文本区域,使其成为滚动窗格的客户端,并将滚动窗格添加到容器中

因此,您可以将JTextPane作为JScrollBar的客户机

示例代码:
JScrollBar没有文档。。。它没有文本表示,因此需要文档。。。也许您可以将JScrollBar子类化并在其中插入您自己的文档?
public class Sample extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sample frame = new Sample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Sample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 300, 300);
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout(0, 0));
        setContentPane(panel);

        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.add(textPane);
        scrollPane.setViewportView(textPane);
        panel.add(scrollPane, BorderLayout.CENTER);

        final StyledDocument doc = textPane.getStyledDocument();
        insertStringToDoc(getString() + "\n", doc, doc.getLength());

    }

    private void insertStringToDoc(String str, StyledDocument doc, int offset) {
        try {

            SimpleAttributeSet keyWord = new SimpleAttributeSet();
            StyleConstants.setForeground(keyWord, Color.RED);
            StyleConstants.setBackground(keyWord, Color.YELLOW);
            StyleConstants.setBold(keyWord, true);
            doc.insertString(doc.getLength(), str, keyWord);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getString() {
        return "hello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello "
                + "\nhello hello hello hello hello hello hello hello hello hello hello ";
    }
}