Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

verification

SweetPSweetP Member Posts: 35
verification

I need to be certain I know what I'm doing here.
Please let me know! Thanks!

##########################

This line gets the content of the content pane
and assigns it to an object variable container.
What does the content pane include ? Is it the
area within the frame ?

Container container = getContentPane();

##########################

Is this:
Container container = getContentPane();

equivalent to this:

Container container = new Container();
container = getContentPane();

##########################

Is this:
container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

equivalent to this:

FlowLayout x = new FlowLayout(FlowLayout.LEFT, 10, 20);
container.setLayout(x);

##########################

The following program is from a book I am using.
I think I understand what's going on but I need
to know for sure. Please correct me if I'm wrong!
Here's what I think is going on:

1. Get the content pane of the frame. The content
pane is the area within the frame.
2. assign it to an object (called container) to
make the content pane accessible.
3. add buttons to the frame; ie add buttons to the
container according to the layout manager, which in
this case is FlowLayout.
4. create an instance of FlowLayout; ie a frame in
main().
5. set frame title and size.
6. assure frame is visible.
7. ready to run.

import.javax.swing.JButton;
import.javax.swing.JFrame;
import.java.awt.Container;
import.java.awt.FlowLayout;

public class ShowFlowLayout extends JFrame
{
//Default constructor
public ShowFlowLayout()
{
//Get the content pane of the frame
Container container = getContentPane();

//Set FlowLayout
container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

//Add buttons
for(int i=1; i<=10; i++)
container.add(new Jbutton("Component " + i));
}

//Main
public static void main(String[] args)
{
ShowFlowLayout frame = new ShowFlowLayout();
frame.setTitle("Show FlowLayout");
frame.setDefaultCloseOperation("JFrame.EXIT_ON_CLOSE");
frame.setSize(200,200);
frame.setVisible(true);
}
}

##########################

I'm not sure I understand the flow of this statement.
Please correct if I'm wrong:
f is a frame;
p is a panel;

f.getContentPane().add(p);

1. Add panel p to frame f.
2. Evaluate getContentPane to get the content of
the content pane and return that value to the frame.
3. The frame now has the panel p.
4. Ready to run with panel p included.

##########################

Comments

  • chamsterchamster Member Posts: 662
    : What does the content pane include?

    Nothing yet (except for the ability of receiving buttons, labels and such and placing them in a nice manner). [b]You[/b] add things (visual things) to it.

    : Is it the area within the frame ?

    Yes. There are four of them and every each of the panes has a different purpose. The purpose of content pane is to hold, well, contents :-). I'm not totally honest with you now and you don't want me to get honest yet either. Think of the content pane as the essential part of the frame.

    There are more explicit (and correct) explaination on Sun's site. Would you like a link?


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • chamsterchamster Member Posts: 662
    : Container container = getContentPane();
    : equivalent to this:
    : Container container = new Container();
    : container = getContentPane();

    Yes it is. You don't need to [b]create[/b] a new Container, though. You get one by [blue]getContentPane ()[/blue] and the one that you've created dies. It's like:
    [blue]
    BestFriend f = new BestFriend ();
    f = getWife ();
    [/blue]
    At the end, you don't get to keep the newly created Friend (or Container) anyway. But it [b]is[/b] eqiuvalent.



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • chamsterchamster Member Posts: 662
    : container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
    : equivalent to this:
    : FlowLayout x = new FlowLayout(FlowLayout.LEFT, 10, 20);
    : container.setLayout(x);

    Yes. There will be a slight difference in code if you'd like to get the layout. In one case you just referre to [blue]x[/blue] in the other you'll use [blue].getLayout ()[/blue]. The funcionality is the same, though.




    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • chamsterchamster Member Posts: 662
    : 1. Get the content pane of the frame. The content pane is the area within the frame.

    It's [b]an[/b] area, not [b]the[/b] area.

    : 2. assign it to an object (called container) to make the content pane accessible.

    It is already accessible (by [blue].getContentPane ()[/blue]). However, some people find it more convenient to call it something. It also produces few more but havily shorter rows.

    : 3. add buttons to the frame; ie add buttons to the container according to the layout manager, which in this case is FlowLayout.
    : 4. create an instance of FlowLayout; ie a frame in main().
    : 5. set frame title and size.
    : 6. assure frame is visible.

    Yes.


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • chamsterchamster Member Posts: 662
    : f.getContentPane().add(p);

    : 1. Add panel p to frame f.
    No. You add p to the content pane (which happens to be content pane of f).

    : 2. Evaluate getContentPane to get the content of the content pane and return that value to the frame.
    See it as a house (frame) and a garage (content pane). Do you park your car in your house? No, you park your car in the garage (which happens to be "held" or "connected" to the house).

    : 3. The frame now has the panel p.
    No. The frame still has only the content pane.

    : 4. Ready to run with panel p included.
    Well, yes, in a way.

    In Swing, JFrame is too lazy to work with components. It only delegates some dude (pane) to do it for it. Basically, what you work with is not JFrame, only a part of it.

    I split the your message to make the answers more comprendable. I hope it helped.



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • Andre YoungAndre Young USAMember Posts: 0

    __ // http://forcoder.org // free ebooks and video tutorials about / Ruby, C++, JavaScript, Go, PL/SQL, C, MATLAB, Java, R, Visual Basic .NET, C#, Delphi, PHP, Swift, Python, Objective-C, Visual Basic, Perl, Scratch, Assembly Lisp, LabVIEW, Awk, Scala, Fortran, Clojure, Prolog, Dart, Hack, Lua, SAS, FoxPro, ABAP, Bash, ML, Ada, Scheme, VBScript, Transact-SQL, Julia, Crystal, Kotlin, D, COBOL, Rust, Alice, Apex, F#, Erlang, Logo / _______

Sign In or Register to comment.