Você está na página 1de 5

IINTRODUCTION TO AWT AWT stands for the Abstract Window Toolkit.

The AWT contains numerous classes and methods that allow you to create and mana e windows. Althou h the main !ur!ose of the AWT is to su!!ort a!!let windows" it can also be used to create stand#alone windows that run in a $UI en%ironment" AWT Classes The AWT classes are contained in the java.awt !acka e. It is one of &a%a's lar est !acka es. some of the AWT classes. () AWT*%ent + *nca!sulates AWT e%ents. ,) -order.ayout +The border layout mana er. -order layouts use fi%e /) Com!onents + North" 0outh" *ast" West" and Center. 1) -utton +Creates a !ush button control. 2)Checkbo3 Creates a check bo3 control. 4)Checkbo3$rou! Creates a rou! of check bo3 controls. 5)Checkbo36enuItem Creates an on7off menu item. 8)Choice Creates a !o!#u! list. 9)Color + 6ana es colors in a !ortable" !latform#inde!endent fashion. (:) Com!onent + An abstract su!erclass for %arious AWT com!onents. (() Container +A subclass of Component that can hold other com!onents. (,) Dialo +Creates a dialo window. (/) *%ent + *nca!sulates e%ents. (1) ;ileDialo +Creates a window from which a file can be selected. (2) ;low.ayout + The flow layout mana er. ;low layout !ositions com!onents left to ri ht" to! to bottom. (4) ;ont + *nca!sulates a ty!e font. (5) .abel +Creates a label that dis!lays a strin . (8) .ist +Creates a list from which the user can choose. (9) 6enu + Creates a !ull#down menu. ,:) 6enu-ar + Creates a menu bar. ,() 6enuCom!onent+ An abstract class im!lemented by %arious menu classes. ,,) 6enuItem +Creates a menu item. ,/) <anel +The sim!lest concrete subclass of Container.

Window ;undamentals The two most common windows are those deri%ed fromPanel" which is used by a!!lets" and those deri%ed from Frame" which creates a standard window. 6uch of the functionality of these windows is deri%ed from their !arent classes.. Com!onent Class+ At the to! of the AWT hierarchy is the Component class. Component is an abstract class that enca!sulates all of the attributes of a %isual com!onent. All user interface elements that are dis!layed on the screen and that interact with the user are subclasses of Component. It defines o%er a hundred !ublic methods that are res!onsible for mana in e%ents" such as mouse and keyboard in!ut" !ositionin and si=in the window" and re!aintin T>* &A?A .I-RAR@ Container Class The Container class is a subclass of Component. It has additional methods that allow other Component obAects to be nested within it. Other Container obAects can be stored inside of a Container Bsince they are themsel%es instances of ComponentC. This makes for a multile%eled containment system. A container is res!onsible for layin out Bthat is" !ositionin C any com!onents that it contains. It does this throu h the use of %arious layout mana ers". <anel Class+ The Panel class is a concrete subclass of Container. It doesn't add any new methodsD it sim!ly im!lements Container. A Panel may be thou ht of as a recursi%ely nestable"concrete screen com!onent. Panel is the su!erclass for Applet. When screen out!ut is directed to an a!!let" it is drawn on the surface of a Panel obAect. In essence" a Panel is a window that does not contain a title bar" menu bar" or border. This is why you don't see these items when an a!!let is run inside a browser. When you run an a!!let usin an a!!let %iewer" the a!!let %iewer !ro%ides the title and border. Other com!onents can be added to a Panel obAect by its add( ) method. Once these com!onents ha%e been added" you can !osition and resi=e them manually usin the setLocation( )" setSize( )" or setBounds( ) methods defined by Component. Window Class +

The Window class creates a to!#le%el window. A top-level window is not contained within any other obAectD it sits directly on the deskto! ;rame Class Frame enca!sulates what is commonly thou ht of as a Ewindow.F It is a subclass of Window and has a title bar" menu bar" borders" and resi=in corners. If you create a Frame obAect from within an a!!let" it will contain a warnin messa e" such as E&a%a A!!let Window"F to the user that an a!!let window has been created. Can%as Class+ Althou h it is not !art of the hierarchy for a!!let or frame windows" there is one other ty!e of window that you will find %aluable+ Canvas. Canvas enca!sulates a blank window u!on which you can draw. Workin with ;rame Windows After the a!!let" the ty!e of window you will most often create is deri%ed from Frame. @ou will use it to create child windows within a!!lets" and to!#le%el or child windows for a!!lications. it creates a standard#style window. >ere are two of Frame's constructors+ () ;rameB C ,) ;rameB0trin titleC The first form creates a standard window that does not contain a title. The second form creates a window with the title s!ecified by title. Notice that you cannot s!ecify the dimensions of the window. Instead" you must set the si=e of the window after it has been created. T>* &A?A .I-RAR@ 0ettin the Window's Dimensions The setSize( ) method is used to set the dimensions of the window. Its si nature is %oid set0i=eBint newWidth" int newHeightC %oid set0i=eBDimension newSizeC The new si=e of the window is s!ecified by newWidth and newHeight" or by the width and hei ht fields of the !imension obAect !assed in newSize. The etSize( ) method is used to obtain the current si=e of a window. Its si nature is shown here+ Dimension et0i=eB C

This method returns the current si=e of the window contained within the width and hei ht fields of a !imension obAect. >idin and 0howin a Window After a frame window has been created" it will not be %isible until you call set"isi#le( ). Its si nature is shown here+ %oid set?isibleBboolean visibleFlagC The com!onent is %isible if the ar ument to this method is true. Otherwise" it is hidden. 0ettin a Window's Title @ou can chan e the title in a frame window usin set$itle( )" which has this eneral form+ %oid setTitleB0trin newTitleC >ere" newTitle is the new title for the window. Closin a ;rame Window When usin a frame window" your !ro ram must remo%e that window from the screen when it is closed" by callin set"isi#le(%alse). To interce!t a window#close e%ent" you must im!lement the windowClosin ( ) method of the WindowListener interface. Inside windowClosin ( )" you must remo%e the window from the screen. PROGRAM :- TO Create a child frame window from within an applet. import java.awt.*; import java.awt.event.*; import java.applet.*; // Create a subclass of Frame. class SampleFrame extends Frame { SampleFrame(String title) { super(title); // create an object to handle window events MyWindowAdapter adapter = new MyWindowAdapter(this); // register it to receive those events addWindowListener(adapter); } public void paint(Graphics g) { g.drawString("This is in frame window", 10, 40); } } class MyWindowAdapter extends WindowAdapter { SampleFrame sampleFrame; public MyWindowAdapter(SampleFrame sampleFrame) {

this.sampleFrame = sampleFrame; } public void windowClosing(WindowEvent we) { sampleFrame.setVisible(false); } } // Create frame window. public class AppletFrame extends Applet { Frame f; public void init() { f = new SampleFrame("A Frame Window"); f.setSize(250, 250); f.setVisible(true); } P.T.O public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); } }

Você também pode gostar