java swing 中如何让JScrollPane的滚动条置底

2025-01-26 12:32:15
推荐回答(1个)
回答1:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ScrollIt extends JFrame
{
JScrollPane spContainer;
JTextArea taedit;
JButton bScroll;
public ScrollIt()
{
spContainer=new JScrollPane();
taedit=new JTextArea();
spContainer.getViewport().add(taedit);
bScroll=new JButton("Scroll to last line");
bScroll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JScrollBar sbar=spContainer.getVerticalScrollBar();
sbar.setValue(sbar.getMaximum());
}
});
for(int i=0;i<40;i++)
taedit.append("line"+i+"\n");
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(spContainer,BorderLayout.CENTER);
this.getContentPane().add(bScroll,BorderLayout.NORTH);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String args[])
{
new ScrollIt();
}
}