2

i have design two JFrames in netbeans(new-> Jframe ) and add one botton on each jframe , when i click a button from frame1 it opens Jframe2 its okay but i want is that the JFrame1 should be closed or not appear on the window..and alternatively when i click button from jframe2 it opens jframe1 but jframe2 should be closed..the one thing i achieved is jframes are opening on buttons click but jframes are not closing as per my requirements.. so i need expertise from you guys to fix this problem.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ankush Pruthi
  • 121
  • 2
  • 4
  • 14
  • The situations sounds like you should be using one window and [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) instead. – kiheru Sep 23 '13 at 08:39
  • 1
    whats wrong with your previous question, is there progress or regress – mKorbel Sep 23 '13 at 08:45
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Sep 23 '13 at 08:52
  • @kiheru : i am not using CardLayout in my project(contains two jframes).. – Ankush Pruthi Sep 23 '13 at 08:57
  • @mKorbe : in my previous question's answer i dont find the solutio working for me – Ankush Pruthi Sep 23 '13 at 09:08
  • @Andrew Thompson : in my project i am using two JFrames .so its a good/bad practice. i have no idea – Ankush Pruthi Sep 23 '13 at 09:13
  • I know you're not using CardLayout from your problem description. I think you should consider using it, however, as it usually results in better user interface than two separate frames. – kiheru Sep 23 '13 at 09:25
  • @Kiheru ok i will do a demo on this. – Ankush Pruthi Sep 23 '13 at 09:44
  • @kiherui: I have downloaded a snake game project in java on the internet.in this project there are three java files name "Engine.java" , "GameBoard.java", "Snake.java". and i have added two jframes in this project .in first frame there are three buttons 'play' , 'rules', 'exit'. when we click play button it runs the snake game and when click to rules button it opens rules.java jframe. can you please suggest me what things i have to do so that when i click play button it actually starts the game. – Ankush Pruthi Sep 23 '13 at 09:47
  • 2
    @Andrew Thompson this article suggesting use cardlayout instead of using multiple jframes. or use one jframe and other interal jframe. I do practice on this to explore the benefit of cardlayout and to ehance my knowledge too. – Ankush Pruthi Sep 23 '13 at 10:03
  • *"I do practice on this to explore the benefit of cardlayout.."* Great decision! [`CardLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) can be a little tricky at first, but I think you'll find it is just right for the job here. Just make sure the content of the cards is centered if it is not expected to be the largest of the cards in size. *"..and to ehance my knowledge too."* It is both fun and rewarding, isn't it? :) – Andrew Thompson Sep 23 '13 at 10:26
  • @Andrew Thompson Yes both funny and praising. more and more practice on cardlayout make me familiar to work with it.. – Ankush Pruthi Sep 23 '13 at 10:31

1 Answers1

3

In your button event handler (assuming your class extends JFrame), do something like this before opening the new frame: this.dispose()

From the Java doc, the .dispose() method:

public void dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).

npinti
  • 51,780
  • 5
  • 72
  • 96
  • @AnkushPruthi: Glad it solves your problem. Please mark the question which answered your problem/ – npinti Sep 23 '13 at 08:49
  • i have downloaded a snake game project in java on the internet.in this there are three java files name "Engine.java" , "GameBoard.java", "Snake.java". i have added two jframes in this project .i first frame there are three buttons 'play' , 'rules', 'exit'. when we click play button it runs the snake game and when click to rules button it opens rules.java jframe. suggest me what should i do if i click play button it actually starts the game. – Ankush Pruthi Sep 23 '13 at 09:49
  • @AnkushPruthi: This is a seperate question. Please post a new question to discuss this. – npinti Sep 23 '13 at 09:51
  • ok i do post this in form of new question. – Ankush Pruthi Sep 23 '13 at 10:05