Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: card layout

  1. #1
    Junior Member tonu's Avatar
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default card layout

    What's Wrong With My Code?

    package cards;
     
    import java.awt.CardLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Card {
     
    	private CardLayout cardLayout; 
    	private JFrame outJFrame;
    	private JPanel panel1, panel2 , panel3 ;
    	private JButton but1, but2, but3 ;
    	private CardLayout cards ;
     
    	public Card()
    	{
    		cardLayout = new CardLayout();
    		outJFrame = new JFrame("Cards");
    		outJFrame.setSize(500, 400);
     
    		but1 = new JButton("but 1");
    		panel1.add(but1);
     
    		but2 = new JButton("but 2");
    		panel2.add(but2);	
     
    		but3 = new JButton("but 3");
    		panel3.add(but3);			
     
    		panel1 = new JPanel();
     
    		outJFrame.add(panel1);
     
    		panel2 = new JPanel();
    		outJFrame.add(panel2);		
     
    		panel3 = new JPanel();
    		outJFrame.add(panel3);		
     
     
    		 outJFrame.add( panel1, "First");
    	              // add panel1 with name "First"
    		 outJFrame.add( panel2, "Second");
    	              // add panel2 with name "Second"
    		 outJFrame.add( panel3, "Third");
    		 outJFrame.setLayout(cardLayout);
     
     
     
    		outJFrame.setLocationRelativeTo(null);
    		outJFrame.setVisible(true);
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: card layout

    Give us a hint. Why do you ask the question in the thread title?

    A hint: Programming is a sequential set of small steps to accomplish a larger objective, and the order of those steps almost always matters. If there's a reason to set the layout manager of the container before contents are added, then perhaps the step of setting the layout manager from other than the default should occur BEFORE the steps that add the contents.

  3. #3
    Junior Member tonu's Avatar
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: card layout

    just i want to learn a card layout , i read some and try to help myself

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: card layout

    Quote Originally Posted by tonu View Post
    What's Wrong With My Code?
    panel1/2/3 are assigned with new JPanel only after the adds. So on the adds you get a NullPointerException. And why did you do a outJFrame.add(panel1); and then after another outJFrame.add( panel1, "First"); (same for 2/3) ?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: card layout

    He is saying to set the layout before you add the panels to outJFrame, not after (like you currently do).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Junior Member tonu's Avatar
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: card layout

    could you please write it to me , i am confuse

Similar Threads

  1. GUI Layout - Does anyone know how to set a layout?
    By mikemontesa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2013, 04:35 PM
  2. [SOLVED] .show is called in card layout, but the frame didn't update
    By nggdowt in forum AWT / Java Swing
    Replies: 2
    Last Post: November 9th, 2011, 02:16 AM
  3. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  4. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM
  5. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM

Tags for this Thread