JScrollBar的样式文档?

时间:2014-09-11 06:16:19

标签: java swing document jtextpane 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);

1 个答案:

答案 0 :(得分:1)

如文档How to Use Scroll Panes中所述,

  

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