JScrollBar的样式化文档



我想自定义滚动条对象的输出。下面的代码非常适用于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的客户端。

示例代码:

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 ";
    }
}

相关内容

  • 没有找到相关文章

最新更新