简体   繁体   English

需要有关如何从JPanel中关闭父JFrame的帮助

[英]Need Assistance on How to Close Parent JFrame from within JPanel

I've searched and could not find the answer I need to do the following: I have two java files: one JFrame, one JPanel. 我已经搜索了并且找不到答案,我需要执行以下操作:我有两个Java文件:一个JFrame,一个JPanel。 I configured a button in the JFrame to open up the JPanel from within the main frame with a new size of 800,800. 我在JFrame中配置了一个按钮,以从主框架中打开JPanel,新大小为800,800。 Now, I want to close the JPanel and go back to the original JFrame (the one that originally was at size 500,500 with an image). 现在,我想关闭JPanel并返回到原始JFrame(该JFrame最初的大小为500,500,带有一个图像)。 It seems simply straightforward, but I've created an instance of the main frame from within the JPanel and set the jPanel to ( this.setVisible(false) ). 看起来似乎很简单,但是我已经从JPanel内部创建了主框架的实例,并将jPanel设置为( this.setVisible(false) )。 I created a new jFrame object and set its visibility to true. 我创建了一个新的jFrame对象,并将其可见性设置为true。 What happens is, a new instance of the JFrame appears alright, but the JFrame at 800,800 with no image still appears as well. 发生的事情是,新出现了一个JFrame实例,但是仍然出现了800,800且没有图像的JFrame。 I've tried several configurations of getContentPane() , setContentPane() and even tried passing a JFrame parameter to the constructor of the JPanel. 我尝试了getContentPane()setContentPane()几种配置,甚至尝试将JFrame参数传递给JPanel的构造函数。 I'm not sure where I am going wrong with this, but any help would be much appreciated. 我不确定我在哪里出错,但是任何帮助将不胜感激。 All I want is the original JFrame with the original size and image displayed. 我想要的只是具有原始大小和图像显示的原始JFrame。 Thank you in advance. 先感谢您。

private void jButton_closeActionPerformed(java.awt.event.ActionEvent evt) {                                              

    this.setVisible(false);
    mainMenuFrame = new MainMenuFrame();
    mainMenuFrame.setVisible(true);
    invalidate(); validate();
    repaint();

}                                             

you could open and close the jpanel from within your JFrame. 您可以从JFrame中打开和关闭jpanel。 the button would be also added to the jframe instead of the jpanel. 该按钮也将添加到jframe中,而不是jpanel中。 for easier accessing use the jpanel as member variable 为了更容易访问,请使用jpanel作为成员变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM