近日做了一个工具用来验证一点东西,开启多线程验证时,把验证过程中的一些结果输出到JTextArea面板中,早就知道TextArea没有提供控制字符或者控制行数的功能,只不过我以前的输出内容不会太多,昨天的程序输出太多内容了,竟然导致抛出了异常,所以今天想办法实现控制它的行数。具体做法是自己继承PlainDocument类重写insertString方法,然后把JTextArea的Document设置为自己的这个类,只要在insertString方法中做些判断,如果超过设置的最大值,就调用AbstractDocument中的remove(int offs, int len)方法,可以把旧的内容删除掉,追加新的内容了,截图就不截了,示意代码如下(两个类文件):
自定义的Document类LimitativeDocument.java:
/***/
/** *project:study *package:swingstudy *file:LimitativeDocument.java *date:2007-10-24*/
package
swingstudy;
import
javax.swing.text.AttributeSet;
import
javax.swing.text.BadLocationException;
import
javax.swing.text.JTextComponent;
import
javax.swing.text.PlainDocument;
/***/
/** *description:自定义的Document *可以控制最大行数 *默认最大为10行 *超过最大行时,上面的一行将被截取 * * *Copyright(c)SurfilterTechnologyCo.,Ltd *@author PurpleMaple--zhangyixuan(张逸轩) *@date2007-10-24上午10:24:34 */
public
class
LimitativeDocument
extends
PlainDocument
...
{ private JTextComponenttextComponent; private int lineMax = 10 ; public LimitativeDocument(JTextComponenttc, int lineMax) ... { textComponent = tc; this .lineMax = lineMax; } public LimitativeDocument(JTextComponenttc) ... { textComponent = tc; } public void insertString( int offset,Strings,AttributeSetattributeSet) throws BadLocationException ... { Stringvalue = textComponent.getText(); int overrun = 0 ; if (value != null && value.indexOf( ' ' ) >= 0 && value.split( " " ).length >= lineMax) ... { overrun = value.indexOf( ' ' ) + 1 ; super .remove( 0 ,overrun); } super .insertString(offset - overrun,s,attributeSet); } }
负责显示的LimitativeTextArea类:
/***/
/** *project:study *package:swingstudy *file:LimitativeTextArea.java *date:2007-10-24*/
package
swingstudy;
import
java.awt.BorderLayout;
import
java.awt.Container;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.event.KeyEvent;
import
java.awt.event.MouseAdapter;
import
java.awt.event.MouseEvent;
import
javax.swing.JButton;
import
javax.swing.JComponent;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
import
javax.swing.JScrollPane;
import
javax.swing.JTextArea;
import
javax.swing.JTextField;
import
javax.swing.KeyStroke;
/***/
/** *description:输入文字的面板 *它的JTextArea可以控制最大行数 *默认最大为10行 *超过最大行时,上面的一行将被截取 * *Copyright(c)SurfilterTechnologyCo.,Ltd *@author PurpleMaple--zhangyixuan(张逸轩) *@date2007-10-24上午10:25:41 */
public
class
LimitativeTextArea
extends
JFrame
...
{ private JTextArealogInfo; private JTextFieldcontent; private JButtonaddButton; private final int contentMax = 5 ; public LimitativeTextArea() ... { initComponent(); } private void initComponent() ... { Containerpane = getContentPane(); pane.setLayout( new BorderLayout()); logInfo = new JTextArea( 22 , 9 ); logInfo.setDocument( new LimitativeDocument(logInfo,contentMax)); pane.add( new JScrollPane(logInfo),BorderLayout.CENTER); content = new JTextField( 18 ); addButton = new JButton( " Submit " ); JPanelcontentPanel = new JPanel(); contentPanel.add(content); contentPanel.add(addButton); pane.add(contentPanel,BorderLayout.SOUTH); InputListenerlistener = new InputListener(); content.registerKeyboardAction(listener,KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0 , false ),JComponent.WHEN_FOCUSED); addButton.addActionListener(listener); } public void showFrame() ... { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable( false ); setSize( 500 , 400 ); setLocationRelativeTo( null ); setVisible( true ); } private void appendContent() ... { StringcontentText = content.getText(); if ( ! contentText.equals( "" )) ... { logInfo.append(contentText + " " ); logInfo.setCaretPosition(logInfo.getText().length()); } content.setText("" ); } public static void main(String[]args) ... { new LimitativeTextArea().showFrame(); } class InputListener extends MouseAdapter implements ActionListener ... { public void actionPerformed(ActionEvente) ... { executeClick(); } public void mouseClicked(MouseEventevent) ... { executeClick(); } private void executeClick() ... { appendContent(); } } }
如果有正好需要这方面功能的可以参考一下,如果您看到的是转载的文章,请记得原地址是http://blog.csdn.net/cleverfoxloving