我想问的是
我用了一个jtextarea,嵌在一个jscrollpane里面
我想让它实现信息的输出,现在的问题是,信息输出到最下面一行之后,滚动条不自动出现,而是卡在那里,等到所有信息输出完了之后才一下子出现
怎么能让滚动条自动出现,然后信息一行一行的输出并且始终显示在最下面一行呢

解决方案 »

  1.   

    在网上找了滚动到最后行的代码,然后加了按钮每次增加一行,你参考看看.package 内部类;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Demo extends JFrame {
    JTextPane textPane = new JTextPane();
    JTextArea ja = new JTextArea(); public Demo() {
    super("JTextPane Demo");
    getContentPane().setLayout(new BorderLayout());
    final JButton buttonHome = new JButton("Go to Home");
    final JButton buttonEnd = new JButton("Go to End");
    final JButton buttonAdd = new JButton("增加行");
    ActionListener positionHandler = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == buttonHome) {
    textPane.setCaretPosition(0);
    textPane.requestFocus();
    } else if (e.getSource() == buttonEnd) {
    textPane.selectAll();
    textPane.setCaretPosition(textPane.getSelectedText()
    .length());
    textPane.requestFocus();
    }else if (e.getSource() == buttonAdd) {
    textPane.setText(textPane.getText()+"aaaaaa\n");
    textPane.selectAll();
    textPane.setCaretPosition(textPane.getSelectedText()
    .length());
    }
    }
    };
    buttonHome.addActionListener(positionHandler);
    buttonEnd.addActionListener(positionHandler);
    buttonAdd.addActionListener(positionHandler); JPanel paneNorth = new JPanel();
    JPanel paneSouth = new JPanel(); paneSouth.add(buttonHome);
    paneSouth.add(buttonEnd);
    paneSouth.add(buttonAdd); getContentPane().add(paneSouth, BorderLayout.SOUTH); paneNorth.setLayout(new BorderLayout());
    paneNorth.add(new JScrollPane(textPane), BorderLayout.CENTER);
    getContentPane().add(paneNorth, BorderLayout.CENTER); setSize(300, 400);
    show(); } public static void main(String[] args) {
    Demo app = new Demo();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  2.   

    不应该出现这个问题啊,你看看我下面的代码,都是自动到最后一行的。你把你的代码贴出来看下。
    import java.awt.*;
    import javax.swing.*;public class TestJTextArea extends JFrame{
    private JTextArea textArea;
    private JScrollPane scrollPane;

    public TestJTextArea(){
    super("TestJTextArea");

    textArea = new JTextArea(200, 150);

    scrollPane = new JScrollPane(textArea);
    scrollPane.setPreferredSize(new Dimension(200, 150));

    add(scrollPane);
    setSize(400, 300);
    setVisible(true);
    }

    public static void main(String args[]){
    new TestJTextArea();
    }
    }
      

  3.   

    没必要这样吧 JScrollPane会自动往下滚的 
      

  4.   

    为什么不直接用textArea.append(str)?然后textArea.setCaretPosition(textArea.getText().length())。这样不行吗?
      

  5.   

    顶一楼,这句是关键噢 new JScrollPane(textPane)
    否则textArea也不会跟着你加上的滚动条走
      

  6.   

    如果你往文本域里写内容的方法要执行很长的时间的话就能看出效果来了,如果是单线程的当主程序转入往文本域写内容的方法中去执行时,界面中的jscrollpane就不能监听到文本域内容的变化了,只能在主程序转回到界面时才能监听到文本域的变化。也就会出现lz所说的情况了。
    “信息输出到最下面一行之后,滚动条不自动出现,而是卡在那里,等到所有信息输出完了之后才一下子出现 “
    放到另一个线程上执行的时候情况主不一样了,界面中的jscrollpane时刻在监听,文本域的变化,所以就在时刻滚动了。
      

  7.   

    写了两个测试程序
    1.会出现楼主说的不滚动的情况public class TestJScrollPane extends JFrame { public static JTextArea jt=new JTextArea(10,10);
    public TestJScrollPane()
    {
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JScrollPane jp =new JScrollPane(jt);
    this.getContentPane().setLayout(new FlowLayout());
    JButton button=new JButton("添加内容");
    button.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0) {
    //添加内容的方法
    SetContent sc=new SetContent();
    sc.run();
    }
    }
    );
    this.add(jp);
    this.add(button);
    }
    public static void main(String[] args) {
    TestJScrollPane tjp=new TestJScrollPane();
    tjp.setSize(400, 400);
    tjp.setTitle("TestJScrollPane");
    tjp.setVisible(true);
    }}package lession8;public class SetContent/* extends Thread*/ { public void run(){
    int count =0;
    while(true)
    {
    TestJScrollPane.jt.append("hello world\n");
    TestJScrollPane.jt.paintImmediately(TestJScrollPane.jt.getBounds());
    try {
    Thread.sleep(1000);
    count++;
    if(count>20)
    {
    break;
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    }2.实现了多线程可以滚动了
    package lession8;import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;public class TestJScrollPane extends JFrame { public static JTextArea jt=new JTextArea(10,10);
    public TestJScrollPane()
    {
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JScrollPane jp =new JScrollPane(jt);
    this.getContentPane().setLayout(new FlowLayout());
    JButton button=new JButton("添加内容");
    button.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0) {
    //添加内容的方法
    SetContent sc=new SetContent();
    sc.start();
    }
    }
    );
    this.add(jp);
    this.add(button);
    }
    public static void main(String[] args) {
    TestJScrollPane tjp=new TestJScrollPane();
    tjp.setSize(400, 400);
    tjp.setTitle("TestJScrollPane");
    tjp.setVisible(true);
    }}package lession8;public class SetContent extends Thread { public void run(){
    int count =0;
    while(true)
    {
    TestJScrollPane.jt.append("hello world\n");
    TestJScrollPane.jt.paintImmediately(TestJScrollPane.jt.getBounds());
    try {
    this.sleep(1000);
    count++;
    if(count>20)
    {
    break;
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    }
      

  8.   

    jtextarea,嵌在一个jscrollpane里面,应该是自动滚动的设置插入符位置
    textArea.setCaretPosition(textArea.getText().length())