Results 1 to 3 of 3
Thread: CardLayout Trouble
- 06-22-2011, 08:34 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
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(); } }
- 06-22-2011, 09:35 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 06-23-2011, 11:04 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
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
-
Question about cardlayout
By scheffetz in forum New To JavaReplies: 1Last Post: 04-02-2011, 08:05 PM -
Using a FlowLayout on top of a CardLayout
By snieuw in forum New To JavaReplies: 1Last Post: 11-08-2010, 06:03 PM -
Help with CardLayout
By Kyle227 in forum New To JavaReplies: 4Last Post: 05-28-2010, 01:03 AM -
Regarding CardLayout
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-07-2008, 07:52 PM -
Using previous with CardLayout
By uncopywritable in forum New To JavaReplies: 2Last Post: 08-05-2007, 09:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks