content is an component, so content.getHeight() returned the height of this component. It is not the height of the text contents.

解决方案 »

  1.   

    It's not easy to calculate the height. You may use styleconstants to calculate.
    Here is a alternative solution.content.setCaretPosition(content.getText().getLength());
      

  2.   

    有没有一个jtextpane配合滚动条滚动的例子啊?content.setCaretPosition(content.getText().getLength());
    好像也不行。是不是滚动条被限制了啊?
      

  3.   

    sorry, should becontent.setCaretPosition(content.getText().length());
    I tested, it's ok
      

  4.   

    http://577.86.cn/_temp/Applet1.html不行,你到上面页面看看
    点一下button4看看,下面是button4中的方法。  void button4_actionPerformed(ActionEvent e) {
        label1.setText(content.getText().length()+"");
        content.setCaretPosition(content.getText().length()-1);  }下面是我的源代码
    http://577.86.cn/_temp/Applet1.java
      

  5.   

    /** 
     * @Author [email protected]
     */import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Demo extends JFrame{
    JTextPane textPane=new JTextPane();
    public Demo(){
    super("JTextPane Demo");
    getContentPane().setLayout(new BorderLayout());

    final JButton buttonHome=new JButton("Go to Home");
    final JButton buttonEnd=new JButton("Go to End");
    ActionListener positionHandler=new ActionListener(){
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==buttonHome){
    textPane.setCaretPosition(0);
    textPane.requestFocus();
    } else if(e.getSource()==buttonEnd){
    textPane.selectAll();
    textPane.setCaretPosition(textPane.getSelectedText().length());
    textPane.requestFocus();
    }
    } };
    buttonHome.addActionListener(positionHandler);
    buttonEnd.addActionListener(positionHandler);

    JPanel paneNorth=new JPanel();
    JPanel paneSouth=new JPanel(); paneSouth.add(buttonHome);
    paneSouth.add(buttonEnd);

    getContentPane().add(paneSouth,BorderLayout.SOUTH); paneNorth.setLayout(new BorderLayout());
    paneNorth.add(new JScrollPane(textPane),BorderLayout.CENTER);
    getContentPane().add(paneNorth,BorderLayout.CENTER); setSize(300,400);
    show(); }
    public static void main(String[] args){
    Demo app=new Demo();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  6.   

    also solve your
    我只是想看到jtextpane中超出窗口可以看到的那部分。就这么难吗?滚不下去
      

  7.   

    用setSelectionStart(jtextpanetextleng)方法设置到最后一行