【问题标题】:Accessing values of individual JTextFields created in a loop访问在循环中创建的单个 JTextField 的值
【发布时间】:2014-05-30 11:11:46
【问题描述】:

这是我创建标签和 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);

例如,我将如何访问/获取带有标签“顶部动画图像名称:”的 JTextField 中文本的值?

我知道通常可以执行 JTextField.getText(),但在我看来,这在这里行不通。

提前致谢!

【问题讨论】:

  • 将标签存储在数组中,以便稍后在程序中访问它们。或者您可以获取面板的所有组件并循环通过它,但这会有点低效。

标签: java arrays swing for-loop jtextfield


【解决方案1】:

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

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

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

【讨论】:

  • 哦!谢谢!在我完成后续问题的输入之前,您已回复。
【解决方案2】:

每次创建JLabel 和JTextField 时,将对它们中的每一个的引用存储在容器类的新实例中。

例如:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2020-05-20
    • 2015-07-11
    相关资源
    最近更新 更多