Java JSpinner 防止字母插入

发布于 2024-09-05 03:20:32 字数 172 浏览 4 评论 0原文

JSpinner 用于在我的应用程序中存储数字(使用 SpinnerNumberModel)。

正如预期的那样,微调器不允许存储无效字符(字母、符号等)。但是,当我输入这些字符时,这些字符确实会出现在微调器组件中。一旦我将焦点切换到另一个组件,它们就会消失。

有没有办法防止无效字符出现在微调器中?

A JSpinner is used to store a number in my application (with a SpinnerNumberModel).

As expected, the spinner doesn't allow invalid characters (letters, symbols, etc.) to be stored. However, those characters do appear in the spinner component when I type them in. As soon as I switch the focus to another component, they disappear.

Is there a way to prevent invalid characters from appearing in the spinner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

故事和酒 2024-09-12 03:20:32

您可以将 DocumentFilter 添加到微调器的编辑器中,以防止将不需要的字符添加到文档中。您可以使用如下代码获得编辑器:

JTextField editor = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();

阅读 Swing 教程中关于 实现文档过滤器了解更多信息。

You can add a DocumentFilter to the editor of the spinner to prevent unwanted character from being added to the Document. You get the editor using code like:

JTextField editor = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();

Read the section from the Swing tutorial on Implementing a Document Filter for more information.

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