我的程序首先提示用户输入他们想要环绕的质量数
public class textEvent1 implements ActionListener { //action listener for "how many masses?"
public void actionPerformed (ActionEvent e) {
n = (int)(Double.parseDouble(massNumField.getText()));
submit1.setVisible(false); //removes text event 1 text from screen
massNumLabel.setVisible(false);
massNumField.setVisible(false);
然后使用该信息创建该数量的标签和文本字段,如下所示
for(int i=1; i<=n; i++) { //adds text event 2 text to the screen
massLabel = new JLabel("How much mass does Mass " +i+ " have? ");
massField = new JTextField(5);
xCorLabel = new JLabel("What is mass "+i+"'s starting x-coordinate?");
xCorField = new JTextField(5);
yCorLabel = new JLabel("what is mass "+i+"'s starting y-coordinate?");
yCorField = new JTextField(5);
xVelLabel = new JLabel("What is mass "+i+"'s starting x velocity?");
xVelField = new JTextField(5);
yVelLabel = new JLabel("What is mass "+i+"'s starting y velocity?");
yVelField = new JTextField(5);
add(massLabel);
add(massField);
add(xCorLabel);
add(xCorField);
add(yCorLabel);
add(yCorField);
add(xVelLabel);
add(xVelField);
add(yVelLabel);
add(yVelField);
}
现在我的问题是使用另一个 actionlistner(提交按钮)从这些文本字段中读取并将输入的值分配到数组中。由于我不知道会有多少质量我不知道会有多少文本字段并且每个文本字段基本上具有相同的名称,我如何分配说当它的文本字段具有相同名称时为 mass2 的质量输入的值所有其他文本字段?