我有 2 个jframes
(jframeA,jframeB)
.单击jframeA
中的按钮,jframeB
必须关闭。
jframeA
是在项目启动时创建的,下面是创建框架并将其设置为可见的代码部分。
/*jFrameA*/
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Login().setVisible(true);
}
});
}
(上面的代码是由网络豆子自动创建的)
我想从jframeB
关闭此帧.正如我们在上面的代码中看到的没有创建jframeA
的对象,只有构造函数被调用并设置可见性以true
。由于没有jframeA
对象,我不明白如何在单击按钮jframeB
关闭此框架。
请提供解决方案。
由于没有 jframeA 的对象,我不明白如何关闭 单击按钮时来自 jframeB 的此帧。
如果您希望对对象执行操作,则需要保存对该对象的引用。
Login jframeA = new Login();
// ...
jframeA.setVisible(false);
jframeA.dispose();
jframeA = null;
首先将 jframeB 的包导入到 jframeA。将此代码写在 jframeA 文件的顶部。
import JFrameB; //write the name of the package of jframeB instead of "JFrameB"
假设 jframeA 的类名是 FrameA,jframeB 的类名是 FrameB,我写下面的代码。
FrameB jframeB = new FrameB();//write this code outside of all the methods but inside of the class.
您说您使用 Netbeans。因此,双击该按钮并在ActionPerformed方法中编写以下代码。
jframeB.dispose();//this will close jframeB when you click on the button.