package test;/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
import java.awt.AWTEvent;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class ScrollBottom extends JFrame{
   private JTextArea ta = new JTextArea();
   public ScrollBottom(){
     super();
     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
     init();
   }   private void init(){
     setBounds(0, 0, 300, 150);     final JScrollPane sp = new JScrollPane(ta);
     getContentPane().add(sp, "Center");     ta.setText("Start.....");
     
     
     for(int i=0;i<=10;i++){
         ta.append(i+"\n");
     }
     
//     TimerTask task = new TimerTask(){
//       public void run(){
//         ta.append("Time: " + (System.currentTimeMillis() / 1000));
//        scrollAndSetCursor();
//       }
//     };
    }   /** 该方法用于实现滚动以及光标相关效果 */
  public void scrollAndSetCursor(){
    // ta.requestFocus();
    ta.setSelectionStart(ta.getText().length());
  }   protected void processWindowEvent(WindowEvent e){
     super.processWindowEvent(e);
     if(e.getID() == WindowEvent.WINDOW_CLOSING)
       System.exit(0);
   }   public static void main(String[] args){
     ScrollBottom scroll = new ScrollBottom();
     scroll.setVisible(true);
   }
}
我动态的向JTextArea 里面添加数据
可是为什么这个滚动条不跟着向下滚动呢···
请高手解答 ····
谢谢····

解决方案 »

  1.   

    (1)在JTextArea插入最后一条消息之后,使用selectAll()将光标强制移动到JTextArea的最后,实现滚动条的自动滚动。(Aviva中采用的方式)(2)在JTextArea插入最后一条消息之后,使用(JTextArea)recvArea.setCaretPosition(recvArea.getText().length()),将光标移到最后,实现滚动条的自动滚动。(3)在JTextArea加载了自动滚动条JScroll之后,将JTextArea加入到JScrolPanel的ViewPort中: (有一些Bug,使得图像有点闪烁)
    recvScrollPane.getViewport().add(recvArea, null);
    然后在JTextArea插入最后一条新消息之后,将滚动条的Viewport重新设置到最底端的位置:
    int height = 20;
    Point p = new Point();
    p.setLocation(0, recvArea.getLineCount() * height);
    recvScrollPane.getViewport().setViewPosition(p);import java.awt.Rectangle;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;public class ScrollTextArea extends JFrame {    private static final long serialVersionUID = 1L;    private JPanel jContentPane = null;    private JScrollPane jScrollPane = null;    private JTextArea jTextArea = null;    /**
        * This method initializes jScrollPane
        *
        * @return javax.swing.JScrollPane
        */
        private JScrollPane getJScrollPane() {
            if (jScrollPane == null) {
                jScrollPane = new JScrollPane();
                jScrollPane.setBounds(new Rectangle(0, 2, 290, 144));
                jScrollPane.setViewportView(getJTextArea());
            }
            return jScrollPane;
        }    /**
        * This method initializes jTextArea
        *
        * @return javax.swing.JTextArea
        */
        private JTextArea getJTextArea() {
            if (jTextArea == null) {
                jTextArea = new JTextArea();
            }
            return jTextArea;
        }    /**
        * @param args
        */
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ScrollTextArea thisClass = new ScrollTextArea();
                    thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    thisClass.setVisible(true);
                }
            });
        }    /**
        * This is the default constructor
        */
        public ScrollTextArea() {
            super();
            initialize();
        }    /**
        * This method initializes this
        *
        * @return void
        */
        private void initialize() {
            this.setSize(300, 200);
            this.setContentPane(getJContentPane());
            this.setTitle("JFrame");
        }    /**
        * This method initializes jContentPane
        *
        * @return javax.swing.JPanel
        */
        private JPanel getJContentPane() {
            if (jContentPane == null) {
                jContentPane = new JPanel();
                jContentPane.setLayout(null);
                jContentPane.add(getJScrollPane(), null);
            }
            return jContentPane;
        }
      

  2.   

    这样就行了    private void init()
        {
            setBounds(0, 0, 300, 150);
            
            final JScrollPane sp = new JScrollPane(ta);
            getContentPane().add(sp, "Center");
            
            for (int i = 0; i <= 10; i++)
            {
                ta.append(i + "\n");
            }
            
            JScrollBar   sb   =   sp.getVerticalScrollBar();   
            sb.setValue(sb.getMaximum());           //    TimerTask task = new TimerTask(){ 
            //      public void run(){ 
            //        ta.append("Time: " + (System.currentTimeMillis() / 1000)); 
            //        scrollAndSetCursor(); 
            //      } 
            //    }; 
        }
      

  3.   

    谢谢 shuanlarousi1和nine_suns99问题解决了
    shuanlarousi1我想还没有明白我的意思··
    我想要的是动态添加··然后放滚动条会显示滚动
    你那样只是获得光标,但是不能动态添加nine_suns99的代码拿走了··谢谢··很好用···