JAVA 如何使JScrollPane中的JTextArea自动滚动到最后一行
1个回答
展开全部
用JScrollBar的setValue方法就可以了,给你举个例子,不过首先你要在JTextArea里面输入很多东西,使滑块出来然后才能设置值:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame{
JScrollPane pane;
JButton button;
public Test(){
super();
pane=new JScrollPane(new JTextArea());
button=new JButton("点击");
add(pane,BorderLayout.CENTER);
add(button,BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JScrollBar bar=pane.getVerticalScrollBar();
bar.setValue(bar.getMaximum());
}
});
}
public static void main (String[] args) {
JFrame frame=new Test();
frame.setVisible(true);
frame.setBounds(100,100,300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame{
JScrollPane pane;
JButton button;
public Test(){
super();
pane=new JScrollPane(new JTextArea());
button=new JButton("点击");
add(pane,BorderLayout.CENTER);
add(button,BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JScrollBar bar=pane.getVerticalScrollBar();
bar.setValue(bar.getMaximum());
}
});
}
public static void main (String[] args) {
JFrame frame=new Test();
frame.setVisible(true);
frame.setBounds(100,100,300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询