【问题标题】:Rainfall Chart - textfield populate array降雨图 - 文本字段填充数组
【发布时间】:2013-04-15 20:50:55
【问题描述】:

我必须创建一个程序来接受当天的用户输入和当天的降雨量。我有 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;
    }
}

在程序结束时,我需要它在文本区域中绘制一个关于用户提交的值的条形图。目前我想知道如何将用户输入从 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());
    }

}

【问题讨论】:

    标签: java arrays swing jtextfield jtextarea


    【解决方案1】:

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

    在类雨帧类中创建一个数组(比如“a”),并在其中存储每 31 天的降雨量值...

    在类雨量图表中创建类雨量框架的对象...

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

    【讨论】:

    • 嗨,我不确定我是否正确地创建了数组。如果你能看一下那就太好了,我已经把它编辑到我的帖子里了!
    • 为什么要在 private void getArray() 中创建数组,而不是将其声明为私有成员,以便对象可以轻松访问它
    • 对不起一个java新手,所以我需要删除私有的void getArray方法,什么是私有成员?
    • 类中的成员变量——这些被称为字段。
    • 私有修饰符——该字段只能在其自己的类中访问。
    猜你喜欢
    • 2017-02-24
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多