"AWT"
The AWT (Abstract Window Toolkit) includes a number of classes and interfaces that are programmed for the GUI of the Java application (graphics user interface graphical UI).
Various elements of the GUI (e.g., Windows, buttons, text boxes, etc.) are implemented by Java classes.
The classes involved in using AWT are generally in the java.awt package and its child packages.
Container and component are the two core classes in AWT.
Component{button,textarea,label,textfield,list; Container[window (Frame,dialog), Panel (Applet)]}
"Component & Container"
The most basic component of Java's graphical user interface is the Component,component class and its subclasses that describe GUI elements that graphically display on the screen and interact with the user, such as a button, a label, and so on.
A generic Component object cannot be displayed independently, and it must be "placed" in a container object that can be displayed.
Container is a component subclass, container subclasses can "accommodate" other component objects.
Container objects can use the method add (..) Add other component objects to it.
Container is a subclass of component, so container objects can also be added to other container objects as component objects.
There are two common types of container:
window: A top-level window whose object represents a free berth
Panel: Its objects can be used as accommodating other component objects, but cannot exist independently and must be added to other container (such as window or applet)
"Frame"
A frame is a subclass of window, and an object created by a frame or its subclasses is a form.
Common construction methods for frame:
Frame ()
Frame (string s) creates a window with the title bar as the string s.
SetBounds (int x,int y,int width,int height)
setSize (int width,int height)
setlocation (int x,int y)
SetBackground (Color c)
SetVisible (Boolean B) setting is visible
Settitle (string name) string GetTitle ()
Setresizable (Boolean B) setting whether size can be resized
The layout manager
In the Java language, objects that provide a layout manager class can manage
Manage the layout of component in container, no more than setting component position and size directly.
Each container has a layout manager object that, when the container needs to locate or size a component, calls its corresponding layout manager and calls container's SetLayout method to change its layout manager object.
AWT provides 5 layout manager classes:
FlowLayout
BorderLayout
GridLayout
CardLayout
GridBagLayout
"FlowLayout layout Manager"
FlowLayout is the default layout manager for the Panel class.
The FlowLayout Layout manager sets line-by-row positioning, line-by-row, left-to-right, row-wrapped.
Do not change the size of the assembly, according to the components of the original size display components, you can set different building examples, navigation and alignment.
FlowLayout layout Manager The default alignment is in the play.
"How to construct FlowLayout"
New FlowLayout (flowlayout.right,20,40);
Right-aligned, horizontal spacing between components 20 pixels, vertical spacing of 40 pixels.
New FlowLayout (Flowlayout.left);
Left-aligned, horizontal and vertical spacing defaults (5).
New FlowLayout ();
Using the default center alignment, the horizontal and vertical spacing is the default value (5).
Example:
Import java.awt.*;p Ublic class Testflowlayout {public static void main (string[] args) {frame f = new Frame ("Flow Layout"); Button button1 = New button ("OK"); Button button2 = New button ("Open"); Button Button3 = New button ("Close"), F.setlayout (New FlowLayout ()); F.add (button1); F.add (button2); F.add (Button3); F.setsize (100,100); f.setvisible (True);}}
"BorderLayout layout Manager"
BorderLayout is the default layout manager for the frame class.
BorderLayout divides the layout of the entire container into: East (east) west (west) south (north) (CENTER) Five regions, the build can only be added to the specified area.
If you do not specify a build, it is added to the center area by default.
Only one component can be added to each zone, such as adding multiple, then the previous join will be overwritten.
Example:
Import java.awt.*;
public class Testborderlayout {public static void main (string[] args) {Frame f;f = new Frame ("Border Layout"); Button bn = New Button ("bn"); Button bs = new button ("BS"); Button bw = New button ("BW"); button is = New button ("be"); button BC = New button ("BC"), F.add (BN, "North"), F.add (BS, ' South '), F.add (BW, "West"), F.add (BE, "East"), F.add (BC, "Cen Ter ");/*f.add (BN, Borderlayout.north), F.add (BS, Borderlayout.south); F.add (BW, borderlayout.west); F.add ( Be, borderlayout.east); F.add (BC, Borderlayout.center); */f.setsize (200,200); f.setvisible (True);}}
"GridLayout layout Manager"
The GridLayout layout manager divides the space into regular, rectangular networks with equal sizes for each cell range. The build is added to each cell, a row is filled in from left to right, and then the top-to-bottom line is wrapped.
Specify the number of rows and columns to split in the GridLayout constructor method, such as: GridLayout (3,4)
"Layout Manager Summary"
Frame is a top-level window, and the default layout manager for frame is BorderLayout
The panel cannot be displayed separately and must be added to a container.
The default layout manager for the panel is FlowLayout.
When a panel is added to a container as a component, the Panel can still have its own layout manager.
With the layout manager, the layout manager is responsible for the size and location of the individual components, so users cannot set the component size and location properties in this case, if attempting to use the setlocation (), SetSize (), SetBounds () methods provided in the Java language, will be overwritten by the layout manager.
If you do need to set the component size or location yourself, you should cancel the container's layout manager by:
SetLayout (NULL)
"ActionEvent"
Example:
Import Java.awt.*;import java.awt.event.*;p ublic class Testactionevent {public static void main (string[] args) {Frame F = New Frame ("Test"); Button B = New button ("Press me!"); Monitor BH = new monitor (); B.addactionlistener (BH); F.add (B,borderlayout.center); F.pack (); f.setvisible (True);}} Class Monitor implements ActionListener {public void actionperformed (ActionEvent e) {System.out.println ("a button has bee n pressed ");}}
"TextField class"
The Java.awt.TextField class is used to create a text box object.
TextField has the following common methods:
TextField ()
TextField (int columns)
TextField (String text)
TextField (String text,int columns)
public void SetText (String t)
Public String GetText ()
public void Setechochar (char c) sets the echo character
public void Seteditable (Boolean b)
public boolean iseditable ()
public void SetBackground (Color c)
public void Select (int selectionstart,int selectionend)
public void SelectAll ()
public void addActionListener (ActionListener l) adds an action listener.
"TextField Event Monitoring"
The TextField object may have an action (the cursor will hit enter in the text box) event. The event class corresponding to the change time is java.awt.event.ActionEvent.
The object used to handle the ActionEvent event is a class that implements the Java.awt.event.ActionListener interface. The ActionListener interface defines a method:
public void actionperformed (ActionEvent e)
The class that implements the interface adds the statement that handles the event (Action) in the method.
Use the addActionListener (ActionListener L) method to register a ActionListener object for the TextField object, and when the TextField object has an action time, Generates a ActionEvent object that is passed as a parameter to the ActionListener object's Actionperformer method, which can obtain information about the object in the method and handle it accordingly.
Tfactionevent.java//textfield Event Monitoring Preliminary
import java.awt.*;import java.awt.event.*;p ublic class Tfactionevent {public static void Main (string[] args) {new Tfframe ();}} Class Tfframe extends Frame {tfframe () {TextField tf = new TextField (); add (TF); Tf.addactionlistener (New Tfactionlistener ());p ack (); setvisible (True);}} Class Tfactionlistener implements ActionListener {public void actionperformed (ActionEvent e) {TextField tf = (TextField) E . GetSource (); System.out.println (Tf.gettext ()); Tf.settext ("");}} Tfpassword.java//Password import java.awt.*;import java.awt.event.*;p ublic class Tfpassword {public static void main (string[] args) {new TFFrame2 ();}} Class TFFrame2 extends Frame {TFFrame2 () {TextField tf = new TextField (); add (TF); Tf.addactionlistener (new TFActionListener2 ()); Tf.setechochar (' * ');p ack (); setvisible (True);}} Class TFActionListener2 implements ActionListener {public void actionperformed (ActionEvent e) {TextField tf = (TextField) E.getsource (); System.out.println (Tf.gettext ()); Tf.settext ("");}}
"Hold the other reference"
Tfmath.java//Mathematical Operations inner class
Import Java.awt.*;import java.awt.event.*;p ublic class Tfmath {public static void main (string[] args) {new Tfframe (). Laun Chframe ();}} Class Tfframe extends Frame {textfield[] num = new textfield[3];p ublic void Launchframe () {for (int i=0;i<3;i++) Num[i] = new TextField (10); Label Lblplus = new label ("+"); Button btnequal = New button ("="); Btnequal.addactionlistener (new Mymonitor (this)); SetLayout (new FlowLayout ()); Add ( Num[0]); Add (lblplus); add (num[1]); Add (btnequal); add (num[2]);p ack (); setvisible (True);}} Class Mymonitor implements ActionListener {tfframe tf = null;public mymonitor (tfframe tf) {this.tf = TF;} public void actionperformed (ActionEvent e) {int n0 = Integer.parseint (Tf.num[0].gettext ()); int n1 = Integer.parseint ( Tf.num[1].gettext ()); Tf.num[2].settext ("" + (N0+N1));}}
"Internal class Version" (Change the above Tfmath.java)
Import Java.awt.*;import java.awt.event.*;p ublic class Tfmath {public static void main (string[] args) {new Tfframe (). Laun Chframe ();}} Class Tfframe extends Frame {textfield[] num = new textfield[3];p ublic void Launchframe () {for (int i=0;i<3;i++) Num[i] = new TextField (10); Label Lblplus = new label ("+"); Button btnequal = New button ("="), Btnequal.addactionlistener (New Mymonitor ()); SetLayout (new FlowLayout ()); Add (Num[0] ); Add (lblplus); add (num[1]); Add (btnequal); add (num[2]);p ack (); setvisible (true);} Class Mymonitor implements ActionListener {public void actionperformed (ActionEvent e) {int n0 = Integer.parseint (num[0].g Ettext ()); int n1 = Integer.parseint (Num[1].gettext ()); Num[2].settext ("" + (N0+N1));}}}
"Inner class"
Benefits:
Can easily access the members of the wrapper class
You can more clearly organize your logic to prevent access to classes that should not be accessed by other classes.
When to use:
When the class does not allow or does not require other classes for access.
"Graphics Class"
Each component has a paint (Graphics g) for drawing purposes, and the Paint method is automatically called each time the component is redrawn.
Many drawing methods are available in the graphics class, such as:
DrawRect (int x,int y,int width,int height)
Fillroundrect (int x,int y,int width,int height,int arcwidth,int archeight), etc.
Example:
Import java.awt.*;p Ublic class Testpaint {public static void main (string[] args) {new Paintframe (). Launchframe ();}} Class Paintframe extends Frame {public void Launchframe () {setbounds (200,200,640,480); setvisible (true);} public void Paint (Graphics g) {Color c = g.getcolor (); G.setcolor (color.red); G.filloval (50,50,30,30); G.setcolor ( Color.green); G.fillrect (80,80,40,40); G.setcolor (c);}}
"Mouse Event Adapter"
Abstract class Java.awt.event.MouseAdapter implements the MouseListener interface, you can use the wife class as a mouseevent listener, as long as you rewrite their corresponding methods.
For other listeners, there is also a corresponding adapter.
Using an adapter avoids the need for a listener class to define an empty method.
Repaint () = Update first ()-again paint (); Double buffering
Example:
Import java.awt.*;import java.awt.event.*;import java.util.*;p ublic class Mymouseadapter {public static void main ( String[] (args) {new MyFrame ("Drawing ...");}} Class MyFrame extends Frame {arraylist<point> points = null; MyFrame (String s) {super (s);p oints = new arraylist<point> (); setlayout (null); SetBounds (300,300,400,300); This.setbackground (New Color (204,204,255)); setvisible (true); This.addmouselistener (new Monitor ());} public void Paint (Graphics g) {Iterator i = Points.iterator (), while (I.hasnext ()) {point P = (point) I.next (); G.setcolor (Co Lor. BLUE); G.filloval (p.x,p.y,10,10);}} public void Addpoint (point P) {Points.Add (P);}} Class Monitor extends Mouseadapter {public void mousepressed (MouseEvent e) {myframe f = (myframe) e.getsource (); f.addpoint (New Point (E.getx (), E.gety ())); F.repaint ();}}
"Window Event"
The event class corresponding to window time is windowevent, and the corresponding event listener interface is WindowListener.
The methods defined by WindowListener are:
public void windowopened (WindowEvent e)
public void windowclosing (WindowEvent e)
public void windowclosed (WindowEvent e)
public void windowiconfied (WindowEvent e)
public void windowdeiconified (WindowEvent e)
public void windowactivated (WindowEvent e)
public void windowdeactivated (WindowEvent e)
The adapter that corresponds to the WindowListener is windowadapter.
"KeyEvent" (anonymous class usage)
Example:
Import Java.awt.*;import java.awt.event.*;p ublic class Testanoymous {frame F = new Frame ("Test"); TextField tf = new TextField (10); Button B1 = New button ("Start");p ublic testanoymous () {F.add (B1, "North"); F.add (TF, ' South '); B1.addactionlistener (New A Ctionlistener () {private int i;public void actionperformed (ActionEvent e) {tf.settext (E.getactioncommand () + ++i);}); F.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}}); F.pack (); f.setvisible (true);} public static void Main (string[] args) {new testanoymous ();}}
Java Summary of GUI programming