45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:实现滚动条的自动滚动到textarea末尾的方法

实现滚动条的自动滚动到textarea末尾的方法

2016-09-05 18:16:40 来源:www.45fan.com 【

实现滚动条的自动滚动到textarea末尾的方法

textarea的滚动主要是用到一个
jScrollPane1.getViewport().setViewPosition(new Point(0,0));//到顶
但是也有人会说用jTextArea2 .setCaretPosition(0);但是事实证明,它好像没有什么大的用处,也放是本人不会用吧!
下面是我用jbuilder做的一个程序,在jbuilder6.0+win2000+jdk1.4能运行。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* <p>Title: This follow is newman's writing</p>
* <p>Description: I want better writing ,instead of best one!</p>
* <p>Copyright: Copyright (c) 2002</p>
* <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"+
"sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds/n"+
"sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds/n"+
"sdfjsdkfjsdlkjfkssdjfkdsfjdksjfksdsdfdssdfsdds/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,0);
this.jScrollPane1.getViewport().setViewPosition(p);
}
}

向csdn上的高手们
学习
学习
再学习!

 

本文地址:http://www.45fan.com/a/question/72834.html
Tags: 实现 滚动 textarea
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部