Java Learning--awt Layout components

Source: Internet
Author: User

Learn some of the Windows components in the AWT development form program in Java and take notes


FlowLayout:

Import java.awt.*;p ublic class Wintwo2 extends frame{//define a text box. TextArea a=new TextArea ("Please fill in the information accurately");//define three button components. Button B1=new button ("submit"); Button B2=new button ("Cancel"); Button B3=new button ("reset"); WINTWO2 () {//sets the window name. This.settitle ("Survey Information Card");//Set layout manager to FlowLayout. This.setlayout (New FlowLayout ());//Place the button component in the window This.add (a); This.add (B1); This.add (B2); This.add (b3);// Set the window's position and size this.setbounds (100, 100, 450, 350);//Set Window visibility this.setvisible (true);} public static void Main (string[] args) {//TODO auto-generated method Stubnew Wintwo2 ();}}


BorderLayout:

Import Java.awt.*;import Java.awt.peer.textareapeer;public class Winthree1 extends frame{//define five button components textarea b1=new TextArea ("Medium"); TextArea b2=new TextArea ("South"); Button B3=new button ("submit"); Button B4=new button ("Cancel"); Button B5=new button ("reset"); Winthree1 () {//Set window name This.settitle ("sharp tool borderlayout");//Set Layout manager borderlayoutthis.setlayout (new BorderLayout ());// Place the button component into the window's specified position This.add (B1, Borderlayout.center); This.add (B2, Borderlayout.north); This.add (B3, Borderlayout.south); This.add (B4, borderlayout.west); This.add (B5, borderlayout.east);//Set window position and size this.setbounds (300, 200, 450, 350);// Set window Visibility this.setvisible (true);} public static void Main (string[] args) {//TODO auto-generated method Stubnew Winthree1 ();}}



GridLayout:

Import java.awt.*; public class Winfour2 extends Frame{textarea b1=new TextArea ("a"); TextArea b2=new TextArea ("B"); Button B3=new button ("C"); Button B4=new button ("ding"); Button B5=new button ("E"); Winfour2 () {this.settitle ("girdlayout layout") this.setlayout (New GridLayout (2, 3)); This.add (B1); This.add (B2); this.add (b3); This.add (B4); This.add (B5); This.setbounds (+, +, +); This.setvisible (true);} public static void Main (string[] args) {//TODO auto-generated method Stubnew Winfour2 ();}}



CardLayout:

Import Java.awt.*;import java.awt.event.*;p ublic class Winfive1 extends Frame implements actionlistener{//define a faceplate panel p =new Panel ();//define five Buttons button Bf=new button ("First"); Button Bl=new button ("last"); Button Bn=new button ("Next"); Button Bp=new button ("previous"); Button Bg=new button ("search");//define a single-line text box TextField tf=new TextField ();//Set Layout manager cardlayout cl=new cardlayout (); Winfive1 () {//Set window name This.settitle ("Use cardlayout layout component"), This.setbackground (Color.Blue); this.setresizable (false); This.setlayout (null); This.add (P);//define p panel for CardLayout layout manager p.setlayout (CL);//Add component for CardLayout layout manager for (int i=1; i< = 3; i++) {button Btemp=new button ("Layout button" +i);p. Add (Btemp, "" +i);} Set the size position for each component and add it to the container p.setbounds (ten, 40, 120); This.add (BF); Bf.addactionlistener (this); This.add (BL); Bl.addactionlistener (this), bl.setbounds (+,-), This.add (BN); Bn.addactionlistener (This) ; Bn.setbounds (+), This.add (BP), Bp.addactionlistener (This), Bp.setbounds ((), This.add (BG); Bg.addactIonlistener (This), Bg.setbounds (20, 160, 40, 20), This.add (TF); Sets the position and size of the window this.setbounds (200, 200, 400, 380);//sets the visibility of the window this.setvisible (true);} Implement the Actionperformed method in the ActionListener interface public void actionperformed (Java.awt.event.ActionEvent e) {//next method if ( E.getsource () ==bn) {Cl.next (P);} Previous method if (E.getsource () ==bp) {cl.previous (P);} First method if (E.getsource () ==bf) {Cl.first (P);} Last method if (E.getsource () ==bl) {cl.last (P);} The Show method if (E.getsource () ==bg) {cl.show (P, Tf.gettext (). Trim ()); Tf.settext ("");}} public static void Main (string[] args) {//TODO auto-generated method Stubnew Winfive1 ();}}



Null:

Import Java.awt.*;import java.awt.event.*;p ublic class Winnull2 extends frame{//define three buttons button B1=new button ("a"); Button B2=new button ("B"); Button B3=new button ("C");p ublic Winnull2 () {//Set window name This.settitle ("null layout");//Set NULL layout manager this.setlayout (NULL);// Add components to the container This.add (B1); This.add (B2); This.add (b3);//Set the size and position of the component B1.setbounds (50, 120, 100, B3.setbounds (50, 190, 100, 50);//Set window position and size this.setbounds (100, 100, 450, 400);//Set Window visibility this.setvisible (true);} public static void Main (string[] args) {//TODO auto-generated method Stubnew Winnull2 ();}}


Java Learning--awt Layout components

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.