Java 循环通过JPanel中的嵌套字段

Java 循环通过JPanel中的嵌套字段,java,swing,loops,constructor,Java,Swing,Loops,Constructor,我已经创建了两个类,第一个是生成JTextFields,第二个是将这些JTextFields注入JPanel。我正在尝试将.setText设置到每个JTextField,但很简单 for (Field f: fields) { f.setValue("my text"); } 不起作用-它只将文本设置为一个JPanel中的JTextFields。由于每个JTextField都有uniqe fieldID,您能告诉我如何将其设置为SETXT(setValue)吗?这些值需

我已经创建了两个类,第一个是生成JTextFields,第二个是将这些JTextFields注入JPanel。我正在尝试将.setText设置到每个JTextField,但很简单

    for (Field f: fields) {
    f.setValue("my text");
    }
不起作用-它只将文本设置为一个JPanel中的JTextFields。由于每个JTextField都有uniqe fieldID,您能告诉我如何将其设置为SETXT(setValue)吗?这些值需要使用ActionListener在循环之外设置

public class Field {

    JTextField field = new JTextField();
    static int fieldID = 0;
    private String text;
    public Field() {
        fieldID++;
    }

    public String getValue() {
        return field.getText();
    }

    public void setValue(String text) {
        field.setText(text);
    }
} 

public class Frame extends JFrame {

public Frame() {
    Field[] fields = new Field[9];
    JPanel[] corePane = new JPanel[9];
    JPanel frontPane = new JPanel();
    frontPane.setLayout(new GridLayout(3, 3));

    for (int i = 0; i < corePane.length; i++) {
        corePane[i] = new JPanel();
        for (int j = 0; j < 9; j++) {
            fields[j] = new Field();
            corePane[i].add(fields[j].field);
        }
        corePane[i].setLayout(new GridLayout(3, 3));
        frontPane.add(corePane[i]);
    }

    setLayout(new BorderLayout());
    setSize(300, 300);
    getContentPane().add(frontPane, BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public static void main(String args[]) {
    SwingUtilities.invokeLater(Frame::new);
}
}
公共类字段{
JTextField=新的JTextField();
静态int fieldID=0;
私有字符串文本;
公共领域(){
fieldID++;
}
公共字符串getValue(){
返回字段。getText();
}
公共void设置值(字符串文本){
field.setText(文本);
}
} 
公共类框架扩展了JFrame{
公共框架(){
字段[]字段=新字段[9];
JPanel[]corePane=新的JPanel[9];
JPanel frontPane=新的JPanel();
设置布局(新的网格布局(3,3));
对于(int i=0;i
因此,您的
字段可能需要是一个二维数组,例如

Field[][] fields = new Field[9][9];
这样,您就可以访问给定面板(第一维)的字段(第二维),即
fields[pane][field]

显然,在
for循环中,需要正确初始化字段数组,例如,
字段[i]=新字段[9]

这可能会使您的代码看起来更像

public class Field {

    JTextField field = new JTextField();
    static int fieldID = 0;
    private String text;

    public Field() {
        fieldID++;
    }

    public String getValue() {
        return field.getText();
    }

    public void setValue(String text) {
        field.setText(text);
    }
}

public class Frame extends JFrame {

    public Frame() {
        Field[][] fields = new Field[9][9];
        JPanel[] corePane = new JPanel[9];
        JPanel frontPane = new JPanel();
        frontPane.setLayout(new GridLayout(3, 3));

        for (int i = 0; i < corePane.length; i++) {
            corePane[i] = new JPanel();
            fields[i] = new Field[9];
            for (int j = 0; j < 9; j++) {
                fields[i][j] = new Field();
                corePane[i].add(fields[i][j].field);
            }
            corePane[i].setLayout(new GridLayout(3, 3));
            frontPane.add(corePane[i]);
        }

        setLayout(new BorderLayout());
        setSize(300, 300);
        getContentPane().add(frontPane, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(Frame::new);
    }
}
将意味着
字段
的每个实例都将具有相同的
字段ID
,无论您将其更改为什么。对我来说(根据你所说的),以这种方式使用
static
是没有意义的


还有一些替代方案,它们可能提供更好、更可重用的解决方案

作为@MadProgrammer解决方案的替代方案,我可以为您提供一些有用的方法,可以帮助您解决这些任务,而无需声明任何数组

/**
 * Searches for all children of the given component which are instances of the given class.
 *
 * @param aRoot start object for search. May not be null.
 * @param aClass class to search. May not be null.
 * @param <E> class of component.
 * @return list of all children of the given component which are instances of the given class. Never null.
 */
public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass) {
    return getAllChildrenOfClass(aRoot, aClass, e -> true);
}

/**
 * Searches for all children of the given component which are instances of the given class and satisfies the given condition.
 *
 * @param aRoot start object for search. May not be null.
 * @param aClass class to search. May not be null.
 * @param condition condition to be satisfied. May not be null.
 * @param <E> class of component.
 * @return list of all children of the given component which are instances of the given class. Never null.
 */
public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass, Predicate<E> condition) {
    final List<E> result = new ArrayList<>();
    final Component[] children = aRoot.getComponents();
    for (final Component c : children) {
        if (aClass.isInstance(c) && condition.test(aClass.cast(c))) {
            result.add(aClass.cast(c));
        }
        if (c instanceof Container) {
            result.addAll(getAllChildrenOfClass((Container) c, aClass, condition));
        }
    }
    return result;
}

因此,您的
字段
可能需要是二维数组,例如
字段[][]字段=新字段[9][9]。这样,您就可以访问给定面板(第一维)的字段(第二维),即
fields[pane][field]
。显然,在
for循环中,需要正确初始化字段数组,例如,
字段[I]=新字段[9]
;似乎文本字段应该替换为一个
JTable
(但关于它们的用途,我没有足够的细节来确定)。一般提示:1)为了更快地获得更好的帮助,请添加或。2)
public class Frame扩展JFrame{
这里没有扩展
JFrame
的好例子。只需使用一个实例即可。
/**
 * Searches for all children of the given component which are instances of the given class.
 *
 * @param aRoot start object for search. May not be null.
 * @param aClass class to search. May not be null.
 * @param <E> class of component.
 * @return list of all children of the given component which are instances of the given class. Never null.
 */
public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass) {
    return getAllChildrenOfClass(aRoot, aClass, e -> true);
}

/**
 * Searches for all children of the given component which are instances of the given class and satisfies the given condition.
 *
 * @param aRoot start object for search. May not be null.
 * @param aClass class to search. May not be null.
 * @param condition condition to be satisfied. May not be null.
 * @param <E> class of component.
 * @return list of all children of the given component which are instances of the given class. Never null.
 */
public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass, Predicate<E> condition) {
    final List<E> result = new ArrayList<>();
    final Component[] children = aRoot.getComponents();
    for (final Component c : children) {
        if (aClass.isInstance(c) && condition.test(aClass.cast(c))) {
            result.add(aClass.cast(c));
        }
        if (c instanceof Container) {
            result.addAll(getAllChildrenOfClass((Container) c, aClass, condition));
        }
    }
    return result;
}
getAllChildrenOfClass(getContentPane(), JTextField.class).forEach(tf -> tf.setText("My text"));