限制用户对 JTextField 的控制

发布于 2024-12-19 06:52:37 字数 328 浏览 4 评论 0原文

我有一个游戏,你可以使用字母库中的字母来创建单词。每次键入字母时,我希望字母图标消失,并在删除时重新出现。我已经完成了大部分功能,但有一些错误我无法修复。现在我正在使用 KeyListener 并根据按键事件过滤输入/采取操作。然而,当同时按下多个键时,就会出现复杂情况。

输入过滤不是我的主要问题,它是在字母被删除时将它们放回银行。有没有一种方法可以完全删除插入符号,并且只允许用户从 JTextField 末尾添加输入到 / 退格键?此外,任何有关正确处理按钮混搭的提示将不胜感激。谢谢!

注意:如果您曾经玩过 Super Text Twist,这基本上就是我想要的猜测方法类型。

I've got a game where you create words using letters in a letter bank. Every time a letter is typed, I want the letter icon to disappear, and reappear when it is erased. I have most of the functionality done already, but there are a few bugs I cannot fix. Right now I am using a KeyListener and filtering input/taking action based on key events. However, complications arise when multiple keys are pressed at the same time.

The input filtering isn't my main problem, it's putting the letters back in the bank when they are erased. Is there a way to remove the caret entirely and only allow users to add input to / backspace from the end of the JTextField? Also, any tips about handling button mashing correctly would be greatly appreciated. Thanks!

Note: If you've ever played Super Text Twist, that's basically the type of guessing method that I'm going for.

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

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

发布评论

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

评论(1

烟雨凡馨 2024-12-26 06:52:37

您可以扩展PlainDocument(在JTextField中使用)并重写insertString()和remove()方法。您可以检查插入或删除的内容并调整您的界面。

或者(可能更好)按照建议使用 DocumentFilter Hovercraft Full Of Eels

You can extend PlainDocument (used in JTextField) and override insertString() and remove() methods. The yoou can check what's inserted or removed and adapt your interface.

Or (may be even better) use the DocumentFilter as suggested Hovercraft Full Of Eels

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