Java 使用swing调用父窗体的方法

Java 使用swing调用父窗体的方法,java,swing,constructor,jbutton,actionlistener,Java,Swing,Constructor,Jbutton,Actionlistener,我有一个名为StaffListMain的JFrame表单,其中一个按钮单击事件中有以下代码: private void btnManageLeaveActionPerformed(java.awt.event.ActionEvent evt) { // Open the new form and pass the selected staff member ManageLeave manageLeaveForm = new ManageLeave(staff.getStaffAt

我有一个名为
StaffListMain
的JFrame表单,其中一个按钮单击事件中有以下代码:

private void btnManageLeaveActionPerformed(java.awt.event.ActionEvent evt) {
    // Open the new form and pass the selected staff member
    ManageLeave manageLeaveForm = new ManageLeave(staff.getStaffAt(lstStaff.getSelectedIndex()));
    manageLeaveForm.setVisible(true);
}
StaffListMain
类还有一个方法调用
writeToFile()
,我想在其他类中使用这个方法调用,比如上面代码片段中的方法(
ManageLeaveForm


因此,我需要一种方法来调用另一种形式的方法。这是可能的,还是我必须将
writeToFile()
分离到另一个类中,然后根据需要在每个单独的类中使用它?

您可以通过给当前实例的构造函数一个StaffListMain字段并将该
传递到该字段中,将对当前实例的引用传递到ManageLeave实例中。然后,如果需要,您可以从ManageLeave对象内调用调用StaffListMain对象的方法。

您可以通过为当前实例的构造函数提供一个StaffListMain字段并将此
传递到该字段中,将对当前实例的引用传递到ManageLeave实例中。然后,如果需要,您可以从ManageLeave对象中调用调用StaffListMain对象的方法。

您使用的是NetBeans吗?请停止使用NetBeans,开始使用Java(例如“form”->“frame”或
JFrame
)。您正在使用NetBeans吗?请停止谈论NetBeans,开始谈论Java(例如“form”->“frame”或
JFrame
)。