问题描述
例如,如果组件是一个复选框,它必须设置为 false,或者它是一个文本字段,它必须清除文本.我正在尝试编写一种方法来重置 JPanel
中的所有组件.它必须像 HTML 表单中的重置功能一样工作.
For example, if component is a checkbox it must set to false, or it is a textfield it must be clear the text. I am trying to write a method for reset all components in a JPanel
. It must work like reset function in HTML forms.
如何将 JComponent
重置为默认值?
How to reset a JComponent
to the default value?
推荐答案
一种可能的解决方法是创建自定义重置函数.重新初始化面板(您的表单).
One possible workaround would be to create a custom reset function. Reinitialize the panel ( your form).
例如
void reset(){
//recreate the form panel.
formPanel = new FormPanel();
}
创建自定义类 FormPanel
来存储表单字段及其侦听器.
Create a custom class FormPanel
to store the form fields and their listeners.
重新初始化面板组件会导致按照@Robin 的建议重新分配监听器的开销.
Re initializing the panel components would result in an overhead of reassigning the listeners as @Robin suggested.
这篇关于将 JComponent 重置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!