Java learning-AWT layout component

Source: Internet
Author: User

Java learning-AWT layout component

Take note of several window components in the form program developed by AWT in java.

 

FlowLayout:

 

Import java. awt. *; public class Wintwo2 extends Frame {// defines 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 () {// set the window name. This. setTitle ("survey information card"); // set the 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 position and size of the window. 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 {// defines the 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 the window name this. setTitle ("tool BorderLayout"); // you can specify the layout manager BorderLayoutthis. setLayout (new BorderLayout (); // place the button component in the specified position in the window. 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 the 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 (100,100,450,450); this. setVisible (true);} public static void main (String [] args) {// TODO Auto-generated method stubnew Winfour2 ();}}


 

 

CardLayout:

 

Import java. awt. *; import java. awt. event. *; public class Winfive1 extends Frame implements ActionListener {// define a 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 ("last"); Button bg = new Button ("Search "); // define a single row text box TextField tf = new TextField (); // set the layout manager CardLayout cl = new CardLayout (); Winfive1 () {// set the window name this. setTitle ("using CardLayout layout component"); this. setBackground (Color. blue); this. setResizable (false); this. setLayout (null); this. add (p); // define the p Panel as the cardlayout layout manager p. setLayout (cl); // Add a component for the 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. setBounds (10, 40,100,100); this. add (bf); bf. addActionListener (this); bf. setBounds (120, 40, 60, 20); this. add (bl); bl. addActionListener (this); bl. setBounds (120, 70, 60, 20); this. add (bn); bn. addActionListener (this); bn. setBounds (120,100, 60, 20); this. add (bp); bp. addActionListener (this); bp. setBounds (120,130, 60, 20); this. add (bg); bg. addActionListener (this); bg. setBounds (60,160, 40, 20); this. add (tf); tf. setBounds (20,160, 40, 20); // you can specify the position and size of the window. setBounds (200,200,400,380); // set the window visibility this. setVisible (true);} // implement the actionreceivmed method public void actionreceivmed (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);} // 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. *; public class Winnull2 extends Frame {// defines three buttons: Button b1 = new Button ("A"); Button b2 = new Button ("B "); button b3 = new Button (""); public Winnull2 () {// set the window name this. setTitle ("null layout"); // sets the null layout manager this. setLayout (null); // Add the component to the container this. add (b1); this. add (b2); this. add (b3); // set the component size and position b1.setBounds (50, 50,100, 50); b2.setBounds (50,120,100, 50); b3.setBounds (50,190,100, 50 ); // set the 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 ();}}


 

 

Related Article

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.