Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将局部变量的值传递给另一个方法_Java - Fatal编程技术网

Java 如何将局部变量的值传递给另一个方法

Java 如何将局部变量的值传递给另一个方法,java,Java,我有如下的加载函数 public void loadAppointment(String appointmentName) { int selectedRow = appointmentJTable.getSelectedRow(); appointmentsJComboBox.setSelectedItem(tableModel.getValueAt(selectedRow, 0)); String name = (String) tab

我有如下的加载函数

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();

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

 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()”中使用“selectedappoiment”的值。我是编程新手,最好有人能给我一个解释清楚的答案。

改变这个

private void PlaceJButtonActionPerformed(java.awt.event.ActionEvent evt)
对此

private void PlaceJButtonActionPerformed(java.awt.event.ActionEvent evt, Appointment appointment)

并在调用函数时传递值。任何基础教程都应该向您展示这一点。

我只需要创建一个约会类型的全局变量,并使其与局部变量相等

`Appointment example;
 public void loadAppointment(String appointmentName)
 {    ...
      Appointment selectedAppointment = appointmentList.get(name);
      example = selectedAppointment;
      ...
 }
 `
然后在另一种方法中使用该“示例”。

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

私有void PlaceJButtonActionPerformed(java.awt.event.ActionEvent evt)方法无法编辑。我在ui中添加了一个动作事件创建了它。