java swing 中如何让JScrollPane的滚动条置底
用swing开发聊天窗口界面,当行数超过高度时滚动条默认是置顶的。如何能让它置底,永远显示最新的消息?...
用swing开发聊天窗口界面,当行数超过高度时 滚动条默认是置顶的。如何能让它置底,永远显示最新的消息?
展开
推荐于2016-05-29
展开全部
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();
}
}
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();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询