将用户输入添加到数组

时间:2013-04-16 11:06:31

标签: java arrays swing jtextfield

有一个程序从JTextFeld获取用户输入并将它们添加到JTextArea。这是动作监听器类:

/**
 * Action listener class for reading in data and processing it
 * when the Add reading button is clicked.
 */
class AddReadingListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        // Fetch the new reading details

            int newDay = Integer.parseInt(dayEntryField.getText());
            double newRain = Double.parseDouble(rainEntryField.getText());

            // Clear the input
            dayEntryField.setText("");
            rainEntryField.setText("");
            dataArea.append(newDay + ": " + newRain + " cm" + "\n");
        }
    }
}

我已经创建了一个存储用户输入的数组,但是我不知道如何将JLabel中的值存储到数组中?我假设当按下按钮时需要在动作监听器类中实现它的东西?

public void getRain()
{
    double[] rArray = new double[32];
    for(int c = 0;c<rArray.length;c++)
    {
        rArray[1] = Integer.parseInt(""+dayEntryField);
    }
}

怀疑阵列是否正确,但任何帮助都会受到赞赏!

3 个答案:

答案 0 :(得分:1)

您的问题并未说明您是否希望以任何方式链接两个输入。如果你这样做,你应该使用HashMap(或者你可以使用两个ArrayLists,如果你觉得更容易)。

您目前使用的方法无效。我将在下面解释:

public void getRain() //if you create a method that adds something, it should be named addRain() instead
{
    double[] rArray = new double[32]; //This creates a array with size 33
    for(int c = 0;c<rArray.length;c++) // here you create a loop that will run 33 times
    {
        rArray[1] = Integer.parseInt(""+dayEntryField); // This will input the value thats in the variable dayEntryField in index 1 of the array.
                                                        //This is not what you want since it will overwrite the current value thats in index 1 each time.
    }
} // End of method. Since you have added the value to an array that was created inside this method, the array will be removed by the garbage collector at this point. Hence your data will not be saved!

您需要做什么:

//Create the list outside the method
private ArrayList<Double> arr = new ArrayList<Double>();

public void addRain() // You could also design the method to take an array as parameter, and add the data to that list (which is normally what you do(
{
   arr.add(dayEntryField); //altho, since adding element is a one-liner this could easily be done inside your listener. If you have your arraylist inside a class you will need a method tho.
}

在没有看到更多代码的情况下,这可以给你很多帮助。但请记住:

  1. 在数组上使用ArrayList,因为它是动态的,更容易使用 与
  2. 确保arraylist在方法之外被初始化     (如果使用方法添加值)
  3. 确保您的方法名称与它们的名称相对应 做

答案 1 :(得分:1)

您可以尝试这样的事情:

   public class RainApplet extends JFrame {


   public RainApplet(){

   final JTextField rainEntryField = new JTextField("0", 10);

   final JTextField dayEntryField = new JTextField("0", 10);

   final JTextArea dataArea = new JTextArea("", 10, 10);

         rainEntryField.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                      // Fetch the new reading details

                    int newDay = Integer.parseInt(dayEntryField.getText());
                    double newRain = Double.parseDouble(rainEntryField.getText());

                    // Clear the input
                    dayEntryField.setText("");
                    rainEntryField.setText("");
                    dataArea.append(newDay + ": " + newRain + " cm" + "\n");

                    arr.add(newRain);
                                 }



    // end of ActionListener
        });

        // create new JPanel 'content' and add all components using BorderLayout
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout(5, 5));

        // placing components around the JPanel
        content.add(rainEntryField , BorderLayout.NORTH);
        content.add(  dayEntryField, BorderLayout.EAST );
        content.add(  dataArea , BorderLayout.EAST );


        // set desirable properties for panel
        this.setContentPane(content);
        this.pack();
        this.setTitle("Rain Applet");
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

           }

             private ArrayList<Double> arr = new ArrayList<Double>();

            public void addRain(Double d)  
            {
               arr.add(d); 
              }
               }

这应该可以解决问题并使用上面的数组,原因是由@John Snow解释

答案 2 :(得分:0)

使用ArrayList<Double>代替double数组,并将其返回。