如何从 Java Swing 中多次使用的同一文本字段获取文本
How to get text from same Text Field used multiple times in Java Swing
我正在尝试从通过我的代码迭代的 JTextField 获取文本(显然,我无法从按钮添加不同的文本字段)。这是“添加项目”按钮的作用:
addButton.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
tf = new JTextField("Name",20);
tfv = new JTextField("Value", 7);
p.revalidate();
p.repaint();
p.add(tf);
p.add(tfv);
}
});
它使用 FlowLayout 在面板中添加了两个新的文本字段。
现在,我想从文本字段中获取用户给出的文本,每个文本字段都分配给不同的变量,或者可能通过单击“确定”按钮进入 ArrayList,但 getText() 方法似乎不起作用。
okButton.addActionListener( e -> {
String txt = tfv.getText(); //only captures the text from the last field in the panel
});
现在好像什么都想不起来了。
方法getComponents()将returnp
中的所有组件(JPanel
)。
为了区分每一个 JTextField
,您可以通过方法 setName() (and retrieve the name via method getName()
) or set some property using method putClientProperty() 给每一个一个唯一的名称(并通过方法 getClientProperty()
检索 属性)。
例如使用setName()
为每个JTextField
设置一个唯一的名称。
private int counter = 0; // class member
addButton.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
tf = new JTextField("Name",20);
tf.setName("tf" + counter);
tfv = new JTextField("Value", 7);
tfv.setName("tfv" + counter);
counter++;
p.revalidate();
p.repaint();
p.add(tf);
p.add(tfv);
}
});
如何识别每个 JTextField
:
okButton.addActionListener( e -> {
Component[] cmpts = p.getComponents();
for (Component cmpt : cmpts) {
String name = cmpt.getName();
if (name.matches("^tf\d+$") {
// Code to handle it.
}
else if (name.matches("^tfv\d+$") {
// Handling code.
}
}
});
在此代码中,当您在 addButton
中重新初始化 tf
和 tfv
时,您丢失了对先前定义的文本字段的引用
tf = new JTextField("Name",20);
tfv = new JTextField("Value", 7);
所以要解决这个问题,您需要定义一个 ArrayList 来保存对所有定义的文本字段的引用,然后您可以访问所有这些:
ArrayList<JTextField> arrayNames = new ArrayList<JTextField>();
ArrayList<JTextField> arrayValues = new ArrayList<JTextField>();
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tf = new JTextField("Name",20);
tfv = new JTextField("Value", 7);
p.revalidate();
p.repaint();
p.add(tf);
p.add(tfv);
arrayNames.add(tf);
arrayValues.add(tfv);
}
});
正在访问
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (JTextField txtValue : arrayValues) {
System.out.println(txtValue.getText());
}
}
});
我正在尝试从通过我的代码迭代的 JTextField 获取文本(显然,我无法从按钮添加不同的文本字段)。这是“添加项目”按钮的作用:
addButton.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
tf = new JTextField("Name",20);
tfv = new JTextField("Value", 7);
p.revalidate();
p.repaint();
p.add(tf);
p.add(tfv);
}
});
它使用 FlowLayout 在面板中添加了两个新的文本字段。 现在,我想从文本字段中获取用户给出的文本,每个文本字段都分配给不同的变量,或者可能通过单击“确定”按钮进入 ArrayList,但 getText() 方法似乎不起作用。
okButton.addActionListener( e -> {
String txt = tfv.getText(); //only captures the text from the last field in the panel
});
现在好像什么都想不起来了。
方法getComponents()将returnp
中的所有组件(JPanel
)。
为了区分每一个 JTextField
,您可以通过方法 setName() (and retrieve the name via method getName()
) or set some property using method putClientProperty() 给每一个一个唯一的名称(并通过方法 getClientProperty()
检索 属性)。
例如使用setName()
为每个JTextField
设置一个唯一的名称。
private int counter = 0; // class member
addButton.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
tf = new JTextField("Name",20);
tf.setName("tf" + counter);
tfv = new JTextField("Value", 7);
tfv.setName("tfv" + counter);
counter++;
p.revalidate();
p.repaint();
p.add(tf);
p.add(tfv);
}
});
如何识别每个 JTextField
:
okButton.addActionListener( e -> {
Component[] cmpts = p.getComponents();
for (Component cmpt : cmpts) {
String name = cmpt.getName();
if (name.matches("^tf\d+$") {
// Code to handle it.
}
else if (name.matches("^tfv\d+$") {
// Handling code.
}
}
});
在此代码中,当您在 addButton
中重新初始化 tf
和 tfv
时,您丢失了对先前定义的文本字段的引用
tf = new JTextField("Name",20);
tfv = new JTextField("Value", 7);
所以要解决这个问题,您需要定义一个 ArrayList 来保存对所有定义的文本字段的引用,然后您可以访问所有这些:
ArrayList<JTextField> arrayNames = new ArrayList<JTextField>();
ArrayList<JTextField> arrayValues = new ArrayList<JTextField>();
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tf = new JTextField("Name",20);
tfv = new JTextField("Value", 7);
p.revalidate();
p.repaint();
p.add(tf);
p.add(tfv);
arrayNames.add(tf);
arrayValues.add(tfv);
}
});
正在访问
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (JTextField txtValue : arrayValues) {
System.out.println(txtValue.getText());
}
}
});