1 / 14

Abstract Window Toolkit (AWT)

Abstract Window Toolkit (AWT). A class library for basic GUI programming It is supposed to be platform independent “lowest common denominator” approach is used The main package, java.awt , is roughly divided into three categories Components: GUI components such as frames, buttons, menus, ...

tabib
Télécharger la présentation

Abstract Window Toolkit (AWT)

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. Abstract Window Toolkit (AWT) • A class library for basic GUI programming • It is supposed to be platform independent • “lowest common denominator” approach is used • The main package, java.awt, is roughly divided into three categories • Components: GUI components such as frames, buttons, menus, ... • Graphics: colors, fonts, images, polygons, ... • Layout Managers: controls the layout of components within their container objects • Other related packages: • java.awt.datatransfer (for cut-and-paste) • java.awt.event (for event handling) • java.awt.image (for image manipulation)

  2. java.awt package Object Event Graphics FlowLayout Component Color BorderLayout Label Font Button CardLayout Dimension TextComponent GridLayout Insets TextArea GridBagLayout TextField Polygon Container Point Rectangle MenuComponent Panel MenuBar Window FontMetrics MenuItem Frame Image Dialog Class Abstract Class FileDialog

  3. Major Classes in Abstract Window Toolkit fields: id, key, modifiers, target,when, x, y, ... constants:KEY_PRESS, MOUSE_DOWN, WINDOW_DESTROY, ... Event Object Objects that can response to events. Java built-in event loop passes Event objects to Component objects. Each component object has a handleEvent() method. Component Container The abstract superclass of all components that can hold other components. Each container may be associated with a LayoutManager instance. Methods: add(), insets(), layout(), ... A top-level window; it has no borders and no menu bar. It could be used, for example, to implement a pop-up menu. Methods: dispose(), show(), pack(), toBack(), toFront(), ... Window Graphic Methods: drawLine,drawRect,drawString, ... fillRec,fillOval, ... getColor, getFont, ... setColor, setFont, ... A top-level window with a title and a border. Can also have a menu bar. AWT sends it all mouse, keyboard, and focus events that occur over it. Frame

  4. Class Frame • A Frame object is a top-level window with a title and a border. It can also have a menu bar. • AWT sends to a frame all mouse, keyboard, and focus events that occur over it. • Two types of Constructors • Frame(); Frame(String s); • All the fields in this class are static final int. • Methods • getX(), setX() where X can be CursorType, MenuBar or Title. • You need to know its inheritance hierarchy and certain properties of its parent class to use it well. • Window is invisible when you create it. Use show(). • Window ignore the destroy() event. Override handleEvent().

  5. Class Window • A Window object is the top-level window; it has no borders and no menu bar. • It could be used, for example, to implement a pop-up menu. • This class extends the Container class. • Important Fields • Important Methods • show(): overrides the show() method of the Component class. • pack(): uses the LayoutManager to arrange the components it contains. • dispose(), toBack(), toFront(), ...

  6. Class Container • The abstract superclass of all components that can hold other components • Each container is associated with a LayoutManager instance • It lets you • add and remove individual components to the container • include LayoutManager that will position the components when they are added to the Container object • Important Methods • add(): • insets(): returns an Insets object. • getLayout() and setLayout(): let you check and change the layoutManager associcated with your container. • A Panel object is the simplest Container. Class Applet extends class Panel. The default layout manager for a panel is the FlowLayout layout manager.

  7. Class Component • Objects that can response to events • Java built-in event loop passes Event objects to Component objects • Each component object has a handleEvent() method

  8. Menu-Related Classes • MenuBar Class: • has no fields. add() method to attach • Frame’s setMenuBar() method to attach Menu object to a framne • MenuItem Class: • has no fields, can trigger an action • new MenuItem(“&Save”); create a menuItem and underline S. • Menu Class: • extends MenuItem. • is a drop-down menu attached to the MenuBar, or a popup menu triggered from another menu. • CheckboxMenuItem. • MenuComponent. an abstract class that is extended by all other menu classes.

  9. Graphics Class • Defines methods for doing line and text drawing and image painting. • Relies on other inner classes such as Color, Font, Image, Polygon. • Methods: • drawLine,drawRect,drawString, ... • fillRec,fillOval, ... • getColor, getFont, ... • setColor, setFont, ...

  10. Adding Texts to a Frame • Label class • Display fix text that the user cannot edit, but your program can change it. • Has three constant fields: LEFT, CENTER, and RIGHT. • getText() and setText(); • Example: • Label l = new Label(“Name: “, Label.RIGHT); • add(l); • TextField • the basic data entry tool used for names, numbers, etc. • No fields, but has good constructors: TextField(); TextField(String s); TextField(int n); TextField(String s; int n); • TextAreas • the multi-line version of the TextField class to display, enter or edit blocks of text.

  11. Rendering Texts and Graphics to a Frame void drawString(string s, int x, int y) void drawLine(int x1, int y1, int x2, int y2) void drawRect(int x, inty, int w, int h) drawOval(int x, int y, int w, int h) void drawPolygon(Polygon p) void fillPolygon(Polygon p) void setColor(Color c) void setFont(Font font) selects a font for graphics context. Graphics Component void setBackground (Color c) set background color. create a polygon object add points to the object draw the polygon Polygon Font Font(String name, int style, int size) create a Font object. Example: Font fi = new Font(“Helvetica”, Font.BOLD+Font.ITALIC, 14); Color(int r, int g, int b) creates a color object. Values of r, g, b are 0-255. Constants: black, blue, green, red, white, ... Color

  12. Rendering Text to a Frame • Need to override the paint method from the Component class. • All drawing in Java must go through a Graphics object. • drawString(String s, int xCoord, int yCoord) paints a string s at location (xCoord, yCoord). • Font(String name, int style, int size) create a new Font object. • void setFont(Font font) selects a font for the graphic context.

  13. FontMetrics Concepts ascender Jacques ascent baseline descent descender leading Huang height baseline Methods in FontMetrics: int getAscent() int getDescent() int getLeading() int getHeight() int stringWidth(String str) Methods in Graphics: void setFont(Font font) FontMetrics getFontMetrics() void drawString(String str, int x, int y)

  14. Class Button • This class creates a labeled button. The application can cause some action to happen when the button is pushed. • Constructors • public Button(); • public Button(String label); • Methods • public void addNotify(); • public String getLabel(); • protected String paramString(); • public void setLabel(String label); • When a button is pushed and released, AWT sends an action event to the button. This event's target is the button, and its object is the string label of the button. • An application should override the action method of the button or of one of its containing windows in order to cause some action to occur.

More Related