rt。。写了一个类实现ActionListener,actionPerformed时递归遍历容器中的所有组件 如果是可输入组件则清除内容,可选择组件则清除选择。
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JComboBox;
- import javax.swing.JList;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- /**
- *
Title: 重置按钮监听器
- *
- *
Description: 单击重置按钮 清空窗体中数据
- *
- * @author YOYO
- *
- * @create 2009-8-8
- *
- * @修改历史
- *
- 版本号 修改日期 修改人 修改说明
- *
- *
- */
- public class SuperResetButtonAction implements ActionListener {
- private Container container;
- public SuperResetButtonAction(Container container) {
- this.container = container;
- }
- /**
- * 清空容器内容
- * @param parent
- */
- private void clear(Container parent) {
- for(Component component: parent.getComponents()){
- if(component instanceof Container) {
- clear((Container) component);
- }
- if(component instanceof JTextField) {
- ((JTextField) component).setText("");
- }
- if(component instanceof JTextArea) {
- ((JTextArea) component).setText("");
- }
- if(component instanceof JComboBox) {
- if(((JComboBox) component).getItemCount()>0) {
- ((JComboBox) component).setSelectedIndex(0);
- }
- }
- if(component instanceof JList) {
- ((JList)component).clearSelection();
- }
- }
- }
- public void actionPerformed(ActionEvent arg0) {
- clear(container);
- }
- }