我必须创建一个程序,接受当天的用户输入和当天的降雨量。我有3个班 - 降雨观察者,降雨图和降雨框架。到目前为止,在Rainfall Frame中我创建了GUI,并设置了动作监听器以将用户输入添加到JTextArea。但是,它只列出一个输入,我需要它列出31天全部初始化为0,然后在用户输入当月和降雨时更新。
/**
* 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类中的数组。
编辑:
在降雨帧类中创建数组 -
private void getArray()
{
int i;
int[ ] a = new int[32];
for(i=0;i<a.length;i++)
{
a[i] = Integer.parseInt(rainEntryField.getText());
}
}
答案 0 :(得分:1)
你可以做这样的事......
在类rain_frame类中创建一个数组(比如'a'),并在其中存储每31天的降雨量值......
在类rain_chart中创建类rainfall_frame的对象......
使用对象使用对象访问数组'a'元素...