为 JFrame 实现清除按钮
我正在为由四个 JPanel 组成的 JFrame 实现一个清除按钮。每个 JPanel 都有多个文本字段、单选按钮和复选框。
当表单加载时,应禁用“清除”按钮。仅当用户在任何面板中的任何字段中输入某些值时才应启用它。
我尝试向面板添加 KeyListener。但它没有正确获取事件。我是否必须为所有 UI 组件注册 KeyListener?还有什么好的方法吗?
提前致谢!
I'm implementing a clear button for a JFrame which consists of four JPanels. Each JPanel has several text fields, radio buttons and checkboxes.
When the forms loads "clear" buttons should be disabled. It should be only enabled when the user entered some value to any of those fields in any panel.
I tried by adding a KeyListener to the panels. But It does not get the events properly. Do I have to register KeyListener for all the UI components? Any other good method?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要进行更改,您可以将
ItemListener
添加到JCheckBox
和JRadioButtons
,对于JTextField
,您可以添加CaretListener
。这个小程序可能会帮助你:
For making changes you can add
ItemListener
to yourJCheckBox
andJRadioButtons
and forJTextField
you can addCaretListener
.This small program might help you :
KeyListener 未指定用于 监听 Swing GUI 中的
Keyboards
事件,因为 Swing 就在那里按键绑定KeyListener isn't designated for Listening a
Keyboards
events in the Swing GUI, for Swing is there KeyBindings您必须将 ActionListener 添加到 JTextFields,然后检查文本字段中的文本值,例如:
You must adding ActionListeners to your JTextFields, after this check text value in text fields, for example:
因此,您应该为所有这些字段实现
KeyListener
接口。在检测到用户的打字操作后,您应该启用该按钮。当然不。编写一个扩展 JTextField 并实现 KeyListener 的类。从此类派生您的对象(所有文本字段)。如果您实现了按键时应该执行的操作,那么所有这些对象都会遵守您的规则。
So you should implement
KeyListener
interface for all these fields. Upon detecting typing action of the user you should enable the button.Of course no. Write a class which extends JTextField and implements KeyListener. Derive your objects (all text fields) from this class. If you implement what you should do upon key strokes, all these objects will obey your rule.