我的程序需要实现一个功能,就是从一个txt中读入文本到TextArea中,将TextArea放入滚动面板使其可以滚动,运行后若文本长度超过一定长度会自动显示滚动条
现在的问题是运行程序后如果文本长度过长,滚动条的初始位置是在最下方,光标定位到文本末端
部分代码如下求教如何使滚动条一开始在最上方 String readMe = "";
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\INSTALL.LOG");
if(file.exists()) {
try {
BufferedReader input = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;
while((text = input.readLine())!=null)
buffer.append(text+"\n");
input.close();
readMe = buffer.toString();
} catch(IOException ioException) {}
}
textArea.setText(readMe);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPanel = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
现在的问题是运行程序后如果文本长度过长,滚动条的初始位置是在最下方,光标定位到文本末端
部分代码如下求教如何使滚动条一开始在最上方 String readMe = "";
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\INSTALL.LOG");
if(file.exists()) {
try {
BufferedReader input = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;
while((text = input.readLine())!=null)
buffer.append(text+"\n");
input.close();
readMe = buffer.toString();
} catch(IOException ioException) {}
}
textArea.setText(readMe);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPanel = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
或者直接设为0,但Minimum可能不为0,虽然大多数情况下我们都设为0,或者说组件默认设为0
JScrollPane.getVerticalScrollBar().setValue(0);
水平滚动条用这个:JScrollPane.getHorizontalScrollBar().
private JTextArea textArea;
private JScrollPane scrollPanel;
public Help(String title) {
super(title);
textArea = new JTextArea(); String readMe = "";
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\test.txt");
if(file.exists()) {
try {
BufferedReader input = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;
while((text = input.readLine())!=null)
buffer.append(text+"\n");
input.close();
readMe = buffer.toString();
} catch(IOException ioException) {}
}
textArea.setText(readMe);
scrollPanel = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.add(scrollPanel);
this.setSize(300,600);
this.setLocation(400, 50);
}我把其他代码都注释掉了,就剩这样最简单的跑滚动条还是在最下面- -我也搞不懂了
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import static javax.swing.ScrollPaneConstants.*;public class JTextAreaExample extends JFrame{
private JTextArea textarea = new JTextArea();
private DefaultEditorKit kit = new DefaultEditorKit();
public JTextAreaExample(){
super("JTextArea Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
getContentPane().add(new JScrollPane(textarea,VERTICAL_SCROLLBAR_AS_NEEDED,HORIZONTAL_SCROLLBAR_NEVER));
setPreferredSize(new Dimension(800,600));
pack();
setLocationRelativeTo(null);
addWindowListener(new WindowAdapter(){
@Override public void windowOpened(WindowEvent evt){
try{
kit.read(new java.io.FileReader("./JTextAreaExample.java"),textarea.getDocument(),0);
textarea.setCaretPosition(0);
}catch(java.io.IOException e){
textarea.setText(e.getMessage());
}catch(BadLocationException e){
}
}
});
} public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
new JTextAreaExample().setVisible(true);
}
});
}
}
textArea.setCaretPosition(0);
哈哈,万分感谢