搜档网
当前位置:搜档网 › 如何不用鼠标拖动就可以实现滚动条的滑动

如何不用鼠标拖动就可以实现滚动条的滑动

如何不用鼠标拖动就可以实现滚动条的滑动?
如 有一个Jtextarea在滚动条容器中,两个按钮a,b.如何使点a时textarea下(或上)滑一行,点b时下滑一页?

---------------------------------------------------------------

应该不是很难了,我给你做了一个例子,看看试不是这种效果!下滑多少你可以自己改变一下数字就行
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public class moveScroll extends JFrame implements ActionListener{
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public moveScroll() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
moveScroll moveScroll1 = new moveScroll();
moveScroll1.setSize(400,300);
moveScroll1.setVisible(true);
moveScroll1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jScrollPane1.setBounds(new Rectangle(28, 29, 349, 177));
jTextArea1.setText("jTextArea1");
jButton1.setBounds(new Rectangle(35, 237, 90, 33));
jButton1.setText("up");
jButton1.addActionListener(this);
jButton2.setBounds(new Rectangle(174, 238, 98, 34));
jButton2.setText("down");
jButton2.addActionListener(this);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
jScrollPane1.getViewport().add(jTextArea1, null);
}

public void actionPerformed(ActionEvent e){
if(e.getSource().equals(jButton1)){
if(jScrollPane1.getVerticalScrollBar().getWidth()>0){
if(jScrollPane1.getViewport().getViewPosition().getY()>0){
jScrollPane1.getViewport().setViewPosition(
new Point((int)jScrollPane1.getViewport().getViewPosition().getX(),
(int)jScrollPane1.getViewport().getViewPosition().getY() - 5));
}
}
}else if(e.getSource().equals(jButton2)){
if(jScrollPane1.getVerticalScrollBar().getWidth()>0){


if(jScrollPane1.getViewport().getViewPosition().getY()< (jTextArea1.getHeight() - jScrollPane1.getHeight())){
jScrollPane1.getViewport().setViewPosition(
new Point((int)jScrollPane1.getViewport().getViewPosition().getX(),
(int)jScrollPane1.getViewport().getViewPosition().getY() + 5));

}
}
}
}
}

相关主题