#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Changing between JPanels causes them to "fall" off JFrame


    Hello

    I'm currently working on the GUI interface for a game I'm making. What I'm doing right now is allowing the user to be able to move from the main menu to a screen which shows the controls. I am treating each screen as it's own object. The user moves between screens by clicking on a JButton.

    The problem I'm having is that when the user moves between the screens, the JPanels slowly move down and eventually disappear (I've included a link to show what this looks like).

    http://postimage.org/image/oaqahslh3/

    I haven't been able to find any similar problems online, but I've noted a few things.
    • The JPanel does not "fall" when returning to main menu
    • The green background of controlPanel remains while returning to the main menu
    • Using another (we'll call it a master) JPanel to hold all other panels of all the menus and removing/adding to the master JPanel doesn't solve the problem
    • Removing setPreferredSize doesn't help, it still "falls"
    • I have a feeling it's a problem in the actionPerformed methods, but I've included the constructors as well.


    Main Menu class (called SpaceShooterApp)
    Code:
    /*Constructor, initializes and adds components of main menu*/
    	public SpaceShooterApp(){
    		menuDrawPanel = new DrawingPanel();
    		newGame = new JButton("New Game");
    		controls = new JButton("Controls");
    		menuPanel = new JPanel();
    		
    		
    		setBackground(Color.lightGray);
    		setLayout(new BorderLayout());
    		
    		newGame.addActionListener(new ButtonListener());
    		controls.addActionListener(new ButtonListener());
    		
    		menuPanel.setPreferredSize(new Dimension (400,40));
    		menuPanel.setBackground(Color.white);
    		
    		menuPanel.add(newGame);
    		menuPanel.add(controls);
    		
    		
    		add(menuDrawPanel, BorderLayout.CENTER);
    		add(menuPanel, BorderLayout.SOUTH);
    		
    		//setPreferredSize(new Dimension(440,340));
    	}//end constructor
    	
    	/*Inner class, contains event listener for JButton, starts a new game*/
    	private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			
    			Object source = event.getSource();
    			
    			if(source == newGame){
    				remove(menuPanel);
    				remove(menuDrawPanel);
    				add(new SpaceShooterPanel());
    				validate();
    				revalidate();
    			} else if (source == controls){
    				remove(menuPanel);
    				remove(menuDrawPanel);
    				add(new ControlPanel());
    				validate();
    				revalidate();
    			}
    		}//end method
    	}//end inner class
    Control Menu Class (called ControlPanel)
    Code:
    /*Constructor, initializes all control menu components and sets up events*/
    	public ControlPanel(){
    		cPanel = new JPanel();
    		buttonPanel = new JPanel();
    		back = new JButton("Main Menu");
    		
    		back.addActionListener(new ButtonListener());
    		
    		buttonPanel.setPreferredSize(new Dimension(440,300));
    		cPanel.setPreferredSize(new Dimension(440,300));
    		buttonPanel.add(back);
    		add(cPanel);
    		add(buttonPanel);
    		
    		setBackground(Color.green);
    	}//end constructor
    	
    	/*Inner class, contains event handling methods*/
    	private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			Object source = event.getSource();
    			if(source == back){
    				remove(cPanel);
    				remove(buttonPanel);
    				add(new SpaceShooterApp());
    				validate();
    				revalidate();
    			}
    		}//end method
    	}//end inner class
    Any help given would be appreciate.

    Thank you for your time.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    Can you make a small, complete program that compiles, executes and shows the problem?
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    Where are SpaceShooterApp and ControlPanel located relative to the main program?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Here's a working copy of the program, with only the buttons added.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SpaceShooterApp extends JPanel{
    	
    	
    	private JPanel menuDrawPanel;
    	private JButton newGame;
    	private JButton controls;
    	private JPanel menuPanel;
    	
    	public static void main(String [] args){
    		JFrame frame = new JFrame("Space Fighter");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new SpaceShooterApp());
    		frame.pack();
    		frame.setVisible(true);
    	}//end main
    	
    	/*Constructor, initializes and adds components of main menu*/
    	public SpaceShooterApp(){
    		
    		/*menuDrawPanel will be a DrawingPanel which extends JPanel, but to 
    		 *minimize the amount of code to copy, I've just made it into a JPanel
    		 **/
    		//menuDrawPanel = new DrawingPanel();
    		menuDrawPanel = new JPanel(); 
    		
    		newGame = new JButton("New Game");
    		controls = new JButton("Controls");
    		menuPanel = new JPanel();
    		
    		
    		setBackground(Color.lightGray);
    		setLayout(new BorderLayout());
    		
    		newGame.addActionListener(new ButtonListener());
    		controls.addActionListener(new ButtonListener());
    		
    		menuPanel.setPreferredSize(new Dimension (400,40));
    		menuPanel.setBackground(Color.white);
    		
    		menuPanel.add(newGame);
    		menuPanel.add(controls);
    		
    		
    		add(menuDrawPanel, BorderLayout.CENTER);
    		add(menuPanel, BorderLayout.SOUTH);
    		
    		setPreferredSize(new Dimension(440,340));
    	}//end constructor
    	
    	/*Inner class, contains event listener for JButton, starts a new game*/
    	private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			
    			Object source = event.getSource();
    			
    			if (source == controls){
    				remove(menuPanel);
    				remove(menuDrawPanel);
    				add(new ControlPanel());
    				validate();
    				revalidate();
    			}
    		}//end method
    	}//end inner class
    	
    }//end class
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ControlPanel extends JPanel{
    	
    	private JPanel cPanel;
    	private JPanel buttonPanel;
    	private JButton back;
    	
    	/*Constructor, initializes all control menu components and sets up events*/
    	public ControlPanel(){
    		cPanel = new JPanel();
    		buttonPanel = new JPanel();
    		back = new JButton("Main Menu");
    		
    		back.addActionListener(new ButtonListener());
    		
    		buttonPanel.setPreferredSize(new Dimension(440,300));
    		cPanel.setPreferredSize(new Dimension(440,300));
    		buttonPanel.add(back);
    		add(cPanel);
    		add(buttonPanel);
    		
    		setBackground(Color.green);
    	}//end constructor
    	
    	/*Inner class, contains event handling methods*/
    	private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			Object source = event.getSource();
    			if(source == back){
    				remove(cPanel);
    				remove(buttonPanel);
    				add(new SpaceShooterApp());
    				validate();
    				revalidate();
    			}
    		}//end method
    	}//end inner class
    	
    }//end class
    Cheers
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    There are missing class definitions: SpaceShooterPanel
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    There are missing class definitions: SpaceShooterPanel
    Sorry, edited the post above. Should compile now.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    I've already copied the code, what do I need to change?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    I've already copied the code, what do I need to change?
    Just remove the first if statement from the actionPerformed method in the ButtonListener class of the SpaceShooterApp class.

    Basically, change...

    Code:
    if(source == newGame){
    				remove(menuDrawPanel);
    				remove(menuPanel);
    				add(new SpaceShooterPanel());
    				validate();
    				revalidate();
    			} else if (source == controls){
    				remove(menuDrawPanel);
    				remove(menuPanel);
    				add(new ControlPanel());
    				validate();
    				revalidate();
    			}
    to...
    Code:
    if (source == controls){
    				remove(menuDrawPanel);
    				remove(menuPanel);
    				add(new ControlPanel());
    				validate();
    				revalidate();
    			}
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    When I execute the code and press the buttons one after the other, the green stripe at the top gets wider and wider, pushing the components below it down and out of the frame. Is that what the problem is?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    When I execute the code and press the buttons one after the other, the green stripe at the top gets wider and wider, pushing the components below it down and out of the frame. Is that what the problem is?
    Yes it is, I'm not sure how to fix it. And I haven't been able to find a solution for it by searching online.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    Try doing some debugging by printing out the parent of the component following each add() being done. Call the getParent() method.
    I think the way you are adding components could be a problem.

    Also try this:
    add these definitions:
    static Color[] colors = new Color[] {Color.orange, Color.green, Color.red, Color.black, Color.blue, Color.cyan, Color.yellow};
    static int clrIdx = 0;


    and change this:
    setBackground(colors[clrIdx++]); //Color.green); //<<<<<<<<<<<<< shows small sliver of each color

IMN logo majestic logo threadwatch logo seochat tools logo