Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Simple Card Layout Problem
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
_Dan_




PostPosted: Thu Sep 11, 2008 9:09 pm   Post subject: Simple Card Layout Problem

I'm just starting to learn about layouts and I can't understand why the following code doesn't execute correctly. It's supposed to display Page 2 but instead it Displays Page 0 (the first page that was added to the applet), can anyone tell me what's wrong :S? Also, how do I implement a key listener that will detect when I press a key down in the active JPanel. Help is much appreciated!

code:
public class LayoutTest extends java.applet.Applet
{
    //Sets up the blank pages
    final int noOfPages = 3;
    JPanel[] pages = new JPanel [noOfPages];
    public void init ()
    {
        JLabel[] text = {new JLabel ("One"), new JLabel ("Two"), new JLabel ("Three") };

        for (int i = 0 ; i < noOfPages ; i++)
        {
            //Creates a page
            pages [i] = new JPanel ();

            //Adds text to the created page
            pages [i].add (text [i]);

            //Adds the created page to the applet
            add ("Page " + i, pages [i]);
        }

        //Sets the layout to the card layout
        CardLayout card = new CardLayout ();
        setLayout (card);

        //Shows the first page
        card.show (this, "Page 2");

        requestFocus ();
    }
}
Sponsor
Sponsor
Sponsor
sponsor
TheFerret




PostPosted: Fri Sep 12, 2008 2:02 am   Post subject: RE:Simple Card Layout Problem

Take a look here

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html
_Dan_




PostPosted: Fri Sep 12, 2008 3:45 pm   Post subject: Re: Simple Card Layout Problem

EDIT: {Prolly best to learn these things yourself as I did, anyways, resolved}

Still doesn't work, now it displays none of the labels :<.
code:
public class LayoutTest extends java.applet.Applet
{
    //Sets up the blank pages
    final int noOfPages = 3;
    JPanel[] pages = new JPanel [noOfPages];

    public void init ()
    {
        //Creates the main page and sets it layout to the card layout
        JPanel mainPage = new JPanel (new CardLayout ());

        //Creates the text to go onto each page
        JLabel[] text = {new JLabel ("One"), new JLabel ("Two"), new JLabel ("Three") };

        for (int i = 0 ; i < noOfPages ; i++)
        {
            //Creates a page
            pages [i] = new JPanel ();

            //Adds text to the created page
            pages [i].add (text [i]);

            //Adds the created page to the main page
            mainPage.add (pages [i], "Page " + i);
        }

        CardLayout cl = (CardLayout) (mainPage.getLayout ());
        cl.show (mainPage, "Page 0");
    }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: