Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示JTextArea中的字符数_Java_Swing_Document_Jtextarea - Fatal编程技术网

Java 显示JTextArea中的字符数

Java 显示JTextArea中的字符数,java,swing,document,jtextarea,Java,Swing,Document,Jtextarea,我正在寻找一个示例来显示用户在JavaJTextArea中已经输入的字符数。我想在JTextArea中限制字符数(255) JTextArea kommentarArea = new JTextArea(11, 10); kommentarArea.setLineWrap(true); kommentarArea.setWrapStyleWord(true); AbstractDocument pDoc = (AbstractDocument) kommentarArea.getDocumen

我正在寻找一个示例来显示用户在JavaJTextArea中已经输入的字符数。我想在JTextArea中限制字符数(255)

JTextArea kommentarArea = new JTextArea(11, 10);
kommentarArea.setLineWrap(true);
kommentarArea.setWrapStyleWord(true);

AbstractDocument pDoc = (AbstractDocument) kommentarArea.getDocument();
pDoc.setDocumentFilter(new DocumentSizeFilter(MAXCOMMENTCHARS));
int option = JOptionPane.showOptionDialog(null, kommentarArea, "Bitte geben Sie einen   Kommentar ein", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, imexIcon, null, null);
if (option == JOptionPane.YES_OPTION && kommentarArea.getDocument().getLength() <= MAXCOMMENTCHARS)
    return kommentarArea.getText();
else if (kommentarArea.getDocument().getLength() > MAXCOMMENTCHARS) {
    throw new CommentTooLongException("Die Länge der Kommentare ist länger als 255 Zeichern");
} else {
    LOGGER.info("Versenden der Datei wurde abbrechen");
    System.exit(0);
}
JTextArea kommentarArea=新JTextArea(11,10);
kommentarArea.setLineWrap(真实);
kommentarArea.setWrapStyleWord(真);
AbstractDocument pDoc=(AbstractDocument)kommentarArea.getDocument();
setDocumentFilter(新的DocumentSizeFilter(MAXCOMMENTCHARS));
int option=JOptionPane.showOptionDialog(null,kommentarArea,“bite geben Sie einen Kommentar ein”,JOptionPane.YES\u NO\u option,JOptionPane.INFORMATION\u MESSAGE,imemicon,null,null);
if(option==JOptionPane.YES_option&&kommentarArea.getDocument().getLength()MAXCOMMENTCHARS){
抛出新的CommentTooLongException(“Die Länge der Kommentare is Länger als 255 Zeichen”);
}否则{
LOGGER.info(“Versenden der Datei wurde abbrechen”);
系统出口(0);
}

我使用Java教程中的DocumentSizeFilter。但是,我希望在此对话框中有一个文本标签,以便用户可以看到在JTextArea中输入了多少字符

您可以附加一个侦听器(侦听
TextEven
DocumentEvent
),当文本太长时,您将向用户发送警告。

您可以使用DocumentListener来侦听文本区域的更改:

kommentarArea.getDocument().addDocumentListener(new DocumentListener {
    public void insertUpdate(DocumentEvent event) {
        if (kommentarArea.getDocument().getLength() > 255) {
            JOptionPane.showMessageDialog(null, "Die Länge der Kommentare ist länger als 255 Zeichern");
        }
    }

    public void removeUpdate(DocumentEvent e) {
        if (kommentarArea.getDocument().getLength() > 255) {
            JOptionPane.showMessageDialog(null, "Die Länge der Kommentare ist länger als 255 Zeichern");
        }
    }

    public void changeUpdate(DocumentEvent event) {
        if (kommentarArea.getDocument().getLength() > 255) {
            JOptionPane.showMessageDialog(null, "Die Länge der Kommentare ist länger als 255 Zeichern");
        }
    }
});

你已经试过什么了,到底是什么没有达到预期效果?给我们一些您已经编写的代码,或者您只是希望有人为您开发解决方案?