Class hierarchy structure:
Java. Lang. Object
-- Java. AWT. Component
-- Java. AWT. Container
-- Java. AWT. Window
-- Javax. Swing. jframe
Structure functions:
Jfram ()
Jframe (String title)
Example: jframe1.java
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Public class jframe1 implements actionlistener {
Public jframe1 (){
Jframe F = new jframe ("jframedemo ");
/* To add other components to the jframe, you must obtain the content pane and then add the component to the content pane.
* Compared with AWT, to add a component to frmae in AWT, you only need to call the add () method directly without obtaining
* Content pane is added to the component. Swing seems to have gone through one more procedure, but it brings more powerful and elastic
* The reason is that swing's jframe has the concept of layers, so that the components you put in jframe are not
* This can cause confusion. For example, when a jframe has a jbutton, a menu (jmenu), a pop-up menu ),
* In the tool bar and tool tip, which component should be placed on or
* Below, there is a way to deal with jframe. We will discuss this issue later.
*/
Container contentpane = f. getcontentpane ();
Jbutton B = new jbutton ("Click me to get new window ");
B. addactionlistener (this );
Contentpane. Add (B );
F. Pack ();
F. Show (); // make jframe visible)
F. addwindowlistener (
New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
}
);
}
Public void actionreceivmed (actionevent e ){
Jframe newf = new jframe (); // generate a jframe without a title
Newf. setsize (200,200 );
Newf. Show ();
}
Public static void main (string [] ARGs ){
New jframe1 ();
}
}
4-1-2: Use of the swing container structure and jlayeredpane:
Class hierarchy structure:
Java. Lang. Object
-- Java. AWT. compontent
-- Java. AWT. Container
-- Javax. Swing. jcomponent
-- Javax. Swing. jlayeredpane
The following figure shows the structure of the swing container:
| Grass pane
|
Root pane |
| Content pane
| Layered pane |
| Menu bar
Root pane can be considered as a virtual container, including grass pane, layered pane, content pane, and menu bar. Swing containers, including japplet
, Jframe, jdialog, jwindow and jinternalframe are both constructed in this structure. japplet, jframe, jdialog, and jwindow are both heavyweight containers, only
Jinternalframe is the lightweight container. When we want to add a component to the swing container, it is not directly added to the root pane, but
A member (layered pane or content pane) under rootpane)
Content pane and menu bar are only one layer of layered pane. We call this layer frame-content layer. If you want to know the frame-content layer
What is the layer of layered pane? You can get it by the frame_content_layer class constant in the jlayeredpane class.
From this we can know that layered pane basically has a lot of "Layers", so how can we tell which layer is above or below?
The answer is that z_order.z_order has two integer values, one representing the layer depth and the other representing the position of the same layer. When z_order
The more hours the layer value is, the more the position is at the bottom layer. When the layer value of z_order is larger, the position is at the upper layer. In the jlayeredpane class
Six z_order constants are defined as follows:
Default_layer: The layer value of z_order is 0, which is expressed by INTEGER (0). Generally, if the component we add is not marked as the layer, the default value is
Put the component in this default layer.
Palette_layer: The layer value of z_order is 100, expressed as an integer INTEGER (100). It is located on the default layer and is generally used to place movable
Toolbar (floatable toolbar ).
Modal_layer: The layer value of z_order is 200, expressed as an integer INTEGER (200). It is located on the palette_layer and is generally used to place the dialog box.
(Dialog box ).
Popup_layer: The layer value of z_order is 300, expressed as an integer INTEGER (300). It is located on modal_layer and is generally used in poup
Menu) and toolbar prompt (Tool tips.
Drag_layer: The layer value of z_order is 400, expressed as an integer INTEGER (400), located on top of popup_layer. It is generally used to drag components to make them different
Region.
Frame_content_layer: The layer value of z_order is-30000, expressed as an integer (-30000). Generally, frame content Layer
The bottom layer is layer, which indicates the position of content pane and menu bar. In most cases, we will not change it to this value.
The general program will provide a good automatic Z-order management mechanism, of course, Java is no exception, so in most cases we will not use Z-order, because the system will automatically
It helps us manage it well. You only need to add the required components directly to the content pane without knowing the sequential relationship between them. However, if you must
When dealing with the hierarchical relationship between objects, for example, you can push a drawing object down to the next layer in word, and you have to handle the Z-order relationship yourself.
The following describes how to use jlayeredpane to control object hierarchies:
In this example, seven jlabel objects are stacked layer by layer, and each jlabel object has different Z-order values to form a layer-7 stacked effect!
Jlayeredpane1.java:
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Public class jlayeredpane1 extends jframe {
Public jlayeredpane1 (){
Super ("jlayeredpane ");
/* Define the component depth value from small to large, that is, the size of the Z-order layer.
*/
Integer [] layerconstants = {
Jlayeredpane. default_layer, jlayeredpane. palette_layer,
New INTEGER (101), jlayeredpane. modal_layer, new INTEGER (201 ),
Jlayeredpane. popup_layer, jlayeredpane. drag_layer
};
/* Define the color of each jlabel
*/
Color [] colors = {
Color. Red, color. Blue, color. Magenta, color. Cyan, color. Yellow,
Color. Green, color. Pink
};
Point Position = new point (10, 10 );
Jlabel [] label = new jlabel [7];
Jlayeredpane layeredpane = getlayeredpane (); // obtain the layered pane of the window.
For (INT I = 0; I <7; I ++ ){
Label = createlabel ("th" + (I + 1) + "layer", colors, position );
Position. x = position. x + 20;
Position. Y = position. Y + 20;
// Place the component (jlabel) into the layered pane and give the value of the depth (Z-order Layer.
Layeredpane. Add (Label, layerconstants );
}
Setsize (new dimension (280,280 ));
Show ();
Addwindowlistener (
New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
}
);
}
Public jlabel createlabel (string content, color, point position ){
Jlabel label = new jlabel (content, jlabel. center );
Label. setverticalalignment (jlabel. Top );
Label. setbackground (color );
Label. setforeground (color. Black );
Label. setopaque (true );
Label. setbounds (position. x, position. Y, 100,100 );
Return label;
}
Public static void main (string [] ARGs ){
New jlayeredpane1 ();
}
}
As shown in the preceding example, although the layer of "button1" is lower than "button 1", it is displayed on "button 1" because "button 1" is added
Content pane is not added to layeredpane. Therefore, content pane and the components added to content pane are used for hierarchical comparison.
As shown in the following example, components with smaller Z-order layer values are overwritten by components with larger Z-order layer values. If two
Components are at the same layer and overlap with each other. How can we know the hierarchical relationship between them? The answer is to use another integer of Z-order: position. Position
The relationship between values and the layer value of Z-order is exactly the opposite. For objects in the same layer, if the position value is smaller, it is in the upper layer. If the position value is greater
In the lower layer. The position value ranges from-1 ~ N ~ 1, n indicates the number of components in the same layer. Value-1 indicates the bottom layer, which means the same as n-1. Value 0 indicates the top layer.
You can use the movetoback () method provided by the jlayeredpane class to push the component to the position of-1 (bottom), or use the movetofront () method.
Push the component to the position 0 (top. Let's take a look at the following example: jlayeredpane3.java
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Public class jlayeredpane3 extends jframe {
Public jlayeredpane3 (){
Super ("jlayeredpane ");
Jlabel label1 = new jlabel ("Left label", jlabel. center );
Label1.setverticalalignment (jlabel. Top );
Label1.setbackground (color. Red );
Label1.setforeground (color. Black );
Label1.setopaque (true );
Label1.setbounds (150,150 );
Jlabel label2 = new jlabel ("right slabe2", jlabel. center );
Label2.setverticalalignment (jlabel. Top );
Label2.setbackground (color. Red );
Label2.setforeground (color. Black );
Label2.setopaque (true );
Label2.setbounds (50, 50, 150,150 );
Jlayeredpane layeredpane = getlayeredpane ();
Layeredpane. Add (label1, new INTEGER (10), 1 );
Layeredpane. Add (label2, new INTEGER (10), 0 );
Setsize (new dimension (280,280 ));
Show ();
Addwindowlistener (
New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
}
);
}
Public static void main (string [] ARGs ){
New jlayeredpane3 ();
}
}