应该是这样:set会使得TextArea显示到最新内容的底部。

解决方案 »

  1.   

    我用的是JDK1.3.1,没出现你所说的问题,或者说我没看清楚你的意思import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;class ScrollTest extends JFrame implements ActionListener
    {
    private JTextArea txt = new JTextArea();
    private JScrollPane scroll = new JScrollPane(txt);
    private JButton insertButton = new JButton("Insert");
    private JButton appendButton = new JButton("Append");
    private JTextField posText = new JTextField(5);
    private JTextField strText = new JTextField(10);

    public ScrollTest() {
    JPanel p = new JPanel();
    p.add(new JLabel("Insert Position:"));
    p.add(posText);
    p.add(new JLabel("Insert String:"));
    p.add(strText);
    p.add(insertButton);
    p.add(appendButton);
    getContentPane().add(p, BorderLayout.NORTH);
    getContentPane().add(scroll, BorderLayout.CENTER);
    insertButton.addActionListener(this);
    appendButton.addActionListener(this);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(600, 400);
    setTitle("Scroll Test");
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==insertButton)
    txt.insert(strText.getText()+"\n", Integer.parseInt(posText.getText()));
    else
    txt.append(strText.getText()+"\n");


    //txt.getScrollableUnitIncrement(txt.getVisibleRect(),
    // SwingConstants.VERTICAL, 1);
    //scroll.scrollRectToVisible(txt.getVisibleRect());
    } public static void main(String args[]) { new ScrollTest();

    }
    }
      

  2.   

    insert, append都很正常呀?
      

  3.   

    万万不可!!!
    swing对线程是不安全的
    必须用EventQueue.invokeLater
    看看
    http://www.csdn.net/expert/topic/1026/1026698.xml?temp=.122677