日期:2014-05-20 浏览次数:20711 次
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* http://bbs.csdn.net/topics/390325621?page=1#post-393293083
* @author Administrator
*
*/
public class AutoMove extends JFrame{
int value;
int i;
JTextArea jt=new JTextArea("sdgsdagfsafdfgdsgdrerwerweeeeeeeee"
+"ddddddddddddddddddddddddddddddddddddddddddddddddddddd");
JScrollPane js=new JScrollPane(jt);
JButton jb=new JButton("Auto");
public static void main(String[] args){
new AutoMove();
}
public AutoMove(){
jt.setLineWrap(true);
js.setBackground(Color.red);
add(jb,BorderLayout.SOUTH);
add(js,BorderLayout.CENTER);
setVisible(true);
setSize(20,150);
final JScrollBar jsb=js.getVerticalScrollBar();
jsb.setMinimum(5);
jb.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
final Timer t=new Timer();
value=0;
i=1;
t.schedule(new TimerTask() {
@Override
public void run() {
jsb.setValue(5*(i++));
if(jsb.getValue()==value){
t.cancel();
}
value=jsb.getValue();
}
},new Date(),50L);
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
}