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 5 of 5

Thread: How to use CardLAyout without using applets???

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to use CardLAyout without using applets???

    I tried it but at the compile time it throwed IllegalArgumentException.
    my code is....
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    public class cards extends JFrame
    {
    	JPanel p1,p2,cards;
    	JLabel l1,l2;
    	JButton b1,b2;
    	public cards()
    	{
    		//super("card1");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLayout(new CardLayout());
    		l1=new JLabel("card1....");
    		l2=new JLabel("card2....");
    		b1=new JButton("b1....");
    		b2=new JButton("b2....");
    		p1=new JPanel(new FlowLayout());
    		p2=new JPanel(new FlowLayout());
    		p1.add(l1);
    		p1.add(b1);
    		p2.add(l2);
    		p2.add(b2);
    		add(p1);
    		add(p2);
    	}
    	public static void main(String[] args)
    	{
    		cards c=new cards();
    		c.setSize(300,300);
    		c.setVisible(true);
    	}
    }

    i m jst a beginner!!!!!!!!!!!
    Last edited by KevinWorkman; July 7th, 2011 at 08:12 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to use CardLAyout without using applets???

    How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    I added highlight tags for you this time, don't forget them in the future. Also, see the link in my signature on asking smart questions. What's happening in your code? What did you expect to happen? Another piece of advice- use standard naming conventions. Classes start with uppercase letters.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    divyarattan (July 8th, 2011)

  4. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How to use CardLAyout without using applets???

    Quote Originally Posted by divyarattan View Post
    I tried it but at the compile time it throwed IllegalArgumentException.
    If you want help with exceptions or error messages, post the full text of the error message, complete with stack trace if present.

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to use CardLAyout without using applets???

    Please post in the correct forum. Thread moved to - AWT / Java Swing
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. The Following User Says Thank You to JavaPF For This Useful Post:

    divyarattan (July 8th, 2011)

  7. #5
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: How to use CardLAyout without using applets???

    Hi divyarattan,

    You are using the CardLayout incorrectly:

    setLayout(new CardLayout());
    add(p1);

    The correct way is:
    setLayout(new CardLayout());
    add(p1, "Name_of_the_card");

    For each component added to the card, you should give it a name, so later you can show that card:

    ((CardLayout) getLayout()).show("Name_of_the_card"))

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:25 PM.

Similar Threads

  1. New to applets
    By Asteroid555 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 30th, 2011, 03:48 PM
  2. Cardlayout Question
    By wagb278 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 10th, 2011, 03:23 PM
  3. Help With Applets
    By mukilan in forum AWT / Java Swing
    Replies: 12
    Last Post: February 2nd, 2011, 11:18 PM
  4. Cardlayout help
    By phantomswordsmen in forum AWT / Java Swing
    Replies: 1
    Last Post: December 22nd, 2010, 02:36 PM
  5. Applets
    By santiagomez in forum Java Theory & Questions
    Replies: 2
    Last Post: July 31st, 2010, 04:05 PM