【问题标题】:JTextArea and DocumentFilter println current textJTextArea 和 DocumentFilter println 当前文本
【发布时间】:2014-04-20 18:49:56
【问题描述】:

好的,这听起来很简单,但仍然困扰着我。我使用 jtextarea 和文档过滤器。我想只要用户按下“a”,例如只是当前要通过 println 打印的文本。我用:

public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {

    if ("a".equals(text)) {     
        String c = fb.toString();
        System.out.println(c);              
    }
    super.replace(fb, offset, length, text, attrs);
    }
}

例如,我键入“帮助”并在控制台中获取 javax.swing.text.AbstractDocument$DefaultFilterBypass@6f9bb25a!但为什么? D:非常感谢

【问题讨论】:

    标签: java swing jtextarea documentfilter


    【解决方案1】:

    使用

    String c = fb.getDocument().getText(0, fb.getDocument().getLength());
    

    您当前正在打印 FilterBypass 对象。需要通过传递偏移量和文档长度来获取Document并获取文本

    【讨论】:

    • 你是我今天的英雄!
    • 如果用户在末尾以外的位置(即开头)插入文本,这将始终将文本添加到末尾。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2022-08-17
    • 2013-04-22
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多