为什么jtextpane的getHeight()方法返回的高度一直不变.? content is an component, so content.getHeight() returned the height of this component. It is not the height of the text contents. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 It's not easy to calculate the height. You may use styleconstants to calculate.Here is a alternative solution.content.setCaretPosition(content.getText().getLength()); 有没有一个jtextpane配合滚动条滚动的例子啊?content.setCaretPosition(content.getText().getLength());好像也不行。是不是滚动条被限制了啊? sorry, should becontent.setCaretPosition(content.getText().length());I tested, it's ok 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 /** * @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); }} also solve your我只是想看到jtextpane中超出窗口可以看到的那部分。就这么难吗?滚不下去 用setSelectionStart(jtextpanetextleng)方法设置到最后一行 JSP动态显示列(用户可以根据需求显示列)(方案或代码) java新手,请指点! 打包!! 常见数据库JDBC连接 关于File中delete方法的异常问题~~ io流的一道代码填空题目,大家帮忙看一下~~~ java 如何判断一个程序正在执行? 关于package包的问题,在使用javabean时,老出问题,不知道什么原因?在先等! 急!!!关于菜单的问题:怎样知道某一菜单项的在该菜单中的位置序号? jdk缺失?AOSP源码Parcel类引用jdk中没有的方法 一个正则表达式的问题,解决后马上结贴 一个阶乘的问题
Here is a alternative solution.content.setCaretPosition(content.getText().getLength());
好像也不行。是不是滚动条被限制了啊?
I tested, it's ok
点一下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
* @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);
}
}
我只是想看到jtextpane中超出窗口可以看到的那部分。就这么难吗?滚不下去