我想问的是
我用了一个jtextarea,嵌在一个jscrollpane里面
我想让它实现信息的输出,现在的问题是,信息输出到最下面一行之后,滚动条不自动出现,而是卡在那里,等到所有信息输出完了之后才一下子出现
怎么能让滚动条自动出现,然后信息一行一行的输出并且始终显示在最下面一行呢
我用了一个jtextarea,嵌在一个jscrollpane里面
我想让它实现信息的输出,现在的问题是,信息输出到最下面一行之后,滚动条不自动出现,而是卡在那里,等到所有信息输出完了之后才一下子出现
怎么能让滚动条自动出现,然后信息一行一行的输出并且始终显示在最下面一行呢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Demo extends JFrame {
JTextPane textPane = new JTextPane();
JTextArea ja = new JTextArea(); public Demo() {
super("JTextPane Demo");
getContentPane().setLayout(new BorderLayout());
final JButton buttonHome = new JButton("Go to Home");
final JButton buttonEnd = new JButton("Go to End");
final JButton buttonAdd = new JButton("增加行");
ActionListener positionHandler = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonHome) {
textPane.setCaretPosition(0);
textPane.requestFocus();
} else if (e.getSource() == buttonEnd) {
textPane.selectAll();
textPane.setCaretPosition(textPane.getSelectedText()
.length());
textPane.requestFocus();
}else if (e.getSource() == buttonAdd) {
textPane.setText(textPane.getText()+"aaaaaa\n");
textPane.selectAll();
textPane.setCaretPosition(textPane.getSelectedText()
.length());
}
}
};
buttonHome.addActionListener(positionHandler);
buttonEnd.addActionListener(positionHandler);
buttonAdd.addActionListener(positionHandler); JPanel paneNorth = new JPanel();
JPanel paneSouth = new JPanel(); paneSouth.add(buttonHome);
paneSouth.add(buttonEnd);
paneSouth.add(buttonAdd); getContentPane().add(paneSouth, BorderLayout.SOUTH); paneNorth.setLayout(new BorderLayout());
paneNorth.add(new JScrollPane(textPane), BorderLayout.CENTER);
getContentPane().add(paneNorth, BorderLayout.CENTER); setSize(300, 400);
show(); } public static void main(String[] args) {
Demo app = new Demo();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import javax.swing.*;public class TestJTextArea extends JFrame{
private JTextArea textArea;
private JScrollPane scrollPane;
public TestJTextArea(){
super("TestJTextArea");
textArea = new JTextArea(200, 150);
scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(200, 150));
add(scrollPane);
setSize(400, 300);
setVisible(true);
}
public static void main(String args[]){
new TestJTextArea();
}
}
否则textArea也不会跟着你加上的滚动条走
“信息输出到最下面一行之后,滚动条不自动出现,而是卡在那里,等到所有信息输出完了之后才一下子出现 “
放到另一个线程上执行的时候情况主不一样了,界面中的jscrollpane时刻在监听,文本域的变化,所以就在时刻滚动了。
1.会出现楼主说的不滚动的情况public class TestJScrollPane extends JFrame { public static JTextArea jt=new JTextArea(10,10);
public TestJScrollPane()
{
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane jp =new JScrollPane(jt);
this.getContentPane().setLayout(new FlowLayout());
JButton button=new JButton("添加内容");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
//添加内容的方法
SetContent sc=new SetContent();
sc.run();
}
}
);
this.add(jp);
this.add(button);
}
public static void main(String[] args) {
TestJScrollPane tjp=new TestJScrollPane();
tjp.setSize(400, 400);
tjp.setTitle("TestJScrollPane");
tjp.setVisible(true);
}}package lession8;public class SetContent/* extends Thread*/ { public void run(){
int count =0;
while(true)
{
TestJScrollPane.jt.append("hello world\n");
TestJScrollPane.jt.paintImmediately(TestJScrollPane.jt.getBounds());
try {
Thread.sleep(1000);
count++;
if(count>20)
{
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}2.实现了多线程可以滚动了
package lession8;import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;public class TestJScrollPane extends JFrame { public static JTextArea jt=new JTextArea(10,10);
public TestJScrollPane()
{
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane jp =new JScrollPane(jt);
this.getContentPane().setLayout(new FlowLayout());
JButton button=new JButton("添加内容");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
//添加内容的方法
SetContent sc=new SetContent();
sc.start();
}
}
);
this.add(jp);
this.add(button);
}
public static void main(String[] args) {
TestJScrollPane tjp=new TestJScrollPane();
tjp.setSize(400, 400);
tjp.setTitle("TestJScrollPane");
tjp.setVisible(true);
}}package lession8;public class SetContent extends Thread { public void run(){
int count =0;
while(true)
{
TestJScrollPane.jt.append("hello world\n");
TestJScrollPane.jt.paintImmediately(TestJScrollPane.jt.getBounds());
try {
this.sleep(1000);
count++;
if(count>20)
{
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
textArea.setCaretPosition(textArea.getText().length())