gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:18:22 25 4
gpt4 key购买 nike

我有如下加载功能。

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”声明为全局变量

关于java - 如何将局部变量的值传递给另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50886375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com