使用JTextArea和JScrollPane怎么实现滚动条自动下滚???? 就是当JTextArea中的数据增加的时候,滚动条自动增加了,怎么实现啊各位高手们!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.BorderLayout;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.Timer;import javax.swing.text.BadLocationException;public class AutoScrollTest{ public static void main(String[] args) { final JTextArea textArea = new JTextArea(); final JScrollPane scrollPane = new JScrollPane(textArea); JFrame f = new JFrame(); f.getContentPane().add(scrollPane, BorderLayout.CENTER); f.setSize(400, 300); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); Timer timer = new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.append(System.currentTimeMillis() + "\n"); textArea.setCaretPosition(textArea.getDocument().getLength()-1); //或者用下面的方法 //int lineCount = textArea.getLineCount(); //try { // int pos = textArea.getLineStartOffset(lineCount-1); // Rectangle rect = textArea.modelToView(pos); // textArea.scrollRectToVisible(rect); //} catch (BadLocationException ex) { // ex.printStackTrace(); //} } }); timer.start(); }} 你的意思是,JTextArea中添加内容时,没有换行,自动加载scrolBar?如果是这样的话setAutoscrolls(false);这个方法就可以解决把 JFrame f = new JFrame();Container con = f.getContentPane();JTextArea t = new JTextArea(3,3);con.add(new JScrollPane(t)); 把JTextArea放到JScrollPane中就可以了。可以把该JTextArea作为JScrollPane的构造函数的参数 textArea.setCaretPosition(textArea.getDocument().getLength()-1); 一道面试智力题! tableviewer中如何添加进度条 关于do while应用的一个实际问题? java基础的基础 java写的代码怎么制作成dll,能做吗? 关与带标号的continue新手问题请多帮忙 ★★★百分求一个类,实现集合接口(Set)的一个类! 关于接口的疑问 KeyEvent类的疑问 为Java 应用程序提高运行速度谁有高招? 求助:JAVA读取.txt文件的问题 怎么才能让图像的背景透明
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.text.BadLocationException;public class AutoScrollTest
{
public static void main(String[] args)
{
final JTextArea textArea = new JTextArea();
final JScrollPane scrollPane = new JScrollPane(textArea);
JFrame f = new JFrame();
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e)
{
textArea.append(System.currentTimeMillis() + "\n"); textArea.setCaretPosition(textArea.getDocument().getLength()-1);
//或者用下面的方法
//int lineCount = textArea.getLineCount();
//try {
// int pos = textArea.getLineStartOffset(lineCount-1);
// Rectangle rect = textArea.modelToView(pos);
// textArea.scrollRectToVisible(rect);
//} catch (BadLocationException ex) {
// ex.printStackTrace();
//}
}
});
timer.start();
}
}
Container con = f.getContentPane();
JTextArea t = new JTextArea(3,3);
con.add(new JScrollPane(t));