2013-04-15 33 views
0

我必须创建一个程序,接受当天的用户输入以及当天的降雨量。我有3个班 - 雨量观测器,降雨量图和降雨量。到目前为止,我在Rainfall Frame中创建了GUI,并设置了动作侦听器以将用户输入添加到JTextArea中。但是,它只列出一个输入,我需要它列出所有初始化为0的31天,然后在用户输入月份的日期和降雨量时更新。降雨图 - textfield填充数组

/** 
* 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 = Integer.parseInt(rainEntryField.getText()); 
     // Clear the input 
     dayEntryField.setText(""); 
     rainEntryField.setText(""); 
     dataArea.setText(newDay + ": " + newRain + " cm" + "\n"); 
    } 
} 

此刻,用户输入不存储在数组中。我已经在班级Rainfall Chart中创建了该数组。

/** 
* Constructor: initializes the rainfall array to 0s. 
*/ 
public RainfallChart() 
{ 
    rainfall = new double[32]; // 31+1 as will not use element 0 
    for(int i=0;i<rainfall.length;i++) 
    { 
     rainfall[i] = 0; 
    } 
} 

在节目的最后,我需要它来绘制一个条形图中关于用户提交的值textarea的。目前我想知道如何将用户输入从Rainfall Frame类中的JTextField转移到Rainfall Chart类中的数组。

编辑:

降雨Frame类

制作的磁盘阵列 - 类rainfall_frame类

private void getArray() 
{ 
    int i; 
    int[ ] a = new int[32]; 
    for(i=0;i<a.length;i++) 
    { 
     a[i] = Integer.parseInt(rainEntryField.getText()); 
    } 

} 

回答

1

你可以做这样的事情...

创建一个阵列(说 'A')并在其中存储每31天的降雨量值...

在班级rainfall_chart中创建班级rainfall_frame的对象...

使用对象访问数组'a'元素使用对象...

+0

嗨,我不知道如果我已经正确地创建阵列了。如果你可以看看那个会很棒,我已经将它编辑到我的文章中了! – user1554786

+0

为什么你在这个'private void getArray()'中创建数组而不是将它声明为私有成员,因此它可以被对象容易地访问 –

+0

抱歉,一位java新手,因此我需要删除private void getArray方法,是私人会员? – user1554786