Simple Card Layout Problem
Author |
Message |
_Dan_
|
Posted: 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
|
|
|
TheFerret
|
|
|
|
|
_Dan_
|
Posted: 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");
}
} |
|
|
|
|
|
|
|
|