AWT graphical user interface design (layout manager 2)

Source: Internet
Author: User

4. cardlayout

The cardlayout layout manager helps users process two or more members sharing the same display space. It divides the container into many layers, and the display space on each layer occupies the entire container size, however, only one component can be placed at each layer. Of course, a panel can be used at each layer to implement a complex user interface. Card layout manager (cardlayout) has 54 cards, just like a pair of neatly stacked cards, but you can only see the top card, each card is equivalent to each layer in the card layout manager.

Example 5.7
Import java. AWT .*;
Import java. AWT. event. *; // the content of the next section.
Public class threepages implements mouslistener {
Cardlayout layout = new cardlayout (); // instantiate a layout manager object
Frame F = new frame ("cardlayout ");
Button page1button;
Label page2label; // label is a label, which is actually a line of strings.
Textarea page3text; // text area with multiple rows and multiple columns
Button page3top;
Button page3bottom;
Public static void main (string ARGs [])
{New threepages (). Go ();}

Public void go ()
{F. setlayout (layout); // set it to the layout manager layout.
F. Add (page1button = new button ("button page"), "page1button");/* The second parameter "page1button" indicates your name for this layer */
Page1button. addmouselistener (this); // registers the listener
F. Add (page2label = new label ("label page"), "page2label ");
Page2label. addmouselisener (this); // registers the listener
Panel = new Panel ();
Panel. setlayout (New borderlayout ());
Panel. Add (page3text = new textarea ("Composite page"), "center ");
Page3text. addmouselistener (this );
Panel. Add (page3top = new button ("top button"), "North ");
Page3top. addmouselistener (this );
Panel. Add (page3bottom = new button ("bottom button"), "South ");
Page3bottom. addmouselistener (this );
F. Add (panel, "Panel ");
F. setsize (200,200 );
F. setvisible (true );
}
......
}

5. Nesting of containers

In the complex graphic user interface design, in order to make the layout easier to manage, it has a simple overall style, A container containing multiple components can also be added to another container as one component, and then added to the container. This forms the nested container. The following is an example of container nesting.

Example 5.8
Import java. AWT .*;
Public class exgui3 {
Private frame F;
Private panel P;
Private button BW, BC;
Private button bfile, bhelp;
Public static void main (string ARGs [])
{
Exgui3 GUI = new exgui3 ();
Gui. Go ();
}
Public void go (){
F = new frame ("Gui Example 3 ");
BW = new button ("West ");
BC = new button ("work space region ");
F. Add (BW, "West ");
F. Add (BC, "center ");
P = new Panel ();
F. Add (P, "North ");
Bfile = new button ("file ");
Bhelp = new button ("help ");
P. Add (bfile );
P. Add (bhelp );
F. Pack ();
F. setvisible (true );
}
}

View running results
  
Knot:

1. frame is a top-level window. The default layout manager of frame is borderlayout.

2. The Panel cannot be displayed separately and must be added to a container. The default layout manager of the Panel is flowlayout.

3. After a panel is added to a container as a component, the Panel can still have its own layout manager. Therefore, you can use panel to display multiple components in a region of borderlayout to design a complex user interface.

4. if you use setlayout (null), you must use setlocation (), setsize (), setbounds (), and other methods to manually set the size and position of the component, not encouraged.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.