日期:2014-05-20  浏览次数:20803 次

java SWT如何在母面板中获取子控件及其值
在一个tabFolder中,有一个tabitem,在tabitem之上放一个composite,然后再在compsite上放了许多个Text控件,
现在我点击一个确定按钮,获取tabFolder当前选中项中的控件,也就是compsite上的text控件,依次获取text中的值,
求一个简单的例子( ⊙ o ⊙ )啊!

------解决方案--------------------
Java code

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);
    }
}