GUI Window Ordering and removing display problem
I am having problems in synchronising how my GUI will appear...
Currently I have the following as the main program
public class MainGUI extends JPanel{
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Firstscreen_Main");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Firstscreen_Main gp = new Firstscreen_Main();
// rest code
f.setVisible(true);
}
});
}
}
where Firstscreen_Main is a class like
public class Firstscreen_Main extends JPanel implements ActionListener, ChangeListener
{
// code
}
After this I have a button in the above class with a listener that gives a dialog for user input....
For this I add in the Firstscreen_Main code in listener handling
this.removeAll();
SecondScreen second = new SecondScreen();
This does'nt remove the contents of the first screen....
This maybe because the dialog is supposed to show the previous screen it originates fr开发者_StackOverflow中文版om (unsure ? )
but how can I completely destroy the first window and start next one with stored data?
I think I am not understanding the way in which the windows work ... Can anybody explain...
I have learnt about CardLayout but is there any simple way where I can just remove everything from first screen and work out a new one?
- You need to call dispose on the frame not on the panel.
- You MainGUI class should not be a JPanel, yet creating and controlling the frames in the application.
- Your classes should not be named in the order of which they should appear. What if you decided to the change the order, you don't want to have to change the names as well. Also class names should not have underscores in them.
- It looks like your classes have to many responsibilities. Try to breakup your classes into more meaningful chucks. At the same time try to reduce the coupling between the classes.
Overall, I think moving the control of what screen is being shown needs to move to a separate class instead of having each JPanel
be opening and closing frames.
Try something like this:
this.removeAll(); // Remove everything from the frame
SecondScreen second = new SecondScreen(); // Create the panel
this.add(second); // Add the panel to the frame
second.revalidate();
this.repaint();
Try to dispose the first screen using this.dispose();
Let me know if it works.
Regards
精彩评论