显示 JTextArea 中的字符数
我正在寻找一个示例来显示用户已在 Java JTextArea 中输入的字符数。我想在 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);
}
我使用 Java 教程中的 DocumentSizeFilter。但是,我想在此对话框中有一个文本标签,以便用户可以看到在 JTextArea 中输入了多少个字符
I am looking for a example to display the number of characters the user has already input in Java JTextArea. I want to have a limit number of characters (255) in JTextArea.
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);
}
I use the DocumentSizeFilter from Java tutorial. However, I want to have a text label in this dialog so that user can see how many characters has been input in the JTextArea
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 DocumentListener 来侦听文本区域的更改:
You could use a DocumentListener to listen for changes to the textarea:
您可以附加一个侦听器(侦听
TextEven
或DocumentEvent
),当文本太长时,您可以向用户发送警告。You can attach a listener (listen a
TextEven
orDocumentEvent
) and when the text is too long, you send a warning to the user.