gpt4 book ai didi

java - swing调用父窗体的方法

转载 作者:行者123 更新时间:2023-11-29 03:38:27 25 4
gpt4 key购买 nike

我有一个名为 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 字段并将 this 传递到该字段中,将对当前实例的引用传递到 ManageLeave 实例中。然后,如果需要,您可以从 ManageLeave 对象中调用调用方 StaffListMain 对象的方法。

关于java - swing调用父窗体的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290060/

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