package test;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.awt.AWTEvent;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class ScrollBottom extends JFrame{
private JTextArea ta = new JTextArea();
public ScrollBottom(){
super();
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
init();
} private void init(){
setBounds(0, 0, 300, 150); final JScrollPane sp = new JScrollPane(ta);
getContentPane().add(sp, "Center"); ta.setText("Start.....");
for(int i=0;i<=10;i++){
ta.append(i+"\n");
}
// TimerTask task = new TimerTask(){
// public void run(){
// ta.append("Time: " + (System.currentTimeMillis() / 1000));
// scrollAndSetCursor();
// }
// };
} /** 该方法用于实现滚动以及光标相关效果 */
public void scrollAndSetCursor(){
// ta.requestFocus();
ta.setSelectionStart(ta.getText().length());
} protected void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING)
System.exit(0);
} public static void main(String[] args){
ScrollBottom scroll = new ScrollBottom();
scroll.setVisible(true);
}
}
我动态的向JTextArea 里面添加数据
可是为什么这个滚动条不跟着向下滚动呢···
请高手解答 ····
谢谢····
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.awt.AWTEvent;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class ScrollBottom extends JFrame{
private JTextArea ta = new JTextArea();
public ScrollBottom(){
super();
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
init();
} private void init(){
setBounds(0, 0, 300, 150); final JScrollPane sp = new JScrollPane(ta);
getContentPane().add(sp, "Center"); ta.setText("Start.....");
for(int i=0;i<=10;i++){
ta.append(i+"\n");
}
// TimerTask task = new TimerTask(){
// public void run(){
// ta.append("Time: " + (System.currentTimeMillis() / 1000));
// scrollAndSetCursor();
// }
// };
} /** 该方法用于实现滚动以及光标相关效果 */
public void scrollAndSetCursor(){
// ta.requestFocus();
ta.setSelectionStart(ta.getText().length());
} protected void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING)
System.exit(0);
} public static void main(String[] args){
ScrollBottom scroll = new ScrollBottom();
scroll.setVisible(true);
}
}
我动态的向JTextArea 里面添加数据
可是为什么这个滚动条不跟着向下滚动呢···
请高手解答 ····
谢谢····
recvScrollPane.getViewport().add(recvArea, null);
然后在JTextArea插入最后一条新消息之后,将滚动条的Viewport重新设置到最底端的位置:
int height = 20;
Point p = new Point();
p.setLocation(0, recvArea.getLineCount() * height);
recvScrollPane.getViewport().setViewPosition(p);import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;public class ScrollTextArea extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JScrollPane jScrollPane = null; private JTextArea jTextArea = null; /**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(0, 2, 290, 144));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
} /**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
} /**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ScrollTextArea thisClass = new ScrollTextArea();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
} /**
* This is the default constructor
*/
public ScrollTextArea() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
} /**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
{
setBounds(0, 0, 300, 150);
final JScrollPane sp = new JScrollPane(ta);
getContentPane().add(sp, "Center");
for (int i = 0; i <= 10; i++)
{
ta.append(i + "\n");
}
JScrollBar sb = sp.getVerticalScrollBar();
sb.setValue(sb.getMaximum()); // TimerTask task = new TimerTask(){
// public void run(){
// ta.append("Time: " + (System.currentTimeMillis() / 1000));
// scrollAndSetCursor();
// }
// };
}
shuanlarousi1我想还没有明白我的意思··
我想要的是动态添加··然后放滚动条会显示滚动
你那样只是获得光标,但是不能动态添加nine_suns99的代码拿走了··谢谢··很好用···