如何将JScrollPane中的滚动条设置为顶部?使用borderlayout。栏总是滚动到我的内容的底部。

时间:2015-01-23 19:03:02

标签: eclipse jscrollpane jtextarea

//创建了一个JScrollPane,当我运行它时,滚动条将自动转到我的textarea的最底部,但我需要它在顶部。 先感谢您! =)

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 86, 262, 57);
content.add(scrollPane);

//使用的文字区域

JTextArea txtrTryingTryingTrying = new JTextArea();
scrollPane.setViewportView(txtrTryingTryingTrying);
txtrTryingTryingTrying.setText("-------");

1 个答案:

答案 0 :(得分:0)

我一直在尝试,并找到了解决方案。

有一种有用的方法叫做setCaretPosition(int)

使用后

component.setText("Text here");

您必须使用

component.setCaretPosition(0); //0 will set it to the top

在您的情况下,您必须在setCaretPosition()

之后添加setText
JTextArea txtrTryingTryingTrying = new JTextArea();
scrollPane.setViewportView(txtrTryingTryingTrying);
txtrTryingTryingTrying.setText("-------");
txtrTryingTryingTrying.setCaretPosition(0);
相关问题