Escolar Documentos
Profissional Documentos
Cultura Documentos
- The basic idea behind the AWT is that a java window is a set of nested
components starting from the outermost window all the way down to the
smallest User Interface component.
- The AWT classes are contained in the java.awt package. It is one of the
java’s largest packages.
- The AWT provides the basic GUI components that are used in java applets
and applications. The AWT provides a machine-independent interface for
applications.
a) Visual Components
b) Container Components
c) Menu Components
The most common methods in the Object and Component classes are:
Containers
- The two important things done with the container is establishing the
container’s layout manager and adding components to the container.
Container is the abstract subclass of component, which allows other
components to be nested inside it. These components can also be
containers allowing other components to be nested inside it.
1. Window – It is a freestanding native window on the display. Window
has Frame and Dialog as its subclasses
3. Dialog – A Dialog does not have a menu bar. Although u can move it,
you cannot resize it.
- Incase u are not using the default layout managers associated with each
container, we must ourselves place the components using the 3 methods –
setLocation(), setSize() and setBounds().
- The layout manager can override your decision. If you must control the size
or position of components in such a way that cannot be done using the
standard layout managers, you can disable the layout manager by issuing
the following method call in your container
Con.setLayout (null); after this u are responsible for the positioning of the
components.
Frame
- Inherits from the container and adds components with the add() method.
- By Default, the Frame is invisible and hence always we have to use the
method setVisible(true).
Import java.awt.*;
pvsm
{
MyFrame fr = new MyFrame(“Hello”);
fr.setSize(500,500);
fr.setBackground(Color.blue);
fr.setVisible(true);
{
}
IMP: You will have to set visible to true in the case of Frame as by default it is
set to invisible. You can either use setSize() or Pack() (Which is a method of the
Window class) also.
Frames are capable of generating the following types of window events:
WindowOpened, WindowClosing, WindowClosed, WindowIconified,
WindowDeiconified, WindowActivated, WindowDeactivated.
- Call the show () method of the window to make the Frame appear on the
screen or to bring it to the front of the window stack.
- Call dispose () method when the frame is no longer needed so that it can
release its window system resources for reuse.
Be default, Java sets the frame’s title to untitled. Alternatively a frame can be
created with a specific title. The following line of code, creates a Frame object
with the title “The Frame has a title”.
7. void setTitle (String title) Sets title for Frame to given title
import java.awt.*;
import java.lang.*;
import java.applet.Applet;
Frame's behavior is curious. If you leave the page where the Frame was
created, the Frame will still be active and visible. This may or may not be the
behavior you want. Furthermore, if you remove the dispose () method from the
destroy call, you can get some downright undesirable behavior. In some
browsers, you could create multiple instances of the frame by reopening its
location reference.
An applet that displays the frame only when you are on the applet's page
import java.awt.*;
import java.lang.*;
import java.applet.Applet;
PANELS
- Panel is a container that does not have its own window. It is contained
within some other container.
- Applet is a subclass of Panel and thus Applets are displayed in a Panel that
is contained within a web browser or applet viewer.
PVSM
{
FrameWithPanel fr = new FrameWithPanel(“Panel inside a Frame”);
Panel pan = new Panel();
fr.setSize(200,200);
fr.setBackGround(Color.blue);
fr.setLayout(null); // override the default layout manager
pan.setSize(100,100);
pan.setBackground(Color.Yellow);
fr.add(pan);
fr.setVisible(true);
}
}
Layout Managers
Flow Layout –
Constructor Summary
FlowLayout()
Constructs a new Flow Layout with a centered alignment and a default
5-unit horizontal and vertical gap.
FlowLayout(int align)
Constructs a new Flow Layout with the specified alignment and a default
5-unit horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap)
Creates a new flow layout manager with the indicated alignment and the
indicated horizontal and vertical gaps.
import java.awt.*;
class Ex
{
Frame f
Button b1;
Button b2;
f.add(b1);
f.add(b2);
f.pack();
f.setVisible(true);
}
PSVM
{
Ex a = new Ex();
a.go();
}
}
import java.awt.*;
import java.applet.Applet;
BORDER Layout
- This is the default layout manager for Frame and Dialog. It has five distinct
areas: North, South, East, West and Center. It is indicated by
BorderLayout.NORTH and so on.
When adding a component to a container with a border layout, use one of these
five constants, for example:
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(new Button("Okay"), BorderLayout.SOUTH);
import java.awt.*;
import java.applet.Applet;
- You can add only a single component to each of the 5 regions of the
BorderLayot manager and if u add more than one, only the one
added last is visible.
Constructor Summary
BorderLayout()
Constructs a new border layout with no gaps between components.
BorderLayout(int hgap, int vgap)
Constructs a border layout with the specified gaps between
components.
Example
f.add(“center”,”b1”);
Individual Components
Labels
- Labels are effectively text strings that you can use to label other UI
components.
- The advantage that a label has over an ordinary text string is that it follows
the layout of the given panel and you do not have to worry about repainting
it every time the panel is redrawn. Labels also can be easily aligned within a
panel, enabling you to attach labels to other UI components without
knowing exact pixel position.
The available alignments are stored in class variables in Label, making them
easier to remember. Label.RIGHT, Label.LEFT, Label.CENTER. You can add a
Label with the following command.
add(new Label(“Hello”,Label.CENTER));
Once you have a label object, you can use methods defined in the Label class
to get and set the values of the text as shown in table.
Button
- The Button is the simples UI components that trigger some action in your
interface when they are pressed.
- They are usually used for Ok and Cancel buttons in dialog box.
Once you have a button object, you can get the value of the button’s label by
using the getLabel () method and set the label using the setLabel(String)
methods. You can add a button as under:
Canvas
The default size of the canvas is uselessly small and one way to deal with this
problem is to use a layout manager that will resize the canvas and the other
way to call setSize () on the canvas ourselves. For eg.
Checkbox
- Checkboxes are user interface components that have two states on and off,
checked and unchecked etc.
- Unlike Buttons, Checkboxes usually do not trigger direct action in an UI, but
instead are used to indicate optional features of some action.
The latter kinds of checkboxes are called radio buttons or checkbox groups and
non-exclusive checkboxes can be created by using the Checkbox Class.
To create a Checkbox,
Then create and add the individual checkboxes using the group as the second
argument and whether or not that checkbox is selected (only one in the series
can be selected).
Choice
- The choice component allows a user to select one of the several options or
items in one place.
- To create a choice list, create a instance of the Choice class and then use
addItem () method to add individual items to it in the order in which they
should appear.
c.addItem (“Singapore”);
c.addItem (“Bombay”);
The class Choice provides methods that allow you to access or set the currently
selected item in the list. The methods are:
8 insert (String, int) Inserts the item into this choice at the
specified position.
Choices, like check boxes send Item events when they are selected.