Java 如何使用FEST获取某个组件

Java 如何使用FEST获取某个组件,java,swing,automated-tests,fest,Java,Swing,Automated Tests,Fest,我有一个问题: 我在JDialog中有四个JtextFields。如果我有四个JTextFields,其中属性name、text和visibility未定义或为空,如何获得某个JTextField public class Form1 { public static void main(String[] args) { JTextField tf1 = new JTextField(); JTextField tf2 = new JTextField();

我有一个问题:

我在
JDialog
中有四个
JtextField
s。如果我有四个
JTextField
s,其中属性
name
text
visibility
未定义或为空,如何获得某个
JTextField

public class Form1 {
    public static void main(String[] args) {
        JTextField tf1 = new JTextField();
        JTextField tf2 = new JTextField();
        JTextField tf3 = new JTextField();
        JTextField tf4 = new JTextField();

        tf1.setPreferredSize(tf1.getPreferredSize());
        tf1.setText("");
        tf2.setPreferredSize(tf2.getPreferredSize());
        tf2.setText("");
        tf3.setPreferredSize(tf3.getPreferredSize());
        tf3.setText("");
        tf4.setPreferredSize(tf4.getPreferredSize());
        tf4.setText("");

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(1, 1));
        frame.add(tf1);
        frame.add(tf2);
        frame.add(tf3);
        frame.add(tf4);
        frame.setSize(300, 85);
        frame.setVisible(true);
    }
}

下面是一个使用反射为对象中包含的所有JComponent实例运行setName的方法。如果对象内部存在循环引用,则会显示堆栈溢出错误

public static void assignComponentNames(Object obj) {

    try {

        Method getComponentsMethod = obj.getClass().getMethod("getComponents", new Class[]{});

        if (null != getComponentsMethod){
            try {
                for (Component component : (Component[])(getComponentsMethod.invoke(obj))){
                    assignComponentNames(component);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (InvocationTargetException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        else {
            System.out.println(obj.toString());
        }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    for (Field field : obj.getClass().getDeclaredFields()) {
        field.setAccessible(true);

        String fieldName = field.getName(); //this is a different name, the reflection level name
        Object fieldValue = null;

        try {
            fieldValue = field.get(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        if (null == fieldValue) {
            continue;
        }

        if (fieldValue instanceof JComponent) {

            String currentComponentNameForFieldValue = ((JComponent) fieldValue).getName();

            if (null == currentComponentNameForFieldValue){
                System.out.println("null component name");
                ((JComponent) fieldValue).setName(fieldName);  //this sets the name specially for JComponent
            }
        }
        else if(fieldValue instanceof Collection){

            for (Object subObject : ((Collection)fieldValue).toArray()) {

                assignComponentNames(subObject);
            }
        }

    }
}

下面是一个使用反射为对象中包含的所有JComponent实例运行setName的方法。如果对象内部存在循环引用,则会显示堆栈溢出错误

public static void assignComponentNames(Object obj) {

    try {

        Method getComponentsMethod = obj.getClass().getMethod("getComponents", new Class[]{});

        if (null != getComponentsMethod){
            try {
                for (Component component : (Component[])(getComponentsMethod.invoke(obj))){
                    assignComponentNames(component);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (InvocationTargetException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        else {
            System.out.println(obj.toString());
        }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    for (Field field : obj.getClass().getDeclaredFields()) {
        field.setAccessible(true);

        String fieldName = field.getName(); //this is a different name, the reflection level name
        Object fieldValue = null;

        try {
            fieldValue = field.get(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        if (null == fieldValue) {
            continue;
        }

        if (fieldValue instanceof JComponent) {

            String currentComponentNameForFieldValue = ((JComponent) fieldValue).getName();

            if (null == currentComponentNameForFieldValue){
                System.out.println("null component name");
                ((JComponent) fieldValue).setName(fieldName);  //this sets the name specially for JComponent
            }
        }
        else if(fieldValue instanceof Collection){

            for (Object subObject : ((Collection)fieldValue).toArray()) {

                assignComponentNames(subObject);
            }
        }

    }
}

afair,您必须设置name属性以便可以找到组件我无法访问源代码HMM。。。然后,你可能需要实现一个定制的Matcher(不要把我的名字,我已经有一段时间,写这是从我的头上;)你可能会考虑问你的问题在Festmail邮递清单,潜伏者倾向于反应相当快。您必须设置name属性,以便可以找到组件我无法访问源代码HMM。。。然后你可能需要实现一个定制的Matcher(不要把我的名字钉在上面,这已经有一段时间了,而且写在我的头顶上。)-你可以考虑问你关于Festmail邮件列表的问题,潜伏者倾向于快速响应。