The scroll of Textarea is mainly to use a jscrollpane1.getViewPort (). setViewPosition (New Point (0)); // to the top, but some people will say use Jtextarea2.SetcaretPosition (0); but the facts prove, it seems to be no big The use of the use, I will not use it! Below is a program I made with JBuilder, run in JBuilder6.0 Win2000 JDK1.4.
Import javax.swing. *; import java.awt. *; import java.awt.event. *; / ** *
title: this follow is newman's write p> *
Description: I want better Writing, Instead of Best One! p> *
Copyright: Copyright (c) 2002 p> *
company: newman0708@eastday.com p> * @Author newman * @version 1.0 * /
public class ScrollFrame extends JFrame {JPanel jPanel1 = new JPanel (); JScrollPane jScrollPane1 = new JScrollPane (); BorderLayout borderLayout1 = new BorderLayout (); JTextArea jTextArea1 = new JTextArea (); JPanel jPanel2 = new JPanel (); String str = " gsdklfsdljfsdlkjfdsljfdslkfjslk / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / N " " SDFJSDKFJSDLKJFKSSDJFKDSFJDKSJFKSDSDSDFDSSDFSDDS / N " " SDFJSDKFJSDLKJFKSSDJFJSDSFJDKSJFKSDSDSDFDSDSDDDS / N " " SDFJSDKFJS dlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds / n " " end sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfs / n "; JButton jButton1 = new JButton (); JButton JButton2 =
new JButton (); public ScrollFrame () {try {jbInit ();} catch (Exception e) {e.printStackTrace ();}} public static void main (String [] args) {ScrollFrame scrollFrame1 = new ScrollFrame (); scrollFrame1.setSize (200,300); scrollFrame1.show ();} private void jbInit () throws Exception {this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jPanel1.setLayout (borderLayout1); jTextArea1.setText (this.str); jScrollPane1.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane1.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jButton1.setText ( "end"); jButton1.addActionListener (new java.awt.event.ActionListener () {public void actionPerformed (ActionEvent e) {jButton1_actionPerformed (e );}}); JButton2.Settext ("start"); jbutton2.addactionListener (new java.awt.event) .ActionListener () {public void actionPerformed (ActionEvent e) {jButton2_actionPerformed (e);}}); this.getContentPane () add (jPanel1, BorderLayout.CENTER);. JPanel1.add (jScrollPane1, BorderLayout.CENTER); jPanel1. Add (JPanel2, BorderLayout.South); JPanel2.Add (jbutton1, null); jpanel2.add (jbutton2, null); jscrollpane1.getViewPort (). add (jtextarea1, null);}
void jButton1_actionPerformed (ActionEvent e) {// end int height = 10; Point p = new Point (); p.setLocation (0, this.jTextArea1.getLineCount () * height);. this.jScrollPane1.getViewport () setViewPosition ( p);
Void JButton2_Actionperformed (ActionEvent E) {// Start Point P = New Point (0); this.jscrollpane1.getViewPort (). setViewPosition (P);}} Learn to learn from the masters on 9CBS!