swing中的JTextArea控制行数

http://blog.csdn.net/findhappy7/archive/2008/05/08/2417490.aspx

 

 

近日做了一个工具用来验证一点东西,开启多线程验证时,把验证过程中的一些结果输出到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) Surfilter Technology Co.,Ltd
 * 
@author PurpleMaple --zhangyixuan(张逸轩)
 * @date 2007-10-24 上午10:24:34
 
*/

public   class  LimitativeDocument  extends  PlainDocument {
    
private JTextComponent textComponent;
    
private int lineMax = 10;
    
public   LimitativeDocument(JTextComponent tc,int lineMax){     
        textComponent 
= tc;
        
this.lineMax = lineMax;
    }

    
public   LimitativeDocument(JTextComponent tc){     
        textComponent 
= tc;
    }

    
public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException {
        
        String value 
=   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) Surfilter Technology Co.,Ltd
 * 
@author PurpleMaple --zhangyixuan(张逸轩)
 * @date 2007-10-24 上午10:25:41
 
*/

public   class  LimitativeTextArea  extends  JFrame {

    
private JTextArea logInfo;
    
private JTextField content;
    
private JButton addButton;
    
private final int contentMax = 5;
    
    
public LimitativeTextArea(){
        initComponent();
    }

    
    
private void initComponent(){
        Container pane  
= 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");
        JPanel contentPanel 
= new JPanel();
        contentPanel.add(content);
        contentPanel.add(addButton);
        pane.add(contentPanel,BorderLayout.SOUTH);
        
        InputListener listener 
= new InputListener();
        content.registerKeyboardAction(listener,KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 
0false),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(){
        String contentText 
= 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(ActionEvent e){
            
            executeClick();
        }

        
public void mouseClicked(MouseEvent event){
            executeClick();
        }

        
private void executeClick(){
            appendContent();
        }

    }

}

如果有正好需要这方面功能的可以参考一下,如果您看到的是转载的文章,请记得原地址是http://blog.csdn.net/cleverfoxloving

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值