垂直滚动条和视口定位

时间:2012-11-20 17:17:30

标签: java jscrollpane

我正在将JTextPane与JScrollPane结合使用。 JTextPane预先填充了大量需要滚动阅读的文本。我遇到的问题是视口默认显示文本的最底部 - 即当JFrame打开或刷新时,垂直滚动条旋钮位于底部(而不是顶部)。我之前使用过JScrollPane时没有遇到过这个问题。

这是我用来创建滚动窗格的代码: -

    JTabbedPane jtpCentre = new JTabbedPane();

    JPanel panHexagramText = new JPanel();
    panHexagramText.setBorder(bor);
    txpHexagramText = new JTextPane();
    txpHexagramText.setPreferredSize(new Dimension(595, 300));
    scrHTPanel = new JScrollPane(txpHexagramText,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);      
    scrHTPanel.setPreferredSize(new Dimension(595,300));

    panHexagramText.add(scrHTPanel);

我用来填充文本窗格的代码位于不同的类中:

     try {
        String strLineText = this.createLineTextString(strLineTextData);
        String[] strFullText = {"Hexagram Text\n\n",
            strTranslationData[0][1],
            "\n\nLine Text",
            strLineText};
        String[] strStyles = {"bold", "regular", "bold", "regular"};
        gui.txpHexagramText.setText(null);
        StyledDocument sDoc = gui.txpHexagramText.getStyledDocument();
        this.addStylesToDoc(sDoc);
        for (int i = 0; i < strFullText.length; i++) {
            sDoc.insertString(sDoc.getLength(), strFullText[i],
                    sDoc.getStyle(strStyles[i]));
        }
    } catch (Exception ex) {// Trap error if there is no translation available.
        gui.txpHexagramText.setText(str);
    }

我想问为什么要这样做,以及我如何纠正它?

非常感谢。

0 个答案:

没有答案