0

得到了一个程序,它从 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);
    }
}

怀疑数组是否正确,但任何帮助将不胜感激!

4

3 回答 3

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 解释

于 2013-04-16T12:28:49.167 回答
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. 使用ArrayListover 数组,因为它是动态的并且更易于使用
  2. 确保arraylist在方法之外被初始化(如果你使用方法来添加值)
  3. 确保您的方法的名称与他们正在做的事情相对应
于 2013-04-16T12:12:57.953 回答
0

使用 anArrayList<Double>而不是double数组,并返回它。

于 2013-04-16T11:32:13.337 回答