Java Basic Learning Summary--gui programming (i) not yet read carefully

Source: Internet
Author: User

I. Introduction of AWT

  

All graphical elements that can be displayed are called Component,component, which represent all visible graphic elements, and component have a special graphical element called Container,container (container). Inside the graphical interface is a kind of container that can hold other component elements, container itself is a kind of component, container inside can also accommodate other container.

Container inside is divided into window and Pannel,window can be displayed independently, usually we see a variety of application windows can be called Window,window as an application window independent display, Pannel can also accommodate other graphic elements, but generally invisible pannel,pannel can not be displayed as a separate window of the application, Pannel must be loaded into the window to display it.

Pannel application is typically the applet (Java's page applet), now basically no longer, Ajax and JavaScript completely replace its application.

Window itself can be divided into frame and dialog,frame is what we usually see the General window, and dialog is those who require the user to do something (such as clicking on a drop-down menu items) only appear dialog box, this dialog is dialog.

Ii. Components and Containers (component and container)

  

2.1.Frame

  

Frame Example:

1 package cn.javastudy.summary; 2  3/** Learn Java GUI programming the first graphical interface window */4 import java.awt.*; 5 6 Public  class Testframe {7 public     static void main ( String args[]) {8         frame f = new Frame ("My First Java Graphical interface window"); 9         /*10          * This just creates a window object in memory and it doesn't really show up          . */12         F.setbackground (color.blue);//Set the background color of the form         //Blue is a static constant in the color class, you can use the "class name. Static constant name" to access         F.setvisible (TRUE);//Set the form to be visible         /*16          * To see the Window object created in memory, you must call the Setvisble () method and pass the parameter true to see the form If the passed parameter is false, then the form is also invisible to          */18         f.setsize (400, 400);//Set the initial size of the form to         f.setlocation (200, 200);// Sets the position of the form when it appears, and defaults to the upper-left corner (0,0) if not set         f.setresizable (false),         /*22          * Sets whether the form can be resized, Set to False to indicate that the form's display size cannot be changed here the size of the form is set to 200x200, so the form can only be displayed in this size, no longer using the mouse to drag large or narrow the          */24     }25}

The results of the operation are as follows:

  

 1 package cn.javastudy.summary; 2 3 Import java.awt.*; 4 public class testmultiframe{5 public static void Main (String args[]) {6 MyFrame f1 = new MyFrame (100,100,20 0,200,color.blue); 7 MyFrame F2 = new MyFrame (300,100,200,200,color.yellow); 8 MyFrame f3 = new MyFrame (100,300,200,200,color.green); 9 MyFrame f4 = new MyFrame (300,300,200,200,color.magenta); 10}11}12/* Customize a class MyFrame and inherit 13 from the frame class so the MyFrame class is Owns all the properties and methods of the frame class 14 and the MyFrame class can also customize properties and methods 15 Therefore, using a custom class inherited from the frame class to create a graphics window is more flexible than using the frame class to create a graphics window 16      So the general use of custom classes inherited from the frame class to create a graphical window interface is good, 17 is not recommended directly using the frame class to create a graphical window interface */19 class MyFrame extends frame{20 static int id = 0;21 Defines a static member variable ID used to record the number of Windows created myframe (int x,int y,int w,int h,color Color) {23//Custom composition method, using super tone in the construction method body Use the parent frame to construct the super ("MyFrame" + (++id)); SetBackground (color); 26/* Use the method inherited from the parent class frame to set the related properties of the form */2  7 setlayout (NULL); SetBounds (x,y,w,h); setvisible (true); 30   }31} 

Operation Result:

  

2.2.Panel

  

Panel Example:

1 package cn.javastudy.summary; 2  3 import java.awt.*; 4 public class testpanel{5 public     static void Main (String args[]) {6         Frame f = new Fra Me ("JAVA Fram with panel"); 7         Panel p = new Panel (null), 8         f.setlayout (NULL), 9         f.setbounds (300,300,500,500);//coordinates set here (300,300) is a         f.setbackground (new Color (0,0,102)) relative to the entire screen,///The background color is set using the three primary colors (red, green, blue) to blend the background color by one         p.setbounds ( 50,50,400,400);//The coordinates set here (50,50) are the P.setbackground (new Color (204,204,255)) relative to the frame form, and         F.add (P);// The Panel container is loaded into the frame container so that it can be displayed in the frame window         f.setvisible (true);     }16}

The results of the operation are as follows:

  

Third, layout manager

  

3.1. First layout manager--flowlayout

  

  

Test code:

 1 package cn.javastudy.summary; 2 3 Import java.awt.*; 4 5 public class Testflowlayout {6 public static void Main (String args[]) {7 frame F = new Frame ("flowlayou T "); 8/* 9 * Use the button class to create one of the construction methods of a Push button class: button (String label) label the text displayed for the button */11 button btn          1 = New button ("Button1"), button btn2 = New button ("Button2"), Button btn3 = New button ("Button3"); 14 /* SetLayout method definition: public void setlayout (LayoutManager Mgr) */15 f.setlayout (New FlowLayout ());//using Flowing water (Flo         W) line-like layout 16/* Using the layout manager FlowLayout, where the layout takes the default horizontal centering mode */17//F.setlayout (new FlowLayout (Flowlayout.left)); 18         /* Here the Flowlayout.left constant is used in the layout, so the button is set to left aligned */19//F.setlayout (new FlowLayout (Flowlayout.right)); 20 /* Here the Flowlayout.right constant is used in the layout, so the button is set to right-aligned */21 F.add (BTN1), or the created button is placed in the frame form by F.add (BTN2); This does not set the size and position of the button f.add (BTN3); Setting the size and position of the buttons is done by the layout manager. F.setvisiBLE (true); F.setsize (200, 200); 26}27} 

The results of the operation are as follows:

3.2. Second layout manager--borderlayout

  

Test code:

 1 package cn.javastudy.summary; 2 3 Import java.awt.*; 4 5 public class Testborderlayout {6 public static void Main (String args[]) {7 frame F = new Frame ("Borderl Ayout "); 8 Button Btneast = New button ("East"); 9 Button btnwest = New button ("West"), ten button Btnsouth = New button ("South"), one button Btnnorth = New Button ("North"), button Btncenter = New button ("Center"), 13/*14 * When placing the button into the frame form, follow East five It is recommended that you use this method to arrange the form elements, 15 * This makes it easy to check for errors because it is written incorrectly, the compiler will prompt an error. */17 f.add (Btneast, Borderlayout.east ); F.add (Btnwest, borderlayout.west); F.add (Btnsouth, Borderlayout.south); F.add (Btnnorth, B Orderlayout.north); F.add (Btncenter, borderlayout.center); 22/*23 * You can also use this arrangement button to place the button on the frame          A string that uses orientation to specify the position of the button when the form is placed 24 * This direction-oriented string specifies that the button is not recommended to be used once the wrong direction string is incorrectly checked out 25 * Because even the wrong one can still be compiled by 26 */27/*28 * F.Add (Btneast, "East"); * F.add (Btnwest, "West"); * F.add (Btnsouth, "South"); * F.add (Btnnorth, "North");     * F.add (Btncenter, "Center"), */34 f.setsize ($); F.setvisible (true); 36 }37}

Operation Result:

3.3. Third layout manager--gridlayout (Table layout Manager)

  

Test code:

1 package cn.javastudy.summary; 2  3 import java.awt.*; 4  5 public class Testgridlayout {6 public     static void Main (String args[]) {7         Fram E F = new Frame ("GridLayout"); 8         Button btn1 = New button ("Btn1"), 9         button btn2 = New button ("Btn2"), ten         button btn3 = New button ("Btn3"); 11< C9/>button btn4 = New button ("Btn4"),         button btn5 = New button ("Btn5"),         button btn6 = New button ("Bnt6"); 14< C12/>f.setlayout (New GridLayout (3, 2));         */* Divide the layout into 3 rows and 2 columns of table layout form */16         f.add (BTN1);         F.add (BTN2); 18         F.add (BTN3),         F.add (BTN4),         F.add (BTN5), F.add (BTN6), F.pack (         );         F.setvisible (true);     }25}

Operation Result:

3.4. Layout Exercises

These layout managers can be set in the frame, can also be set in the Panel, and the panel itself can be added to the frame inside, so through the frame and panel nested can achieve more complex layout

Implementation code:

 1 package cn.javastudy.summary; 2 3 Import java.awt.*; 4 public class testtenbuttons{5 public static void Main (String args[]) {6/* Here is mainly to set the display form */7 Frame f = new Frame ("Nested use of layout manager"); 8 F.setlayout (New GridLayout (2,1));//divide the entire form into 2 rows and 1 columns of table layout 9 f.setlocation (300,400); F.setsize (400,30         0); F.setvisible (True), F.setbackground (New Color (204,204,255)), 13/* This is primarily the setup of the panel layout */14         panel p1 = new Panel (new BorderLayout ()), panel p2 = new Panel (new GridLayout (2,1)),//P2 table layout using 2 rows and 1 columns 16         panel p3 = new Panel (new BorderLayout ()), 2 panel p4 = new Panel (new GridLayout (2,2)), and//P4 table layout with 2 columns in rows 18 /* This is mainly to add the button element to the panel inside the */19 p1.add ("East (p1-)"), Borderlayout.east), and the P1.add (The New button ("West         (p1-West), Borderlayout.west), P2.add (New button ("P2-button1")), P2.add (New button ("P2-button2")); 23 /*p1 inside nested P2, put P2 inside the button as the middle part of P loaded into P1 */24 P1.add (p2,borderlayout.center);//Add P2 as an element to P1 P3.add ("East (p3-East)"), borderlayout.east); 27 P3.add (New button ("West (p3-)"), borderlayout.west), (int i=0;i<4;i++) {P4.add (new Butto N ("P4-button" +i)),}31/*p3 nested P4, the P4 inside the button as the middle part of the p loaded into the P3 */32 P3.add (p4,borderlayout.center) F.add (p1);//The panel is loaded into the frame so that it can be displayed in the frame form f.add (p3); 36 37} 38}

Operation Result:

Iv. Summary of Layout Manager

  

Java Basic Learning Summary--gui programming (i) not yet read carefully

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.