Top Banner
JAVA Abstract Window Toolkit Rakesh Ravaso Patil Dept. of Computer Engg. T.E. ‘B’ 12276
25

Awt

May 15, 2015

Download

Education

Rakesh Patil

Abstract Window Toolkit
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Awt

JAVAAbstract Window Toolkit

Rakesh Ravaso PatilDept. of Computer Engg.T.E. ‘B’ 12276

Page 2: Awt

Overview…Introduction to AWT

Swing vs. AWTBuilding GUIComponent HierarchyAWT Control FundamentalsAdding ComponentsArranging ComponentsOverview of Event HandlingWorking with GraphicsConclusion

Page 3: Awt

AWT (Abstract Window Toolkit)Present in all Java implementations

Adequate for many applications

Uses the controls defined by your OS

Difficult to build an attractive GUI

import java.awt.*;import java.awt.event.*;

Page 4: Awt

SwingSame concepts as AWTDoesn’t work in ancient Java implementations

(Java 1.1 and earlier)Many more controls, and they are more

flexibleSome controls, but not all, are a lot more

complicatedGives a choice of “look and feel” packagesMuch easier to build an attractive GUIimport javax.swing.*;

Page 5: Awt

Swing vs. AWTSwing is bigger, slower, and more complicated

But not as slow as it used to beSwing is more flexible and better lookingSwing and AWT are incompatible--you can use

either, but you can’t mix themActually, you can, but it’s tricky and not worth doing

Learning the AWT is a good start on learning SwingMany of the most common controls are just renamed

AWT: Button b = new Button ("OK");Swing: JButton b = new JButton("OK");

Page 6: Awt

To build a GUI...

Create some Components, such as buttons, text areas, panels, etc.

Add your Components to your display areaArrange, or lay out, your Components Attach Listeners to your Components

Interacting with a Component causes an Event to occur

A Listener gets a message when an interesting event occurs, and executes some code to deal with it

Page 7: Awt

An Applet is Panel is Containerjava.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet

…so you can display things in an Applet

Page 8: Awt

Example: A "Life" appletContainer (Applet)

Containers (Panels)

Component (Canvas)

Components (Buttons)

Components (Labels)

Components (TextFields)

Page 9: Awt

Some types of components

Label Button

Button

Checkbox

Choice

List

Scrollbar

TextField TextArea

CheckboxGroupCheckbox

Page 10: Awt

Creating componentsLabel lab = new Label ("Hi, Dave!");Button but = new Button ("Click me!");Checkbox toggle = new Checkbox

("toggle");TextField txt =

new TextField ("Initial text.", 20);Scrollbar scrolly = new Scrollbar

(Scrollbar.HORIZONTAL, initialValue, bubbleSize, minValue, maxValue);

Page 11: Awt

Adding components to the Applet class MyApplet extends Applet {

public void init () { add (lab); // same as this.add(lab) add (but); add (toggle); add (txt); add (scrolly); ...

Page 12: Awt

Arranging componentsEvery Container has a layout managerThe default layout for a Panel is FlowLayoutAn Applet is a PanelTherefore, the default layout for a Applet is

FlowLayoutYou could set it explicitly with

setLayout (new FlowLayout( ));You could change it to some other layout

manager

Page 13: Awt

Flow LayoutUse add(component); to add

to a component when using a FlowLayout

Components are added left-to-right

If no room, a new row is startedExact layout depends on size of

AppletComponents are made as small

as possibleFlowLayout is convenient but

often ugly

Page 14: Awt

BorderLayoutAt most five

components can be added

If you want more components, add a Panel, then add components to it.•setLayout (new

BorderLayout());

Page 15: Awt

GridLayoutThe GridLayout

manager divides the container up into a given number of rows and columns:

new GridLayout(rows, columns)

•All sections of the grid are equally sized and as large as possible

Page 16: Awt

Making components activeMost components already appear to do

something--buttons click, text appearsTo associate an action with a component,

attach a listener to itComponents send events, listeners listen for

eventsDifferent components may send different

events, and require different listeners

Page 17: Awt

ListenersListeners are interfaces, not classes

class MyButtonListener implements ActionListener {

An interface is a group of methods that must be supplied

When you say implements, you are promising to supply those methods

Page 18: Awt

Writing a ListenerFor a Button, you need an ActionListener

b1.addActionListener (new MyButtonListener ( ));

An ActionListener must have an actionPerformed(ActionEvent) method

public void actionPerformed(ActionEvent e) { …}

Page 19: Awt

Listeners for TextFieldsAn ActionListener listens for someone hitting

the Enter keyAn ActionListener requires this method:

public void actionPerformed (ActionEvent e)

You can use getText( ) to get the text

A TextListener listens for any and all keysA TextListener requires this method:

public void textValueChanged(TextEvent e)

Page 20: Awt

Circle

RoundedRectangle

Triangle

Working With Graphics

Rectangle

Ellipse

Lines

Arc

Page 21: Awt

Working with Graphicsvoid drawLine(int x1, int y1, int x2, int y2)void drawRect(int top, int left, int width, int height)void drawRoundRect(int top, int left,int width, int

height, int xDiam, int yDiam)void drawOvel(int top, int left, int width, int height)void drawArc(int top, int left, int width,int height, int

startAngle, int sweepAngle)void drawPoly(int x[], int y[], int numPoints)

void fill….

Page 22: Awt

Format ShapeWorking with colors

HSBRGBstatic int HSBtoRGB(float hue, float saturation,

float brightness)Static float[] RGBtoHSB(int red, int green, int

blue, float value[])

Working with FontsFont(String fontName, int fontStyle, int

pointSize)

Page 23: Awt

Summary : Building a GUICreate a container, such as Frame or AppletChoose a layout managerCreate more complex layouts by adding

Panels; each Panel can have its own layout manager

Create other components and add them to whichever Panels you like

For each active component, look up what kind of Listeners it can have

Create (implement) the Listeners often there is one Listener for each active

component

Page 24: Awt

Summary: Building a GUIFor each Listener you implement, supply the

methods that it requiresFor Applets, write the necessary HTMLGraphics

Drawing LinesDrawing RectanglesDrawing Ellipse and CirclesDrawing Polygons

Color schemes and Fonts

Page 25: Awt