1

我有一个 JFrame 和一堆不同的 JTextField 等等。如何在不为每个组件设置操作的情况下收集用户输入的数据?

注意:我使用“内联”代码创建此 JTextField,如下所示:

    layout.row().grid(new JLabel("Density")).add(new JTextField("1"))
            .spanRow();
    layout.row().grid(new JLabel("Minimal size"))
            .add(new JTextField("1")).spanRow();
4

2 回答 2

1

The Swing Utils class has some convenience methods that makes this easy:

List<JTextField> components = SwingUtils.getDescendantsOfType(JTextField.class, container, true);

for (JTextField component: components)
{
    // add custom code here
}
于 2010-09-08T14:56:54.700 回答
1

您可能需要遍历每个容器的子元素,检查它是否是一个实例,JTextField然后使用该getText方法读取内容。

于 2010-09-08T12:54:55.743 回答