在 Swing 范围内传输数组中的值 - java

Transporting values in arrays within scopes in Swing - java

我的目标是能够绘制图表,然后在需要时保存其值。

在上图中,我从comboBox中选择了一种特定类型的图形来绘制,除了Clear,它只是清除显示的图形。这已经可以正常工作了。 我有一个 UserPattern(我创建的)对象的数组列表,其中包含一个字符串、一个双精度数组和一个双精度数组。

我想将图表中的值存储在某个变量中(我目前正在使用双精度数组)以供日后使用。 对于双数组,我在传递值时遇到了问题,因为当我按 "Save Pattern" 它会保留图形的最新值(显示的最后一个图形)并将其输入到我的 UserPattern 列表的每个元素上以前保存过。所以,即使我保存了多个图案,它们都保留了最后一次保存的值。

这是我用来在 ArrayList 中存储值的代码:

private void readAndInsertPatternValues(List<UserPattern> patternLi, double[] graphValue) {



    UserPattern tempUserPattern = new UserPattern(typePattern);
    //extra code

        tempUserPattern.setMonthlyConsump(consTemp);
        tempUserPattern.setNameID(patternName);
        tempUserPattern.setPatternValues(graphValue);
        patternLi.add(tempUserPattern);
        System.out.println("Inserted: ");
        System.out.println(tempUserPattern);

    }
}

我在按钮上的鼠标事件中调用此方法 "Save Pattern":

    JButton btnSaveUserPattern = new JButton("Save Pattern");
    btnSaveUserPattern.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            if(!((comboBoxPattern.getSelectedItem()).equals(UserPattern.PatternType.CLEAR)))
                readAndInsertPatternValues(patternList, patternValue);

            for(UserPattern upTemp : patternList) {
                System.out.println("Inside the Pattern List:");
                System.out.println(upTemp.toString());
            }
        }
    });
    btnSaveUserPattern.setFont(new Font("Tahoma", Font.PLAIN, 13));
    btnSaveUserPattern.setEnabled(false);

此外,我通过选择组合框的一个选项获得了在事件中创建图表的值:

 comboBoxPattern = new JComboBox<UserPattern.PatternType>();
    comboBoxPattern.setFont(new Font("Tahoma", Font.PLAIN, 13));
    comboBoxPattern.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        //this method creates the graphs, so I send the array "patternValue" to "get" the values.
            printPatternGraph(comboBoxPattern, chartPanel, "User Pattern for Energy Consumption", "Hours", "Energy Consumption(Wh)", true, patternValue);

            btnSaveUserPattern.setEnabled(true);

        }
    });

我在调用和使用上述方法的方法中将 patternValue 数组初始化为局部变量:

double[] patternValue = new double[1440]; 

有了这一切,就发生了传输值的问题。在 "insertion time" 处,控制台显示了正确的值,但是,当我单击 "Save Pattern" 按钮时,它显示了 UserPattern 列表中的所有对象,之前保存的图表中的图表值等于我最后保存的那个。 (我保存的最后一个损坏了所有其他的)。

我尝试改变传递值的方式,将方法"printPatternGraph"改为return一个double[]数组,然后做成这样:

  patternValue=printPatternGraph(comboBoxPattern, chartPanel, "User Pattern for Energy Consumption", "Hours", "Energy Consumption(Wh)", true);

然而,这给我带来了错误,

Local variable patternValue defined in an enclosing scope must be final or effectively final.

因此,我尝试将 "patternValue" 设为全局变量。这实际上解决了问题,但我不认为这是最好的解决方案(我多次阅读全局变量是 "bad coding".

这样,你觉得我应该用哪种方式来实现?

感谢您的关注,抱歉拖了这么久post,

恩赫卡斯

解决方案:

我明白哪里不对了!很抱歉打扰你们。 我的问题是,当我将双数组添加到 ArrayList 时,我是这样做的:

patternLi.add(graphValues);

这不是将值存储在 graphValue 数组中,而是存储对它的引用。因此,当我想创建不同的图表时,对图表的引用是相同的,但更改了值。

我必须执行以下操作:

new double[] arrayTemp= new double[1440];
for ( int i=0; i<1440;i++) {
    arrayTemp[i]=graphValues[i];
}
patternLi.add(arrayTemp);

这样,成功了!因为,由于 arrayTemp 是本地的,所以每次我调用该方法并传递正确的值时都会创建它。 感谢大家的投入!

恩赫卡斯