访问在循环中创建的各个JTextField的值

时间:2014-04-15 21:28:09

标签: java arrays swing for-loop jtextfield

以下是我创建标签和JTextFields的方法:

    JPanel panel3 = new JPanel(new SpringLayout());
    String[] labels = {"Non-animated image name:","Left animation image name:","Top animation image name:",
            "Right animation image name:","Bottom animation image name:"};
    for(int i=0; i<labels.length; i++){
        JLabel l = new JLabel(labels[i],JLabel.TRAILING);
        JTextField n = new JTextField(10);
        panel3.add(l);
        l.setLabelFor(n);
        panel3.add(n);
    }

    SpringUtilities.makeCompactGrid(panel3,
            5, 2,
            6, 6,
            6, 6);

比如说,我如何使用标签“Top animation image name:”访问/获取JTextField中文本的值?

我知道通常情况下,可以执行JTextField.getText(),但对我而言,这似乎不适用于此。

提前致谢!

2 个答案:

答案 0 :(得分:2)

这只是问题的一个具体例子:

  

如何访问循环中创建的对象。

答案是一样的:把它们放在一个集合或数组中。请注意,收集选项具有更大的灵活性。例如,如果您创建了一堆JLabel / JTextField关联,则可以使用HashMap<String, JTextField>将JTextField与String关联。

例如:

Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();
String[] labels = {"Non-animated image name:","Left animation image name:","Top animation image name:",
        "Right animation image name:","Bottom animation image name:"};
for(int i=0; i<labels.length; i++){
    JLabel l = new JLabel(labels[i],JLabel.TRAILING);
    JTextField n = new JTextField(10);
    panel3.add(l);
    l.setLabelFor(n);
    panel3.add(n);
    fieldMap.put(labels[i], n);
}

// and then later you can get the text field associated with the String:
String text = fieldMap.get(labels[2]).getText();

或者是一个完整的例子:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

@SuppressWarnings("serial")
public class InputForm extends JPanel {
   private static final int COLUMNS = 10;
   private static final int GAP = 3;
   private static final Insets LABEL_INSETS = new Insets(GAP, GAP, GAP, 15);
   private static final Insets TEXTFIELD_INSETS = new Insets(GAP, GAP, GAP, GAP);
   private String[] labelTexts;
   private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();

   public InputForm(String[] labelTexts) {
      this.labelTexts = labelTexts;
      setLayout(new GridBagLayout());
      for (int i = 0; i < labelTexts.length; i++) {
         String text = labelTexts[i];
         JTextField field = new JTextField(COLUMNS);
         fieldMap.put(text, field);

         addLabel(text, i);
         addTextField(field, i);
      }
   }

   public String[] getLabelTexts() {
      return labelTexts;
   }

   private void addTextField(JTextField field, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 1;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.EAST;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = TEXTFIELD_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(field, gbc);
   }

   private void addLabel(String text, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 0;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = LABEL_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(new JLabel(text), gbc);
   }

   public String getFieldText(String key) {
      String text = "";
      JTextField field = fieldMap.get(key);
      if (field != null) {
         text = field.getText();
      }
      return text;
   }

   private static void createAndShowGui() {
      String[] labelTexts = new String[] { "One", "Two",
            "Three", "Four" };
      InputForm inputForm = new InputForm(labelTexts);

      int result = JOptionPane.showConfirmDialog(null, inputForm, "Enter Stuff Here",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      if (result == JOptionPane.OK_OPTION) {
         for (String text : labelTexts) {
            System.out.printf("%20s %s%n", text, inputForm.getFieldText(text));
         }
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:0)

每次创建JLabelJTextField时,都会在容器类的新实例中存储对每个引用的引用。

例如:

private class LabelTextFieldContainer {
    JLabel label;
    JTextField textField;

    //Constructor goes here...
}


for(int i=0; i<labels.length; i++){
    JLabel l = new JLabel(labels[i],JLabel.TRAILING);
    JTextField n = new JTextField(10);
    panel3.add(l);
    l.setLabelFor(n);
    panel3.add(n);

    containerList.add( new Container(l, n) );  //Instantiate List<LabelTextFieldContainer> containerList somewhere else
}