Container and layout Configuration

Source: Internet
Author: User

1. Java AWT

Container (container): window (FRAME), Panel

Component (Component): button ,,,

Others: layout, color ,,,

2, container (container)

1) Frame

public class FrameTest {public static void main(String[] args) {Frame f1 = new Frame();Frame f2 = new Frame();f1.setSize(100, 200);f2.setSize(200,100);f1.setVisible(true);f2.setVisible(false);}}

2) Panel

Import Java. AWT. *; import Java. applet. *; public class appletpaneltest extends applet // note the panel Implementation Method {public void Init () {panel P = new Panel (); p. setbackground (color. red); p. setsize (100,100); setbackground (color. green); setsize (200,200); add (p );}}

3. layout Configuration

1) borderlayout (east, west, north, and South)

Frame F = new frame ("borderlayout test ");
Button b1 = new button ("one ");
F. Add (B1, borderlayout. East );

2) flowlayout)

3) cardlayout

4) gridlayout

Frame F = new frame ("gridlayout test ");

F. setlayout (New gridlayout (2, 3); // two rows and three columns

5) gridbaglayout design your own Layout

Import Java. AWT. *; public class gridbaglayoutstep1 {public static void main (string argv []) {frame F = new frame ("gridbaglayout Step 1"); F. setlayout (New gridbaglayout (); button B [] = new button [4]; int ATT [] [] = {0, 0, 1, 1}, {1, 0, 1, 1}, {2, 0, 1, 1}, {0, 1, 3, 1}; For (INT I = 0; I <B. length; I ++) {B [I] = new button ("button" + (I + 1); add (F, B [I], ATT [I]);} f. pack (); F. setvisible (true);} Private Static void add (container con, component COM, int ATT []) // Add the com {gridbagconstraints cons = new gridbagconstraints () component according to the limitation array ATT in the container con; // obtain a restriction object cons. gridx = ATT [0]; cons. gridy = ATT [1]; cons. gridwidth = ATT [2]; cons. gridheight = ATT [3]; con. add (COM, cons); // Add the component COM in the container according to the limitation cons }}

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.