Java 向数组添加用户输入

Java 向数组添加用户输入,java,arrays,swing,jtextfield,Java,Arrays,Swing,Jtextfield,获取了一个程序,该程序从JTextFeld获取用户输入并将其添加到JTextArea。下面是action listener类: /** * Action listener class for reading in data and processing it * when the Add reading button is clicked. */ class AddReadingListener implements ActionListener { public void act

获取了一个程序,该程序从JTextFeld获取用户输入并将其添加到JTextArea。下面是action listener类:

/**
 * 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中的值存储到数组中?我假设当按下按钮时,它需要在action listener类中实现

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

对于(int c=0;c使用
ArrayList
而不是
double
数组,并返回它。

您的问题没有说明您是否希望两个输入以任何方式链接。如果要这样做,您应该使用
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!
public void getRain()//如果创建了一个添加内容的方法,那么应该将其命名为addRain()
{
double[]rArray=new double[32];//这将创建一个大小为33的数组

对于(int c=0;c您可以尝试以下方法:

   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); 
              }
               }
公共类RainApplet扩展JFrame{
公共图书馆(){
最终JTextField rainEntryField=新JTextField(“0”,10);
最终JTextField dayEntryField=新JTextField(“0”,10);
最终JTextArea数据区=新的JTextArea(“”,10,10);
rainEntryField.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
//获取新的阅读详细信息
int newDay=Integer.parseInt(dayEntryField.getText());
double newRain=double.parseDouble(rainEntryField.getText());
//清除输入
dayEntryField.setText(“”);
rainEntryField.setText(“”);
dataArea.append(newDay+“:”+newRain+“cm”+“\n”);
arr.add(newRain);
}
//ActionListener的结尾
});
//创建新的JPanel“内容”,并使用BorderLayout添加所有组件
JPanel content=新的JPanel();
content.setLayout(新的BorderLayout(5,5));
//在JPanel周围放置组件
内容。添加(rainEntryField,BorderLayout.NORTH);
content.add(dayEntryField,BorderLayout.EAST);
添加(数据区,BorderLayout.EAST);
//为面板设置所需的属性
此.setContentPane(内容);
这个包();
此.setTitle(“Rain Applet”);
此参数为.setresizeable(false);
此.setLocationRelativeTo(空);
此.setVisible(true);
此.setDefaultCloseOperation(关闭时退出);
}
private ArrayList arr=new ArrayList();
公共空间添加(双d)
{
arr.add(d);
}
}

这应该可以做到这一点,并使用上面的数组来解释@John Snow

您在哪里调用getRain()的原因?dayEntryField的数据类型?您正在使用double数组来存储int!为什么?为了更快地获得更好的帮助,请发布一个。请注意,一个空行总是足够用于代码的空格。添加更多的空行既不能修复损坏的代码,也不能使其运行更快。“从JLabel获取用户输入”,JLabel不会从用户处获取输入,好的。
   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); 
              }
               }