我有JTextArea text和JScrollPane pane=new JScrollPane(文本),我放入pane.setAutoScrolls(真)。当我将一些文本附加到我的组件文本后,窗格在末尾(最后一行)滚动时,如何获得它?
发布于 2011-05-09 09:35:59
跟随来自此线程ScrollPane scroll to bottom problem的链接
发布于 2011-05-09 10:08:06
Rob Camick对如何移动插入符号的最好(也是最新的)解释:
http://tips4java.wordpress.com/2008/10/22/text-area-scrolling/
发布于 2011-08-29 05:45:06
有没有可能,你不在东部夏令时?如果附加没有发生在EDT上,则JTextArea的位置不会更新。
演示此行为的简短、可运行的示例:
import java.awt.TextArea;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Sample {
public static void main(String[] args) {
/*
* Not on EDT
*/
showAndFillTextArea("Not on EDT", 0, 0);
/*
* On EDT
*/
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
showAndFillTextArea("On EDT", 400, 0);
}
});
}
private static void showAndFillTextArea(String title, int x, int y) {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(20, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocation(x, y);
frame.setVisible(true);
for(int i = 0; i < 50; i++) {
textArea.append("Line" + i + "\n");
}
}
}
https://stackoverflow.com/questions/5934975
复制