显示 JTextArea 中的字符数

发布于 2024-12-26 23:51:10 字数 1036 浏览 3 评论 0原文

我正在寻找一个示例来显示用户已在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2025-01-02 23:51:10

您可以使用 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");
        }
    }
});

You could use a DocumentListener to listen for changes to the textarea:

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");
        }
    }
});
眼眸里的快感 2025-01-02 23:51:10

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

You can attach a listener (listen a TextEven or DocumentEvent) and when the text is too long, you send a warning to the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文