Chap_01_Abstract Windowing Toolkit(modify)
Chap_01_Abstract Windowing Toolkit(modify)
CHAPTER 01
To Introduce AWT
To discuss the classes present in the java.awt package
To understand the 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
INTRODUCTION OF AWT
9
Some Types Of Components
Button Checkbox
Label
Choice Scrollbar
Button
Checkbox
CheckboxGroup
10
Windows and Frames
The Window class defines a top-level Window with no Borders or Menu bar.
Usually used for application splash screens
Frame defines a top-level Window with Borders and a Menu Bar
Frames are more commonly used than Windows
Once defined, a Frame is a Container which can contain Components
Panels
When writing a GUI application, the GUI portion can become
quite complex.
To manage the complexity, GUIs are broken down into groups of
components. Each group generally provides a unit of
functionality.
A Panel is a rectangular Container whose sole purpose is to hold
and manage components within a GUI.
Container & Components Together for GUI
Container (Applet)
Containers (Panels)
Component
(Canvas)
Components
(Buttons)
Components (TextFields)
Components (Labels)
1.2 Creating Windowed Program And Applet
Download JDK 8
Note:- Latest Version of JDK is JDK 17 ,but it is not possible to test your applet program with
appletviewer tool because this tool is not present with later version of jdk, above 8 so we download
and use JDK 8
Install And Run 1st Program With Applet
Creating Windowed Program with Applet
import java.awt.*;
import java.applet.*;
public class sampleApplet extends Applet
{
public void init()
{ Frame ob=new Frame();
ob.setSize(200,200);
ob.setTitle("Frame Program");
ob.setVisible(true);
}
public void paint(Graphics g)
{ g.drawString("Inside Applet",20,10);
}
} /* <applet code="sampleApplet" width=200 height=300></applet> */
1.3 AWT Controls And Managers:
use Of
Labels,
Buttons,
checkbox,
checkboxGroup,
scrollBars,
TextField,
TextArea,
List,
Adding Control on Applet window.
Java AWT Label
3. void setLabel (String It sets the label of button with the specified
label) string.
4. String getLabel() It fetches the label of the button.
Example
Sr Constructor Description
1. Checkbox() It constructs a checkbox with no string as the label.
2. Checkbox(String label) It constructs a checkbox with the given label.
3. Checkbox(String label, It constructs a checkbox with the given label and
boolean state) sets the given state.
4. Checkbox(String label, It constructs a checkbox with the given label, set
boolean state, the given state in the specified checkbox group.
CheckboxGroup group)
5. Checkbox(String label, It constructs a checkbox with the given label, in the
CheckboxGroup group, given checkbox group and set to the specified state.
boolean state)
Checkbox Class Methods
1. // creating a frame
2. Frame f = new Frame("TextField Example");
3. // creating objects of textfield
4. TextField t1, t2;
5. // instantiating the textfield objects
6. // setting the location of those objects in the frame
7. t1 = new TextField("Welcome to Javatpoint.");
8. t1.setBounds(50, 100, 200, 30);
9. t2 = new TextField("AWT Tutorial");
10. t2.setBounds(50, 150, 200, 30);
11. // adding the components to frame
12. f.add(t1);
13. f.add(t2);
Java AWT Textarea
1. // creating a frame
2. Frame f = new Frame(List Example);
3. // creating a text area
4. List l1 = new List(“summer");
5. // setting location of text area in frame
6. area.setBounds(10, 30, 300, 300);
7. // adding text area to frame
8. f.add(area);
1.4 Use of Layout Managers:
FlowLayout
BorderLayout
GridLayout
CardLayout
GridbagLayout
MenuBars
Menus
DialogBoxes
FileDialog
JAVA LAYOUTMANAGERS
It arranges the components in a container like the words on a page. It fills the
top line from left to right and top to bottom. The components are arranged
in the order as they are added i.e. first components appears at top left, if the
container is not wide enough to display all the components, it is wrapped
around the line. Vertical and horizontal gap between components can be
controlled. The components can be left, center or right aligned.
•public void next(Container parent): is used to flip to the next card of the
given container.
•public void previous(Container parent): is used to flip to the previous card
of the given container.
•public void first(Container parent): is used to flip to the first card of the
given container.
•public void last(Container parent): is used to flip to the last card of the given
container.
•public void show(Container parent, String name): is used to flip to the
specified card with the given name.
JAVA GRIDBAG LAYOUT
The Java GridBagLayout class is used to align components vertically,
horizontally or along their baseline.
The components may not be of the same size. Each GridBagLayout object
maintains a dynamic, rectangular grid of cells. Each component occupies one or
more cells known as its display area. Each component associates an instance of
GridBagConstraints. With the help of the constraints object, we arrange the
component's display area on the grid. The GridBagLayout manages each
component's minimum and preferred sizes in order to determine the
component's size. GridBagLayout components are also arranged in the
rectangular grid but can have many different sizes and can occupy multiple
rows or columns.
Constructor
GridBagLayout(): The parameterless constructor is used to create a grid bag layout
manager.
Specifying Constraints
The following code is typical of what goes in a container that uses a GridBagLayout. You will
see a more detailed example in the next section.
pane.add(theComponent, c);
As you might have guessed from the above example, it is possible to reuse the
same GridBagConstraints instance for multiple components, even if the components have
different constraints. However, it is recommended that you do not reuse GridBagConstraints, as
this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each
new instance.
gridx, gridy
Specify the row and column at the upper left of the component. The leftmost
column has address gridx=0 and the top row has address gridy=0.
Use GridBagConstraints.RELATIVE (the default value) to specify that the
component be placed just to the right of (for gridx) or just below (for gridy)
the component that was added to the container just before this component was
added. We recommend specifying the gridx and gridy values for each
component rather than just using GridBagConstraints.RELATIVE; this tends
to result in more predictable layouts.
gridwidth, gridheight
Specify the number of columns (for gridwidth) or rows (for gridheight) in the
component's display area. These constraints specify the number of cells the component
uses, not the number of pixels it uses. The default value is 1.
Use GridBagConstraints.REMAINDER to specify that the component be the last one in
its row (for gridwidth) or column (for gridheight).
Use GridBagConstraints.RELATIVE to specify that the component be the next to last
one in its row (for gridwidth) or column (for gridheight). We recommend specifying
the gridwidth and gridheight values for each component rather than just
using GridBagConstraints.RELATIVE and GridBagConstraints.REMAINDER; this
tends to result in more predictable layouts.
fill
Used when the component's display area is larger than the component's
requested size to determine whether and how to resize the component.
Valid values (defined as GridBagConstraints constants)
include NONE (the default), HORIZONTAL (make the component wide
enough to fill its display area horizontally, but do not change its
height), VERTICAL (make the component tall enough to fill its display
area vertically, but do not change its width), and BOTH (make the
component fill its display area entirely).
ipadx, ipady
Specifies the internal padding: how much to add to the size of the component.
The default value is zero. The width of the component will be at least its
minimum width plus ipadx*2 pixels, since the padding applies to both sides of
the component. Similarly, the height of the component will be at least its
minimum height plus ipady*2 pixels.
insets
Specifies the external padding of the component -- the minimum amount of space
between the component and the edges of its display area. The value is specified
as an Insets object. By default, each component has no external padding.
IPADX,IPADY AND INSETS
anchor
Used when the component is smaller than its display area to determine where
(within the area) to place the component. Valid values (defined
as GridBagConstraints constants) are CENTER (the
default), PAGE_START, PAGE_END, LINE_START, LINE_END, FIRST_
LINE_START, FIRST_LINE_END, LAST_LINE_END,
and LAST_LINE_START.
Here is a picture of how these values are interpreted in a container that has the
default, left-to-right component orientation.
FIRST_LINE_START PAGE_START FIRST_LINE_END
Version note: The PAGE_* and *LINE_* constants were introduced in 1.4. Previous releases require values named after points of the
compass. For example, NORTHEAST indicates the top-right part of the display area. We recommend that you use the new constants, instead,
since they enable easier localization.
weightx, weighty
Specifying weights is an art that can have a significant impact on the appearance of the
components a GridBagLayout controls. Weights are used to determine how to distribute
space among columns (weightx) and among rows (weighty); this is important for
specifying resizing behavior.
Unless you specify at least one non-zero value for weightx or weighty, all the components
clump together in the center of their container. This is because when the weight is 0.0
(the default), the GridBagLayout puts any extra space between its grid of cells and the
edges of the container.
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are
used as necessary. Larger numbers indicate that the component's row or column should
get more space. For each column, the weight is related to the highest weightx specified
for a component within that column, with each multicolumn component's weight being
split somehow between the columns the component is in. Similarly, each row's weight is
related to the highest weighty specified for a component within that row. Extra space
tends to go toward the rightmost column and bottom row.
WEIGHTX AND WEIGHTY
MENU, MENUITEMS AND MENUBAR CLASS
Constructor Description
Creates a menu bar to which one or many menus
public MenuBar()
are added.
Creates a menu with a title.
public Menu(String title)
public MenuItem(String title) Creates a menu item with a title.
JAVA AWT DIALOG
The Dialog control represents a top level window with a border and a
title used to take some form of input from the user. It inherits the
Window class.
Frame vs Dialog
Frame and Dialog both inherits Window class. Frame has maximize
and minimize buttons but Dialog doesn't have.
FILEDIALOG BOX
FileDialog control represents a dialog window from which the user can select a file.
Class declaration
Following is the declaration for java.awt.FileDialog class:
public class FileDialog extends Dialog
Field
•static int LOAD -- This constant value indicates that the purpose of the file dialog
window is to locate a file from which to read.
•static int SAVE -- This constant value indicates that the purpose of the file dialog
window is to locate a file to which to write.
S.N. Constructor & Description
1 FileDialog(Frame parent)
Creates a file dialog for loading a file.
2 FileDialog(Frame parent, String title)
Creates a file dialog window with the specified title for loading a
file.
3 FileDialog(Frame parent, String title, int mode)
Creates a file dialog window with the specified title for loading or
saving a file.
S.N. Method & Description
1 String getDirectory()
Gets the directory of this file dialog.
2 String getFile()
Gets the selected file of this file dialog.
3 FilenameFilter getFilenameFilter()
Determines this file dialog's filename filter.
4 int getMode()
Indicates whether this file dialog box is for loading from a file or for
saving to a file.
5 void setDirectory(String dir)
Sets the directory of this file dialog window to be the specified directory.
6 void setFile(String file)
Sets the selected file for this file dialog window to be the specified file.
7 void setFilenameFilter(FilenameFilter filter)
Sets the filename filter for this file dialog window to the specified filter.
8 void setMode(int mode)
Sets the mode of the file dialog.