日期:2014-05-20 浏览次数:21148 次
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ChildrenTest extends JFrame{
JTextField text1, text2, text3, text4;
JButton btn1, btn2;
public ChildrenTest(){
this.setSize(250,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
text1 = new JTextField(20);
text2 = new JTextField(20);
text3 = new JTextField(20);
text4 = new JTextField(20);
btn1 = new JButton("确定");
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Component[] components = btn1.getParent().getComponents();
for(Component c: components){
if(c instanceof JTextField){
JTextField textField = (JTextField)c;
System.out.println(textField.getText());
}
}
}
});
btn2 = new JButton("清空");
this.add(text1);
this.add(text2);
this.add(text3);
this.add(text4);
this.add(btn1);
this.add(btn2);
}
public static void main(String[] args) {
ChildrenTest test = new ChildrenTest();
test.setVisible(true);
}
}