将JTextArea安在JScrollPane上,JScrollPane便有了滚动条,再用setLineWrap(true)让其超过横向显示区域后自动换行,请问如何实现纵向滚动条随JTextArea内容增加而自动向下移动呢?望哥哥姐姐们帮忙。

解决方案 »

  1.   

    private JTextArea
        t1 = new JTextArea("t1", 1, 20),
        t2 = new JTextArea("t2", 4, 20),
        t3 = new JTextArea("t3", 1, 20),
        t4 = new JTextArea("t4", 10, 10),
        t5 = new JTextArea("t5", 4, 20),
        t6 = new JTextArea("t6", 10, 10);
      private JScrollPane
        sp3 = new JScrollPane(t3,
          JScrollPane.VERTICAL_SCROLLBAR_NEVER,
          JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
        sp4 = new JScrollPane(t4,
          JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
          JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
        sp5 = new JScrollPane(t5,
          JScrollPane.VERTICAL_SCROLLBAR_NEVER,
          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS),
        sp6 = new JScrollPane(t6,
          JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)
       这下知道怎么改了吧!
      

  2.   

    好好看看 JScrollPane的构造函数就行了!
      

  3.   

    问题没解决啊大哥们,怪我自己没说清楚,我是说的用JTextArea的Append(String)方法加入内容,让滚动条自动显示最下面的内容,interhanchi大哥的代码什么都没做啊,我还做了实验的,可能是我愚钝吧,望大哥指点迷津。import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /**
     * Sample application using Frame.
     *
     * @author 
     * @version 1.00 05/09/24
     */
    public class QqqqqqqFrame extends Frame {
        
        /**
         * The constructor.
         */  
             private JTextArea
         t1 = new JTextArea("t1", 1, 20),
         t2 = new JTextArea("t2", 4, 20),
        t3 = new JTextArea("t3", 1, 20),
        t4 = new JTextArea("t4", 10, 10),
        t5 = new JTextArea("t5", 4, 20),
        t6 = new JTextArea("t6", 10, 10);
       private JScrollPane
        sp3 = new JScrollPane(t3,
          JScrollPane.VERTICAL_SCROLLBAR_NEVER,
          JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
        sp4 = new JScrollPane(t4,
          JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
          JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
        sp5 = new JScrollPane(t5,
          JScrollPane.VERTICAL_SCROLLBAR_NEVER,
          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS),
        sp6 = new JScrollPane(t6,
          JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);    
         public QqqqqqqFrame() {
                    
            MenuBar menuBar = new MenuBar();
            Menu menuFile = new Menu();
            MenuItem menuFileExit = new MenuItem();
            
            menuFile.setLabel("File");
            menuFileExit.setLabel("Exit");
            JButton b=new JButton("ddd");
          this.setLayout(new FlowLayout());
          add(sp3);
          add(sp4);
          add(sp5);
          add(sp6);
          add(b);
            
            // Add action listener.for the menu button
            menuFileExit.addActionListener
            (
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        QqqqqqqFrame.this.windowClosed();
                    }
                }
            ); 
            b.addActionListener
            (
             new ActionListener() {
             public void actionPerformed(ActionEvent e) {
             t3.append("123ewseddsdsfdfdfdfdfdsfdfdsf\n");
             t4.append("123ewseddsdsfdfdfdfdfdsfdfdsf\n");
             t5.append("123ewseddsdsfdfdfdfdfdsfdfdsf\n");
             t6.append("123ewseddsdsfdfdfdfdfdsfdfdsf\n");
            
             }
             }
            );
            menuFile.add(menuFileExit);
            menuBar.add(menuFile);
            
            setTitle("Qqqqqqq");
            setMenuBar(menuBar);
            setSize(new Dimension(600, 600));
            
            // Add window listener.
            this.addWindowListener
            (
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        QqqqqqqFrame.this.windowClosed();
                    }
                }
            );  
        }
        
        
        /**
         * Shutdown procedure when run as an application.
         */
        protected void windowClosed() {
        
         // TODO: Check if it is save to close the application
        
            // Exit application.
            System.exit(0);
        }
           public static void main(String[] args) {
            // Create application frame.
            QqqqqqqFrame frame = new QqqqqqqFrame();
            
            // Show frame
            frame.setVisible(true);
        }
    }
      

  4.   

    上面的是我实验的代码,我想要的是实现QQ那样的行为,JTEXTAREA中总是能显示最新加进来的信息,而不需要拖动滚动条。望答复!
      

  5.   

    我再把问题问一遍吧。
    将JTextArea安在JScrollPane上,JScrollPane便有了滚动条,再用setLineWrap(true)让其超过横向显示区域后自动换行,请问如何实现纵向滚动条随JTextArea内容增加而自动向下移动呢?如果直接在JTextArea中输入,纵向滚动条随JTextArea内容增加而自动向下移动没有问题,但我用的是append(str+"\n"),str是String对象,想实现像QQ一样的行为(总是显示最新append进来的内容,当新append进来的内容超过可视范围了,滚动条就自动下移来显示它)。