Wednesday, January 6, 2010
JFrame
The last post was creating a very simple hello world swing application using JFrame, JPanel and JLabel. In this post, I would like to post a bit more about JFrame.
JFrame is one of the three top-level container classes that come with Swing. The rule of thumb is that each Swing application has AT LEAST ONE top-level container. The above figure is a JFrame and in this application, we have one top level container, which is a JFrame. In our JFrame, we have a menu bar(y'all know what a menu bar is) and a content pane(the yellow background). In a containment hierarchy, this is what the above figure looks like
JFrame
|
------- Content Pane
|
------- Menu Bar
Content pane can be customized and new components can be added to our original Swing application. In my previous example, I added a label to the content pane. We can call content pane directly from JFrame by the method frame.getContentPane(). But we used a slightly different approach last time. We created a JPanel component and then added it to JFrame. In my last post, I explained that content pane existed inside JPanel. It was my mistake to misuse the word since I'm using contentpane as a variable name.
What I really meant was that I was trying to create a customized content pane. when we call frame.getContentPane(), we actually get a container object, not a JComponent object. ( I will talk more about JComponent in my next post). Since I wanted to make use of JComponent features, I needed to either type cast or create my own content pane. In this case, I created a JPanel and added to JFrame, making my contentPane JPanel a content pane.
As I mentioned above, JFrame is one of the top-level containers. In Swing, there are two other top-level containters : JDialog & JApplet. Each standalone Swing application has JFrame as root container and each applet application has JApplet has root container. If we have an application that has a dialogue box, then there will be two top-level containers in this case, which are JFrame & JDialog.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment