Thanks for the link, Michèle.  The interesting thing is that I didn't
need to the create a JPanel to do my layout.  Immediately after
creating my JFrame, I just set the layout directly on it.

    JFrame frame = new JFrame("It's time to play SHOW THAT FLAG");
    frame.setLayout(new BoxLayout(
        frame.getContentPane(), BoxLayout.PAGE_AXIS));

This article, 
http://weblogs.java.net/blog/hansmuller/archive/2005/11/jframeadd_conte.html,
explains that this is actually an illusion -- a convenience feature
that actually sets the layout on the content pane of the frame.  The
above is equivalent to this:

    JFrame frame = new JFrame("It's time to play SHOW THAT FLAG");
    frame.getContentPane().setLayout(new BoxLayout(
        frame.getContentPane(), BoxLayout.PAGE_AXIS));

Even more interesting is that I never set the content pane of the
frame before setting the content pane's layout.  How can I set the
layout on an object that doesn't exist?  What I found out is that
every JFrame comes with a default content pane.  What type of object
is it?  Well let's find out...

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        System.out.println("frame.getContentPane() is a "
            + frame.getContentPane().getClass().getName());
    }

    run:
    frame.getContentPane() is a javax.swing.JPanel
    BUILD SUCCESSFUL (total time: 2 seconds)

It's actually a JPanel.  So if the JFrame already includes a default
content pane that's a JPanel, then why create another JPanel to slap
on top of it?  In the LunarPhases example (http://137.166.68.100/java-
tut/uiswing/learn/example5.html), they could have done without the
mainPanel and instead added the selectPanel and displayPanel to the
lunarPhasesFrame's content pane.

One possible advantage that I could see is that the mainPanel is used
to throw a border around the edges of the frame so that the
selectPanel and displayPanel don't seem so cramped.  If that's what
the purpose was, then it wasn't clear to me from reading the tutorial.

Thanks,
Mike


On Jul 21, 2:21 am, Michèle Garoche <[email protected]> wrote:
> On 21 juil, 10:16, "Mike M. Lin" <[email protected]> wrote:> In the Swing 
> homework, the example we key off of (lunar images) is set
> > up with a JFrame which holds a JPanel. that JPanel acts as a container
> > for two other JPanel's - one to house the combo box, the other to
> > house the image.
>
> > What is the purpose of the containing JPanel?  I did my homework
> > assignment without that container and it seems to work just fine. I
> > just put my sub-panels directly on the JFrame.
>
> > It seems pretty useless to me in this program. What am I missing?
>
> You may have a look at this 
> tutorial:http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/...
> Especially the section Tips on choosing a Layout Manager.

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to