问题
In my program I have two JFrame
instances. When I click next
button I want to show next frame and hide current frame. So I use this.setVisible(false)
and new Next().setVisible(true)
. But in Next
window if I click back
button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
回答1:
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
- only one JFrame would be needed
- any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.


回答2:
That is an odd & quirky GUI. I suggest instead to run a JFrame
for the main GUI, and when the user wants to search, pop a JOptionPane
(or modal JDialog
) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
回答3:
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
回答4:
You may place your JFrames
on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
回答5:
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.
来源:https://stackoverflow.com/questions/7176299/how-to-use-setvisible-in-jframes