我有如下加载功能。

public void loadAppointment(String appointmentName)
    {
        int selectedRow = appointmentJTable.getSelectedRow();
        appointmentsJComboBox.setSelectedItem(tableModel.getValueAt(selectedRow, 0));
        String name = (String) tableModel.getValueAt(selectedRow, 1);
        nameJTextField.setText(name);
        rankJtextField.setText((String) tableModel.getValueAt(selectedRow, 2));
        notesJTextArea.setText((String) tableModel.getValueAt(selectedRow, 3));

        Appointment selectedAppointment = appointmentList.get(name);

        colorJpanel.setBackground(selectedAppointment.getColor());
        loadedData=getValueString();

    }


在这里,“ selectedAppointment”是一个局部变量。我想使用同一类中另一个方法的“ selectedAppointment”值。另一种方法是

 private void PlaceJButtonActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (valid())
        {
            String gridReference = appointmentForesightGridSelecter.getGridDisplayString();
            gridReference = gridReference.replaceAll(" ", "");

            if(isLoadedDataChanged)
            {
                **replaceRow(selectedAppointment);**

            }
            createAppointment(gridReference);
            resetPanel();
        }
    }


由于局部变量仅在方法内可见,因此我可以使用“ PlaceJButtonActionPerformed()”中“ selectedAppointment”的值。我是编程新手,如果有人可以给我一个很好解释的答案,那就更好了。

最佳答案

您有两种解决方案:
1:向“ PlaceJButtonActionPerformed”添加参数,该参数将作为第二个参数“ selectedAppointment”
2:或者您将“ selectedAppointment”声明为全局变量

10-08 00:24