Java Notes 6
Java Notes 6
CONTENTS
• Introduction
• Designing GUI with Components and Layout Managers
o Using Components for Java GUIs
o Using Layout Managers for Java GUI
o GridBagLayoutManager
• Event Handling
• Drawing methods for Graphics objects
• Practice Questions
• Assignment
• Q&A
Introduction
Java is a very powerful programming language to develop varieties of applications and makes
the application development easy. Java provides a package of classes called the Abstract
Windowing Toolkit (or also called the Advanced Window Toolkit ), which we will simply refer to as
the AWT. It is an Object Oriented Graphical User Interface (GUI) frame work that allows the
application developer to design modern, mouse controlled, graphical application interfaces. A
basic overview of this package is shown in the Figure 7.1. This package is so vast that it is not
possible to discuss in this Chapter about each class and to illustrate their uses. However, we will
pick up only few (those are marked in bold type) of them as a token and will be illustrated their
application.
Figure 7.1 : Abstract Windowing Toolkit Package Hierarchy
The main idea of using these classes is that to create the object of their type. Each class have
their own constructors for initialization of the objects. When an object is created and initialized, it
then can be placed on an applet by the add() method; this method is defined in their
corresponding classes.
Following are 11 samples for illustrations of using these component. Readers are advised to test
the illustrations.
Note : Button class contains one default constructor where no argument has to be passed and a
button will appear without any string label on it. One can change the label of a button at any time
using the public void setLabel(String label) method defined in Button class. There is another
function also defined as public String getLabel( ) to return the label of a button object.
Note : This class has 3 constructors; one constructor with three arguments first for label, second
for fencing type(default value is null) and third for initial state : false for deselected and true for
selected.
import java.awt.*;
import java.applet.*;
public class ChoiceDemo extends Applet {
public void init ( ) {
int width = Integer.parseInt (getParameter( " width" ));
int height = Integer.parseInt (getParameter(" height"));
// To get the width and height o
f the applet.
Choice os = new Choice ( ); // os is an instance o
f choice class.
Choice browser = new Choice ( ); // browser is the anot
her instance.
os.addItem ( " Windows 95" );
os.addItem ( " Solaris 2.x " );
os.addItem ( " Mac OS 7.5 ");
browser.addItem (" Netscape Navigator 2.0");
browser.addItem ( "Internet Explorer 4.0" );
browser.addItem ("Hot Java ") ;
add (os);
add (browser );
os.reshape ( 0, 0, width /2, height /2 );
browser.reshape (0, height /2, width, height);
}
}
The Choice class is used for creating a pop-up selection menu. A Choice component is used to
display menu item, each item in the menu is a String which appears as a left justified label in the
order they are added to the Choice object. The Choice class uses addItem() to add new Strings
to a Choice object. In the above example, two choice menus and their items are displayed. This
time we have used the reshape() method to customarily place the choices on the applet view. It
first two arguments are the left-top and remaining two argument are right-bottom coordinates.
In the Component class, there is Frame, a sub class of Window. This Frame class can be used
to create a user defined frame. A frame is a window with a title and resize corners. The program
given below creates a simple frame with a specific title, size and back ground color :
The program runs continuously. To quit press CTRL+C. It can be noted that, the show( ) and
resize( ) methods must be called to make the frame visible.
Panels like frames, provide the space for you to attach any GUI component, including other
panels. Panels are created using the constructor Panel(). Once a Panel object is created, it must
be added to a Window or Frame object in order to be visible. This is done using the add() method
of the container class. The following program creates a small yellow panel, and adds it to a
Frame object :
With the class Label, one can construct a label object with the string labeled that has to be
displayed. An alternative constructor is provided to allow left, right, or center justification of the
label text. Three constants LEFT, RIGHT, and CENTER are available for corresponding
alignment. Here, is an example, that creates three labels, each with a different alignment.
import.java.awt.*;
import.java.applet.*;
public class LabelDemo extends Applet {
public void init ( ) {
setLayout (null );
int width = Integer.parseInt ( getParameter ("width" ));
int height = Integer.parseInt (get.Parameter (" height" )
);
Label left = new Label ( ); // defa
ult alighment is left )
Label center = new label ( " Center", Label.CENTER);
// For Centered alignment
Label right = new Label("Right", Label.RIGHT); // for Rig
ht justified label
The List class provides a compact, multiple-choice, scrolling selection list. It can be noted that
the Choice object shows only the single selected item in the menu, whereas, a List object can be
constructed to show any number of choices in the visible window. Following is an applet to
display two selection lists, one multiple choice and other single choice.
import java.awt.*;
import java.applet.*;
public class ListDemo extends Applet {
public void init ( ) {
setLayout (null ); // It is a default Layo
ut setting
int width = Integer.parseInt (getParameter (" width" ) );
int height = Integer.parseInt (getParameter (" height" ) );
It can be noted that, the List class has two constructors namely :
The first constructor is to create default list with no items and no selection, (when the list is
created). Other constructor, comes in two varieties : one allows multiple selection with
multiSelectionChoice as true; another with false behave exactly like a Choice such that it only
accepts single (mutually exclusive ) selection.
Scroll bars are used to select continuous values between a given minimum and maximum and
one can slide over this range. The scroll bar is either in vertical or horizontal orientation.
Following is an example that creates both a vertical and an horizontal scroll bar :
import java.awt.*;
import java.applet.*;
public class ScrollDemo extends Applet {
public void init ( ) {
setLayout( new Borderlayout ( ) ); // defa
ult border layout setting
int width = Integer.parseInt (getParameter (" width" ));
int height = Integer.parseInt (getParameter (" height"));
Scrollbar hScroll = new Scrollbar ( Scrollbar. HORIZONTAL
, 50, width /10, 0, 100 );
Scrollbar vScroll = new Scrollbar ( Scrollbar.VERTICAL, 5
0, hight /2, 0, 100);
add (hScroll );
add (vScroll );
}
}
Note the numeric argument that have been passed to the Scrollbar constructor : First argument
is to specify the kind of the scroll bar. Second argument dictate the starting position of the slider,
third argument is to decide the proportion of the range that scroll bar will appear ie. 1/10th of the
width of the applet for horizontal or � of the height of the applet view for vertical scroll bar. The
last two arguments specify the minimum and maximum range ( for example, 1-12 for month , 1-
31 for dates, 1 to 256 for colours etc.), we have taken 0 - 100 for something in both the scroll bar.
In the scroll bar class, therre are two methods vide :
import java.awt.*;
import java.applet.* ;
public class TextFieldDemo extends Applet {
public void init ( ) {
add ( new TextField("Type your Name", 20);
// create a text field window of 20 character widths
Label login = new Label ( "Login : " , Label.LEFT );
Label pswd = new Label ( "Password : " , Label.CENTER );
In the above example, note the use of setEchoCharacter() method which is used to mask the
actual character on the screen.
The TextArea class, as its name suggests, is used when larger amount of text need to be input /
displayed. Let us see the following illustration :
import java.awt.*;
import java.applet.*;
public class TextAreaDemo extends Applet {
TextArea text;
String multiLineText =
" To learn Java, you will first"
+ " need to obtain \ n two different pieces of "
+ "softwares : \ n "
+ " The first is the JDK (Java Development Kit"
+ "and \n the second piece of software "
+ "is a Java capable browser."
+ " \n JDK software is needed for Writing, "
+ " Compiling, and Testing Java applet \n and
+ " Applications.\n \n" ;
public void init ( ) {
setLayout(null);
text = new TextArea (multiLineText, 20, 40);
add (text);
}
}
Here, the constructor TextArea (String text, int rows, int columns) where rows and columns are to
describe the rows and columns in the text area, to be displayed.
import java.awt.*;
import java.applet.*;
import java.util.*;
public class FlowLayoutDemo extends Applet {
public void init ( ) {
setLayout (new FlowLayout (FlowLayout.RIGHT, 10, 3));
String val = " Data is not Information" +
" is not knowledge is not wisdom";
StringTokenizer str = new StringTokenizer (val );
// Read a token from the String val
while (str. hasMoreTokens ( ) ) {
add (new Button (str.nextToken( ) ) ); // For o
ne token add a label into the panel
}
}
}
In the above example, the FlowLayout manager is invoked using the setLayout() method
inherited from Component class. There are three forms of this method :
alignment is LEFT, RIGHT or CENTER, hskip and vskip are the spaces between two
components horizontally and vertically respectively.
The BorderLayout Manager implement a common layout style which has five zones - each of
these zones is named with a string North, South, East, and West represent the four sides, and
Center is the middle area.
Here is an example of BorderLayout with component in each layout area in a frame.
import java.awt.*;
import java.applet.*;
import java.util.*;
public class BorderLayoutDemo extends Applet {
public void intit ( ) {
Frame frame = new Frame ( );
frame.setLayout ( new BorderLayout (20,40));
Button buttonN, buttonS, buttonE, buttonW, buttonC;
import java.awt.*;
import java.applet.Applet;
public class GridLayoutDemo extends Applet {
Button b1,b2,b3,b4,b5,b6 ;
public void init ( ) {
setLayout (new GridLayout (3,2) ) ; // 3 ro
ws, 2 columns
b1 = new Button ("B1");
b2 = new Button ("B2");
b3 = new Button ("B3" );
b4 = new Button ("B4" );
b5 = new Button ("B5" );
b6= new Button ("B6" );
add (b1); add (b2); add (b3);
add (b4); add (b5); add (b6);
}
}
GridBagLayoutManager
GridBagLayoutManager is the more flexible analog of GridLayoutManager. With this manager
you can specify width, height etc. of individual grids in a container. It is not possible to illustrate
this manager with a small example.
The Card Layout Manager is unique from the other layout managers in the sense that it
represents several different layouts that can be thought of as living on separate index cards that
can be shuffled so that any one card is on top at a given time. This can be useful for user
interfaces that have optional components which can be dynamically enabled and disabled upon
user input. The following program illustrates the use of CardLayout with five cards containing a
single button each.
import java.awt.*;
public class Crads extends java.applet.aApplet {
CardLayout layout;
public void init ( ) {
layout = new CardLayout ( ) ;
setLayout (layout );
add ("1", new Button ("Card1" ));
add ("2", new Button ("Card2" ));
add ("3", new Button ("Card3" ));
add ("4", new Button ("Card4" ));
add ("5", new Button ("Card5" ));
}
public boolean key Down (Even e int key ){
layout next (this);
return (true );
}
}
Note : The add() method used here to add cards to a CardLayout, and you have to use the
labeled variety to pass a card name; in the above example 1,2 ��etc. are the name for five
cards. Later, one can refer to cards by this name. Also note that a CardLayout variable (i.e.
layout) was created. This is because, we actually need to refer to our CardLayout object after we
have created it.
To demonstrate the ability to cycle through the cards, this applet was made to listen to the key
board and select the next card whenever you press a key. This is a mechanism of event handling
which is our next topic of discussion
Event Handling
All of the components we have created so far have looked right, but they have been essentially
useless since we never talked about how to process the user input to these user interface
controls. Suppose, there are a number of buttons in a container and each button is
corresponding to a particular event. Thus, if a user selects a button then the system should
respond accordingly and this type of GUI programming (this is why it is also known as Event
Driven programming ). In this very section we will explore, how Java provides the programmers
event handling capabilities.
In the context of Java events, an event is an asynchronous data generated by the window server
and results from some device activity. The devices are usually mouse and keyboard. Examples
of event include:
Mouse events :
MOUSE_ENTER, MOUSE_EXIT, MOUSE_MOVE, MOUSE_DRAG, MOUSE_UP, MOUSE_DOWN .These a
re generated by mouse actions within the borders of Canvas, Frame, and Panel comp
onents.
Focus events :
GOT_FOCUS, LOST_FOCUS Generated as the mouse enters or exits the borders
of a Canvas, Frame, or Panel Component. They are also generated as you click a mou
se in a Text Component.
Keyboard events :
KEY_PRESS, KEY_ACTION, KEY_RELEASE, KEY_ACTION_RELEASE Generated by press
ing or releasing keys on the key board when you are in a Frame, Canvas, Panel or T
ext Component.
Action event :
ACTION_EVENT Generated when you click on a Button, Choice, or CheckBox Compone
nt, Press Enter in a TextField, or double click on a List item.
Scroll bar events :
SCROLL_LINE_UP, SCROLL_LINE_DOWN, SCROLL_PAGE_UP, SCROLL_PAGE_DOWN, SCROLL_ABS
OLUTE .Generated by actions involving the scroll bar in Scrollbar Component.
The id variable in the Event class will be initialized by the corresponding event type during the
automatic instantiation of respective Event object.
Now, there are a number of event handling methods declared in Component class and Event
class. The method handleEvent(Event evt) in Component class is the central event dispatcher for
a component. This method for a given event type (evt.id ) calls the corresponding method. These
methods are defined in Component class and summarized in Section 8.4 of this Chapter:
However, all these method can be called exclusively in applets/Application with the overridden
code.
It can be noted that, there are few events which are generated by List and Scrolllbar components
that do not have their helper functions. To handle these, it is required to override the
handleEvent() method.
As an illustration let us consider the following example (Illustration 7.3) which creates 15-cells
GridLayout and then the use of even handler namely action().
import java.awt.*;
import java.applet.*;
public class EventDemo extends Applet {
static final int n = 4;
Label label;
public void init( ) { // To initialize the ap
plet for 16-cells grid
setLayout (new GridLayout (n,n); // To for 4x4 c
ells grid
setFont (new Font ("Helvetica", Font.BOLD, 24 )); // F
ont of Helvetica of size 24
for (int i =0; i < n; i++ ) {
for (int j = 0; j < n; j++ ) {
int k = i * n + j;
if (k > 0)
add (new Button (" " + k); // Pla
ce a button at (i,j)-th cell
}
}
label = new Label (" * " , Label.CENTER );
label.setFont (new FONT (" Times Roman", Font.ITALIC, 24
));
add (label );
}
Whenever the applet EventDemo runs the event handler, namely, action(Event e, Object obj) will
come into play till the user selects a button in the GridLayout, and it then returns false.
Note : Each event handling method returns boolean and they are in general public.
import java.awt.*;
import java.applet.*;
public class GraphicsText extends Applet {
public void init ( ) {
public void paint (Graphics g) {
Dimension d = size ( ) ; // retu
rns the width and height of the applet
g.setColor (Color.black ); // set
the drawing color as black
g.drawRect (0, 0, d.width-1, d.height -1);
g.setColor (Color.blue);
g drawRect (10, 10, d.width-10, d.height-10 );
// draw another inner blue Rectangle
g.setColor (Color.red );
g.drawOval (15,15, 50,50 );
g.setColor( Color.yellow );
g.fillOval ( 20, 20,40, 40 );
g.setColor (Color.red );
g.drawArc (230, 10, 50, 50, -90, 270 );
g.fillArc (190, 10, 50, 50, -90, 270 );
g.setColor (Color.pink );
g.drawRoundRect (130, 70, 50, 50, 10, 10 );
g.fillRoundRect (190, 70, 50, 50, 10, 10 );
int [ ] xPoints = { 10,70,120,120,70,10,1
0 };
int [ ] yPoints = { 130, 150, 130, 180, 1
70, 180, 130 };
Color myColor = new Color (255,100,150); // s
et a RGB color
// A color can be choos
ed, R, G, B integers ranges between 0 to 255
g.setColor (myColor );
g.drawPolygon (xPoints , yPoints, 7); // To d
raw a polygon with 7 points
g.setColor (Color.gray );
g.draw3DRect (10, 190, 50, 50, true );
g fill3DRect (70, 199, 50, 50, true);
g.setColor (Color.green );
g.draw3DRect (130, 190, 50, 50, false );
g.fill3DRect (190, 190, 50, 50, false );
}
}
}
Graphics objects are also capable of displaying images; there is a number of methods in
Component and Applet class dealing with image. Two methods are mentioned here :
getImage (URL url, String s ); Returns the image file img (say) having names and l
ocated at url. For stand alone machine, url is getDocumentBase( ).
drawImage (Image img; int x, int y, ImageObserver observer ); This method requ
ires an Image object and an ImageObserver observer.
import java.awt.*;
import java.applet.Applet;
public class ImageDemo extends Applet {
Image myImage;
public void init ( ) {
myImage = getImage (getDocumentBase ( ), "graphi
cs/title.gif " );
}
public void paint (Graphics g) {
g.drawString (myImage, 25, 25, this);
}
}
Here, we assume that an image file tille.gif is located in a directory graphics in the applet running
machine. The getDocumentBase( ) method returns the URL url of the current machine.
The Graphics class is also very powerful handle string/font in several ways. But discussion of this
is beyond the scope of this book.
Practice Questions
Practice 7.1
This example shows how an applet code retrieve the value of parameters from .html
file.
AppletParameter.java
import java.applet.*;
import java.awt.*;
AppletParameter.htm
< HTML>
< HEAD>
< TITLE>Passing Parameter in Java Applet
< BODY>
This is the applet:< P>
< APPLET code="AppletParameter.class" width="800" height="100">
< PARAM name="message" value="Welcome in Passing parameter in java applet
example.">
< / APPLET>
Practice 7.2
This example shows how to draw the different types of shapes like line, circle and
rectangle.
import java.applet.*;
import java.awt.*;
< HTML>
< HEAD>
< BODY>
< div align="center">
< APPLET CODE="CircleLine.class" WIDTH="800" HEIGHT="500">
< / div>
< / BODY>
< / HTML>
Practice 7.3
This program illustrates how to draw the different types of colorful shapes like l
ine, circle and the rectangle.
import java.applet.*;
import java.awt.*;
g.drawOval(x-r,y-r,100,100);
g.setColor(Color.yellow); //Fill the yellow color in circle
g.fillOval( x-r,y-r, 100, 100 );
g.setColor(Color.magenta);
g.drawString("Circle",275,100);
g.drawRect(400,50,200,100);
g.setColor(Color.yellow); //Fill the yellow color in rectangel
< HTML>
< HEAD>
< / HEAD>
< BODY>
< div align="center">
< APPLET ALIGN = "CENTER" CODE = "ShapColor.class" WIDTH = "800" HEIGHT =
"500" >
< / div>
< / BODY>
< / HTML>
Practice 7.4
This program illustrates how to create choice in applet
import java.awt.*;
import java.applet.*;
public class ChoiceDemo extends Applet {
public void init ( ) {
int width = Integer.parseInt (getParameter("width" ));
int height = Integer.parseInt (getParameter("height"));
< BODY>
< div align="center">
< APPLET ALIGN = "CENTER" CODE = "ChoiceDemo.class" WIDTH = "800" HEIGHT =
"500">
< PARAM name="width" value="400">
< PARAM name="height" value="500">
< / APPLET>
< / div>
< / BODY>
< / HTML>
Practice 7.5
This program illustrates how to create label
import java.awt.*;
import java.applet.*;
add(left);
add(right );
add(center );
left.reshape ( 0, 0, width, height/3);
right.reshape(0, height/3, width, height/3);
center.reshape (0, 2 * height/3 , width, height/3);
}
}
< HTML>
< HEAD>
< TITLE>Passing Parameter in Java Applet< / TITLE>
< / HEAD>
< BODY>
This is the applet:< P>
< APPLET code="LabelDemo.class" width="800" height="100">
< PARAM name="width" value="200">
< PARAM name="height" value="300">
< / APPLET>
< / BODY>
< / HTML>
Practice 7.6
import java.awt.*;
import java.applet.* ;
public class TextFieldDemo extends Applet {
public void init ( ) {
add(new TextField("Type your Name", 20));
< HTML>
< HEAD>
< TITLE>Passing Parameter in Java Applet< / TITLE>
< / HEAD>
< BODY>
This is the applet:< P>
< APPLET code="TextFieldDemo.class" width="800" height="100">
< / APPLET>
< / BODY>
< / HTML>
Practice 7.7
This program illustrates Flow layout manager
import java.awt.*;
import java.applet.*;
import java.util.*;
< HTML>
< HEAD>
< TITLE>Passing Parameter in Java Applet< / TITLE>
< / HEAD>
< BODY>
This is the applet:< P>
< APPLET code="FlowLayoutDemo.class" width="800" height="100">
< / APPLET>
< / BODY>
< / HTML>
Practice 7.8
This example shows an Applet program which performs Addition, Subtraction, Multipl
ication and Division operations. To do these operations, this applet implements Ac
tionListener interface and use four buttons for four operations.
import java.applet.*;
import java.awt.event.*; import java.awt.*;
if(i >j){
Sub = i - j;
}
else{
Sub = j - i;
}
if(source.getLabel() == "Subtract"){
txtArea.append("Sub : " + Sub + "\n");
}
Mul = i*j;
if(source.getLabel() == "Multiply"){
txtArea.append("Mul = " + Mul + "\n");
}
if(source.getLabel() == "Divide"){
txtArea.append("Divide = " + Div);
}
}
}
HTML code of the program:
< HTML>
< BODY>
< APPLET CODE ="EventListeners" WIDTH="800" HEIGHT="500">< / APPLET>
< / BODY>
< / HTML>
Practice 7.9
This example shows an interactive applet for summing two numbers.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
}
}
< HTML>
< HEAD>
< TITLE>Passing Parameter in Java Applet< / TITLE>
< / HEAD>
< BODY>
This is the applet:< P>
< APPLET code="InteractiveApplet.class" width="800" height="100">
< / APPLET>
< / BODY>
< / HTML>
Practice 7.10
This program illustrates displaying an image in an applet
import java.applet.*;
import java.awt.*;
Note: This example assumes that sample.JPG exists in the source folder
Practice 7.11
This program shows how to display the banner in applet.
import java.awt.*;
import java.applet.*;
< HTML>
< BODY>
< APPLET CODE = "SampleBanner" WIDTH = "500" HEIGHT = "300">< / APPLET>
< / BODY>
< / HTML>
Practice 7.12
This example displays the time in an applet in the time format like: hours,
minutes and then seconds (hh:mm:ss).
Practice 7.13
This applet shows a rectangle that will change color when the mouse moves over it.
Java code of the program:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
int rect1x,rect1y,rect1width,rect1height;
boolean rect1Active;
< HTML>
< HEAD>
< / HEAD>
< BODY>
< div align="center">
< APPLET ALIGN = "CENTER" CODE = "MouseMotionExample.class" WIDTH = "800"
HEIGHT = "500">
< / APPLET>
< / div>
< / BODY>
< / HTML>
Practice 7.14
This applet draws an arc with mouse.
if ( listOfPositions.size() >= 50 ) {
// delete the first element in the list
listOfPositions.removeElementAt( 0 );
}
// add the new position to the end of the list
listOfPositions.addElement( new Point( e.getX(), e.getY() ) );
repaint();
e.consume();
}
public void mouseDragged( MouseEvent e ) { }
< HTML>
< HEAD>
< / HEAD>
< BODY>
< div align="center">
< APPLET ALIGN = "CENTER" CODE = "MouseArc.class" WIDTH = "800" HEIGHT = "500">
< / APPLET>
< / div>
< / BODY>
< / HTML>
Practice 7.15
This applet generates color spectrum.
import java.applet.*;
import java.awt.*;
public class ColorSpectrum extends Applet {
// As i goes from 1 to N, this color goes from almost pure green to pure
red.
spectrum2[ i-1 ] = new Color( i/(float)N, (N-i)/(float)N, 0 );
}
}
< HTML>
< HEAD>
< / HEAD>
< BODY>
< div align="center">
< APPLET ALIGN = "CENTER" CODE = "ColorSpectrum.class" WIDTH = "800" HEIGH
T = "500">
< / APPLET>
< / div>
< / BODY>
< / HTML>
Practice 7.16
This applet reduces flickering while painting with mouse dragging. Here, backbuffe
r is used to store results of drawing operations.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
addMouseMotionListener( this );
}
public void mouseMoved( MouseEvent e ) { }
public void mouseDragged( MouseEvent e ) {
int x = e.getX();
int y = e.getY();
backg.fillOval(x-5,y-5,10,10);
repaint();
e.consume();
}
< HTML>
< HEAD>
< / HEAD>
< BODY>
< div align="center">
< APPLET ALIGN = "CENTER" CODE = "BackBufferPaint.class" WIDTH = "800" HEI
GHT = "500">
< / APPLET>
< / div>
< / BODY>
< / HTML>
Assignment
Q: Write an applet which takes input from user and performs basic
mathematical operations on the inputs and shows the result.
Q: Write an applet which takes the input - a text and font color from the user
and displays the text in the selected color.
Q: Write an applet which prompts for dimension of circle/square/rectangle
and displays the figure with proper dimension. After some time, figure
will vanish from the screen and again ask to enter dimension of the
figure.
Q&A
Q: What are AWT peers?
A: A component is associated with a standard AWT button object, a peer object
and an interfacing button object constructed per the native GUI.
Q: What are the different types of controls in AWT?
A: The AWT supports the following types of controls:
Labels, Pushbuttons, Checkboxes, Choice lists, Lists, Scroll bars, Text
components These controls are subclasses of component.
Q: What is difference between Swing and AWT in Java?
A: Swing is a considered as light weight and AWT is considered as heavy weight.
Another difference between AWT and Swing is that, Swing offers uniform look
and feel across platform while look and feel of AWT GUI application are
platform dependent because AWT mostly use native components e.g. a AWT
windows will look different in DOS and Windows operating system.
Q: What interface is extended by AWT event listener?
A: The java.util.EventListener interface is extended by all the AWT event listeners.
Q: What is the default layout for Applet?
A: The default layout manager for an Applet is FlowLayout, and the FlowLayout
manager attempts to honor the preferred size of any components.
Q: Name Components subclasses that support painting?
A: The Canvas,Frame,Panel and Applet classes support painting.
Q: What is difference between BorderLayout and GridLayout ?
A: BorderLayout and GridLayout are two widely used LayoutManager from Swing
API, former arranges components in predefined position e.g. NORTH, SOUTH,
EAST and WEST while later arranges components one after other until there is
space and wraps them afterwards.
Q: What is the lifecycle of an applet?
A:
init() method � can be called when an applet is first loaded.
stop() method � can be used when the browser moves off the applet�s page.
destroy() method � can be called when the browser is finished with the appl
et.