1 / 115

Topics

Topics. AWT Classes Window Fundamentals Working with Frame Windows Creating a Frame Window in an Applet Creating a Windowed Program Displaying Information within a Window Working with Graphics Working with color Setting the Paint Mode Working with Fonts

leena
Télécharger la présentation

Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topics AWT Classes Window Fundamentals Working with Frame Windows Creating a Frame Window in an Applet Creating a Windowed Program Displaying Information within a Window Working with Graphics Working with color Setting the Paint Mode Working with Fonts Managing Text Output Using Font Metrics.

  2. AWT Classes one of Java’s largest packages

  3. AWT Classes AWT  Abstract Window Toolkit The main purpose of the AWT is to support applet windows It can also be used to create stand-alone windows that run in a GUI environment, such as Windows. The AWT classes are contained in the java.awtpackage

  4. AWT Classes - some of the original methods were deprecated and replaced by new ones when Java 1.1 was released. For backward-compatibility, Java 2 still supports all the original 1.0 methods.

  5. AWT Classes

  6. AWT Classes

  7. AWT Classes

  8. AWT Classes

  9. Window Fundamentals • The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. • The two most common windows are • those derived from Panel, which is used by applets, • those derived from Frame, which creates a standard window.

  10. Window Fundamentals Much of the functionality of these windows is derived from their parent classes.

  11. -Component • An abstract class that • encapsulates all of the attributes of a visual component.(All user interface elements are subclasses) • It defines over a hundred public methods for managing events (such as mouse and keyboard input, positioning and sizing the window, and repainting.) • Remembers the current foreground and background colors and the currently selected text font.

  12. - Container • A subclass of Component • A container is responsible for laying out any components that it contains. • Includes methods that allow other Component objects to be nested within it • Other Container objects can be stored inside of a Container

  13. -Panel • A Panel is a window that does not contain a title bar, menu bar, or border. • A concrete subclass of Container • The superclass for Applet • It doesn’t add any new methods; it simply implements Container.

  14. -Panel • Other components can be added to a Panel object by its add( ) method. • add( ) method inherited from Container. • you can position and resize the added components manually using the setLocation( ), setSize( ), or setBounds( ) methods defined by Component.

  15. - Window • This class creates a top-level window • A top-level window is not contained within any other object; (it sits directly on the desktop) • You will use a subclass of Window called Frame to create Window objects

  16. -Frame • A subclass of Window and has a title bar, menu bar, borders, and resizing corners. • If you create a an applet window , it will contain a warning message, such as “Java Applet Window,” • When a Frame window is created by a program rather than an applet, a normal window is created.

  17. Working with Frame Windows • It creates a standard-style window • You will use it to create • child windows within applets, • top-level or child windows for applications. Two of Frame’s constructors: • Frame( )  creates a standard window that does not contain a title. • Frame(String title)  creates a window with the title specified by title

  18. Several methods are used when working with Frame windows. • Setting the Window’s Dimensions • void setSize(int newWidth, int newHeight) • void setSize(Dimension newSize) newWidth and newHeight  new size of the window newSize the Dimension object contains width and height fields The dimensions are specified in terms of pixels. • Dimension getSize( )  used to obtain the current size of a window

  19. Setting a Window’s Title void setTitle(String newTitle) newTitle  the new title for the window • Closing a Frame Window setVisible(false). • you must implement the windowClosing( ) method of the WindowListener interface • Inside windowClosing( ), you must remove the window from the screen

  20. Creating a Frame Window in an Applet STEPS: Create a subclass of Frame. Override any of the standard window methods (init( ), start( ), stop( ), and paint( )) Finally, implement the windowClosing( ) method of the WindowListener interface ( calling setVisible(false) when the window is closed ) you create an object of frame’s subclass. This causes a frame window to come into existence and you make window visible by calling setVisible( ).

  21. Note the following : • SampleFrame calls Frame’s constructor. (with super()). This causes a standard frame window to be created with the title passed in title. • This example overrides the applet window’s start( ) and stop( ) methods. This causes the window to be removed automatically when you terminate the applet, when you close the window,

  22. Sample output

  23. Handling Events in a Frame Window • Since Frame is a subclass of Component, it inherits all the capabilities defined by Component. • So you can use and manage a frame window that you create just like you manage your applet’s main window. • Whenever an event occurs in a window, the event handlers defined by that window will be called. • Each window handles its own events.

  24. Handling Events in a Frame Window • The MouseListener Interface • void mouseClicked(MouseEventme) • void mouseEntered(MouseEventme) • void mouseExited(MouseEventme) • void mousePressed(MouseEventme) • void mouseReleased(MouseEventme) • The MouseMotionListener Interface • void mouseDragged(MouseEventme) • void mouseMoved(MouseEventme)

  25. Handling Events in a Frame Window The WindowListener Interface • void windowActivated(WindowEvent we) • void windowClosed(WindowEvent we) • void windowClosing(WindowEvent we) • void windowDeactivated(WindowEvent we) • void windowDeiconified(WindowEvent we) • void windowIconified(WindowEvent we) • void windowOpened(WindowEvent we)

  26. The following program creates a window that responds to mouse events. The main applet window also responds to mouse events.

  27. Sample output

  28. It is possible to create stand-alone AWT-based applications, too. To do this, simply create an instance of the window or windows you need inside main( ).

  29. Sample output Once created, a frame window takes on a life of its own. Notice that main( ) endswith the call to appwin.setVisible(true).

  30. Working with Graphics

  31. Working with Graphics • All graphics are drawn relative to a window. (main window of an applet, a child window of an applet, or a stand-alone application window.) • The origin of each window is at the top-left corner and is 0,0. • Coordinates are specified in pixels • All output to a window takes place through a graphics context.

  32. Working with Graphics • A graphics context is encapsulated by the Graphics class and is obtained in two ways: • It is passed to an applet when one of its various methods, such as paint( ) or update( ), is called. • It is returned by the getGraphics( ) method of Component. • The Graphics class defines a number of drawing functions.

  33. Drawing Lines void drawLine(int startX, int startY, int endX, int endY) - displays a line in the current drawing color that begins at startX,startY and ends at endX,endY

More Related