限制用户对 JTextField 的控制
我有一个游戏,你可以使用字母库中的字母来创建单词。每次键入字母时,我希望字母图标消失,并在删除时重新出现。我已经完成了大部分功能,但有一些错误我无法修复。现在我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以扩展
PlainDocument
(在JTextField
中使用)并重写insertString()和remove()方法。您可以检查插入或删除的内容并调整您的界面。或者(可能更好)按照建议使用
DocumentFilter
Hovercraft Full Of Eels
You can extend
PlainDocument
(used inJTextField
) 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 suggestedHovercraft Full Of Eels