获取面板中所有 jtextfields 的所有值
Get all the values of all jtextfields in a panel
面板中的所有文本字段都有文档侦听器。它将所有文本字段中的所有值相加,并将其设置文本到另一个面板中的另一个文本字段以求和。
我的问题是我有 74 个文本字段,如何使用 for 循环检查它的所有值?我不知道该怎么办。
查看下面的代码。它的要点在评论中。
干杯!
import java.awt.Component;
import java.awt.Container;
import java.util.stream.Stream;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class IterateOverJTextField {
private static void iterateOverJTextFields(Container container) {
// You have to call getComponents in order to access the
// container's children.
// Then you have to check the type of the component.
// In your case you're looking for JTextField.
// Then, you do what you want...
// Old-style
for (Component component : container.getComponents()) {
if (component instanceof JTextField) {
System.out.println(((JTextField) component).getText());
}
}
// New-style with Stream
Stream.of(container.getComponents())
.filter(c -> c instanceof JTextField)
.map(c -> ((JTextField) c).getText())
.forEach(System.out::println);
}
public static void main(String[] args) {
JPanel panel = new JPanel();
panel.add(new JTextField("text 1"));
panel.add(new JTextField("text 2"));
panel.add(new JTextField("text 3"));
panel.add(new JTextField("text 4"));
// You have to work with your container
// the has the 74 fields. I created this
// panel just to test the code.
iterateOverJTextFields(panel);
}
}
面板中的所有文本字段都有文档侦听器。它将所有文本字段中的所有值相加,并将其设置文本到另一个面板中的另一个文本字段以求和。
我的问题是我有 74 个文本字段,如何使用 for 循环检查它的所有值?我不知道该怎么办。
查看下面的代码。它的要点在评论中。
干杯!
import java.awt.Component;
import java.awt.Container;
import java.util.stream.Stream;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class IterateOverJTextField {
private static void iterateOverJTextFields(Container container) {
// You have to call getComponents in order to access the
// container's children.
// Then you have to check the type of the component.
// In your case you're looking for JTextField.
// Then, you do what you want...
// Old-style
for (Component component : container.getComponents()) {
if (component instanceof JTextField) {
System.out.println(((JTextField) component).getText());
}
}
// New-style with Stream
Stream.of(container.getComponents())
.filter(c -> c instanceof JTextField)
.map(c -> ((JTextField) c).getText())
.forEach(System.out::println);
}
public static void main(String[] args) {
JPanel panel = new JPanel();
panel.add(new JTextField("text 1"));
panel.add(new JTextField("text 2"));
panel.add(new JTextField("text 3"));
panel.add(new JTextField("text 4"));
// You have to work with your container
// the has the 74 fields. I created this
// panel just to test the code.
iterateOverJTextFields(panel);
}
}