VIPS Unit 3 AWT COMPONENTS, Event, Anonymous Class, Layout Manager
VIPS Unit 3 AWT COMPONENTS, Event, Anonymous Class, Layout Manager
Nishtha Kansal
Assistant Professor
VIPS
OBJECTIVE OF AWT
•The objectives of this chapter are:
• To discuss the classes present in the java.awt package
• To understand the inheritance hierarchy of the AWT
• To outline the basic structure of GUIs
• To show how to add components to containers
• To understand how to use Layout Managers
• To understand basic graphics processing under the AWT
AWT (Abstract Windowing Toolkit)
• The window is the container that have no borders and menu bars.
• It should use frame, dialog or another window for creating a window.
• The Window 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.
Frame
• It is a subclass of Window and has a title bar, menu bar, borders, and
resizing corners.
• If you create a Frame object from within an applet, it will contain a
warning message, such as “Java Applet Window,” to the user that an
applet window has been created.
• The Frame is the container that contain title bar and can have menu
bars. It can have other components like button, textfield etc.
• When a Frame window is created by a stand-alone application rather
than an applet, a normal window is created.
Frame
Frame defines a top-level Window with Borders and a Menu Bar
Frames are more commonly used than Windows
Syntax
Frame aFrame = new Frame(“Hello World”);
aFrame.setSize(100,100);
aFrame.setLocation(10,10);
aFrame.setVisible(true);
Example of Frame
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not
visible
}
public static void main(String args[]){
First f=new First();
}}
Note The setBounds(int xaxis, int yaxis, int width, int
height) method is used in the above example that sets the
position of the awt button.
List AWT Component example
import java.applet.Applet;
import java.awt.List;
import java.awt.Color;
public class Demo extends Applet {
public void init() {
List l = new List(8, true);
l.add("Desktop");
l.add("Laptop");
l.add("Tablet");
l.add("Phone");
l.add("Kindle");
l.add("Screen");
l.add("System");
add(l);
setBackground(Color.blue); } }
Examples of GUI based Applications
• void fillRoundRect(int top, int left, int width, int height,int xDiam, int yDiam)
Example Drawing Rectangles
// Draw rectangles
import java.awt.*;
import java.applet.*;
/* <applet code="Rectangles" width=300 height=200> Sample output from this program is
shown here:
</applet>
*/
• To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). These methods are shown here:
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
/ Draw Ellipses import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses" width=300 height=200>
</applet>
*/
13
UNIT- Abstract Window Toolkit
ButtonTest.java
17
add(label1,"North");
add(label2,"Center");
add(label3,"South");
}
}
20
textArea.setEditable(false);
textField.setText("TextField");
textArea.setText("TextArea Line1 \n TextArea Line2");
add(textField,"North");
add(textArea,"South");
}
…
}
mb.add(m1);
MenuItem mi1_1 = new MenuItem("Menu Item 1_1"); m1.add(mi1_1);
m1.addSeparator();
MenuItem mi1_2 = new MenuItem("Menu Item 1_2"); m1.add(mi1_2);
RoundRectangle Rectangle
Polygon Arc
Abstract Window Toolkit UNIT-III 30
drawLine(x1,y1,x2,y2)
26
(x,y)
g.drawImage(im, x, y, w, h, observer);
• BorderLayout
GridLayout
– north, south, west, east & center – tabular form (rows &
• FlowLayout columns)
– left to right & top down GridBagLayout
• CardLayout – tabular form(variable
– stack of panels row heights and
column widths)
North
South
Output :