Results 1 to 3 of 3
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default CardLayout Trouble

    I've written this program where you have a CardLayout which switches between two panels based on which of the two buttons are pressed.
    The problem is that the panels aren't visible.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    
    
    public class Whatever {
    	
    	CardLayout deckOfPanels;
    	JFrame frame;
    	JPanel panel;
    	JPanel gamePanel;
    	JPanel aboutPanel;
    	
    	public Whatever()
    	{
    		frame = new JFrame("Example");
    		
    		gamePanel = new JPanel();
    		gamePanel.add(new JButton("I'm a button"));
    		
    		aboutPanel = new JPanel();
    		aboutPanel.add(new JLabel("Some stuff"));
    		
    		deckOfPanels = new CardLayout();
    		deckOfPanels.addLayoutComponent(gamePanel, "Game");
    		deckOfPanels.addLayoutComponent(aboutPanel, "About");
    		
    		panel = new JPanel(deckOfPanels);
    		panel.setBackground(Color.WHITE);
    		
    		JPanel anotherPanel = new JPanel(new FlowLayout());
    		JButton button1 = new JButton("Game Panel");
    		button1.addActionListener(new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					System.out.print("y");
    					deckOfPanels.show(panel,"Game");
    				}
    			}
    		);
    		anotherPanel.add(button1);
    		
    		JButton button2 = new JButton("About Panel");
    		button2.addActionListener(new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					System.out.print("o\n");
    					deckOfPanels.last(panel);
    				}
    			}
    		);
    		anotherPanel.add(button2);
    		frame.getContentPane().add(BorderLayout.NORTH, anotherPanel);
    		frame.getContentPane().add(BorderLayout.CENTER, panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    	}
    	
    	public static void main(String args[])
    	{
    		new Whatever();
    	}
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The components need to be added to the panel, not the layout.

    See: How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) for a working example.

  3. #3
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Thanks, my bad.

    Just writing the correct code here:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    
    
    public class Whatever {
    	
    	CardLayout deckOfPanels;
    	JFrame frame;
    	JPanel panel;
    	JPanel gamePanel;
    	JPanel aboutPanel;
    	
    	public Whatever()
    	{
    		frame = new JFrame("Example");
    		
    		gamePanel = new JPanel();
    		gamePanel.add(new JButton("I'm a button"));
    		
    		aboutPanel = new JPanel();
    		aboutPanel.add(new JLabel("Some stuff"));
    		
    		deckOfPanels = new CardLayout();
    		
    		
    		panel = new JPanel(deckOfPanels);
    		panel.setBackground(Color.WHITE);
    		panel.add(gamePanel, "Game");
    		panel.add(aboutPanel, "About");
    		
    		JPanel anotherPanel = new JPanel(new FlowLayout());
    		JButton button1 = new JButton("Game Panel");
    		button1.addActionListener(new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					System.out.print("y");
    					deckOfPanels.show(panel,"Game");
    				}
    			}
    		);
    		anotherPanel.add(button1);
    		
    		JButton button2 = new JButton("About Panel");
    		button2.addActionListener(new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					System.out.print("o\n");
    					deckOfPanels.last(panel);
    				}
    			}
    		);
    		anotherPanel.add(button2);
    		frame.getContentPane().add(BorderLayout.NORTH, anotherPanel);
    		frame.getContentPane().add(BorderLayout.CENTER, panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    	}
    	
    	public static void main(String args[])
    	{
    		new Whatever();
    	}
    }

Similar Threads

  1. Question about cardlayout
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 04-02-2011, 08:05 PM
  2. Using a FlowLayout on top of a CardLayout
    By snieuw in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 06:03 PM
  3. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 01:03 AM
  4. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 PM
  5. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 09:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •