当前位置: 技术问答>java相关
我怎么将插入在TextArea中的内容显示在当前页面?详细内容请入内查看!!
来源: 互联网 发布时间:2015-09-22
本文导语: 我将TextArea放在ScrollPane里面,我向TextArea里面插入内容时,出现滚动条,我怎么样让滚动条自己向下滑,而在TextArea中显示最近插入的内容!! | 万万不可!!! swing对线程是不安全的 必须用E...
我将TextArea放在ScrollPane里面,我向TextArea里面插入内容时,出现滚动条,我怎么样让滚动条自己向下滑,而在TextArea中显示最近插入的内容!!
|
万万不可!!!
swing对线程是不安全的
必须用EventQueue.invokeLater
看看
http://www.csdn.net/expert/topic/1026/1026698.xml?temp=.122677
swing对线程是不安全的
必须用EventQueue.invokeLater
看看
http://www.csdn.net/expert/topic/1026/1026698.xml?temp=.122677
|
我用的是JDK1.3.1,没出现你所说的问题,或者说我没看清楚你的意思
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ScrollTest extends JFrame implements ActionListener
{
private JTextArea txt = new JTextArea();
private JScrollPane scroll = new JScrollPane(txt);
private JButton insertButton = new JButton("Insert");
private JButton appendButton = new JButton("Append");
private JTextField posText = new JTextField(5);
private JTextField strText = new JTextField(10);
public ScrollTest() {
JPanel p = new JPanel();
p.add(new JLabel("Insert Position:"));
p.add(posText);
p.add(new JLabel("Insert String:"));
p.add(strText);
p.add(insertButton);
p.add(appendButton);
getContentPane().add(p, BorderLayout.NORTH);
getContentPane().add(scroll, BorderLayout.CENTER);
insertButton.addActionListener(this);
appendButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setTitle("Scroll Test");
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==insertButton)
txt.insert(strText.getText()+"n", Integer.parseInt(posText.getText()));
else
txt.append(strText.getText()+"n");
//txt.getScrollableUnitIncrement(txt.getVisibleRect(),
// SwingConstants.VERTICAL, 1);
//scroll.scrollRectToVisible(txt.getVisibleRect());
}
public static void main(String args[]) {
new ScrollTest();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ScrollTest extends JFrame implements ActionListener
{
private JTextArea txt = new JTextArea();
private JScrollPane scroll = new JScrollPane(txt);
private JButton insertButton = new JButton("Insert");
private JButton appendButton = new JButton("Append");
private JTextField posText = new JTextField(5);
private JTextField strText = new JTextField(10);
public ScrollTest() {
JPanel p = new JPanel();
p.add(new JLabel("Insert Position:"));
p.add(posText);
p.add(new JLabel("Insert String:"));
p.add(strText);
p.add(insertButton);
p.add(appendButton);
getContentPane().add(p, BorderLayout.NORTH);
getContentPane().add(scroll, BorderLayout.CENTER);
insertButton.addActionListener(this);
appendButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setTitle("Scroll Test");
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==insertButton)
txt.insert(strText.getText()+"n", Integer.parseInt(posText.getText()));
else
txt.append(strText.getText()+"n");
//txt.getScrollableUnitIncrement(txt.getVisibleRect(),
// SwingConstants.VERTICAL, 1);
//scroll.scrollRectToVisible(txt.getVisibleRect());
}
public static void main(String args[]) {
new ScrollTest();
}
}
|
insert, append都很正常呀?
|
应该是这样:set会使得TextArea显示到最新内容的底部。