就是当JTextArea中的数据增加的时候,滚动条自动增加了,怎么实现啊各位高手们!!

解决方案 »

  1.   

    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();
    }
    }
      

  2.   

    你的意思是,JTextArea中添加内容时,没有换行,自动加载scrolBar?如果是这样的话setAutoscrolls(false);这个方法就可以解决把
      

  3.   

    JFrame f = new JFrame();
    Container con = f.getContentPane();
    JTextArea t = new JTextArea(3,3);
    con.add(new JScrollPane(t));
      

  4.   

    把JTextArea放到JScrollPane中就可以了。可以把该JTextArea作为JScrollPane的构造函数的参数
      

  5.   

    textArea.setCaretPosition(textArea.getDocument().getLength()-1);