GUI--AWT frame and container, create simple form, guiawt frame form
GUI Overview: GUI (Graphical User Interface)-Graphical User Interface. Users and programs can easily interact with each other through the GUI. in Java, JFC (Java Foundation Classed) is an API set for GUI development. It consists of the following parts:
A) AWT (Abstract Window Toolkit): The original toolkit for Java Development user interface, which is the main basis for building JFC;
B) Swing components: a new and more powerful graphical component package Built on AWT;
C) JAVA 2D: Implements high-quality 2D images;
AWT framework:
① The java. awt package contains a complete class set to support GUI program design. The classes and their relationships can be used to describe:
② The Component class is the core class, which forms the basis of the Java graphical user interface. Most components are derived from this class;
③ The Component class is mainly composed of basic components and Container components;
④ Container components are mainly divided into Window Container and Panel Container;
Frame container:
① Window is a container that can exist independently. It has a sub-class Frame, which is a Window with a title and a zoom angle;
② Frame has a constructor, Frame (String title );
③ You can add other components to the Frame container by using the add () method;
④ The Frame container has the default layout manager;
⑤ The Frame is invisible after it is created;
Panel container:
① A Panel can only be displayed in other containers (Windows or its subclass;
② You can create a Panel using the default Panel constructor Panel;
③ Containers can not only accommodate components, but also other containers, and can make complex la s through container nesting;
Create a form class
1 class MyFrame1 extends Frame {2 public MyFrame1 (String title) {3 super (title); 4} 5 6 public void init () {7 this. setSize (300,300); // set the width and height of the form to 8 this. setBackground (Color. GREEN); // set the background color for the Form 9 this. setVisible (true); // set the form to be visible 10} 11}
Main method call
1 MyFrame1 myFrame1 = new MyFrame1 ("your own form"); 2 myFrame1.init ();
Output result
Create a form with Panel and Button:
1 class MyFrame2 extends Frame {2 public MyFrame2 (String title) {3 super (title); 4} 5 6 public void init () {7 this. setSize (300,300); // set the width and height of the form to 8 this. setBackground (Color. GREEN); // set the background color for the Form 9 this. setLayout (null); // remove the default layout manager 10 11 Panel = new panel (); 12 Panel. setLayout (null); // remove the default layout manager 13 Panel of the panel container. setBackground (Color. BLUE); 14 panel. setBounds (0, 0,200,200); // distance from the vertex on the parent container (Frame), left vertex 0, width and height 20015 16 Button btn1 = new Button ("click me "); // create a button 17 btn1.setBounds (0, 0,100,100); 18 btn1.setBackground (Color. RED); 19 20 panel. add (btn1); // add button to Panel 21 22 this. add (panel); // add the panel to the Form 23 24 this. setVisible (true); // make the form visible 25} 26}
Output result