Unit - 1
Unit - 1
Unit - 1
Mundhe Shankar G.
Lecturer In Information Technology
Government Polytechnic Nanded.
Unit -1
AWT
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI)
programming.
import java.awt.*;
For examples buttons, checkboxes, list and scrollbars of a graphical user interface.
2. Container
A sub class of Component that can contain other components .
3. Panel
Panel is the simplest concrete subclass of Container.
Panel is a window that does not contain a title bar ,menu bar or border.
Components can be added to panel using function add( )
4. Window
Window is a rectangular area which is displayed on the screen.
Window class creates a top level window .
A top level window is not contained within any other object ,it sits
directly on the desktop .
Generally you don’t create Windows object directly instead you will
use subclass of window called Frame .
5. Frame
A Frame encapsulates Window. Frame is a subclass of Window and
has title bar ,menu bar ,borders and resizing corners.
6. Canvas
Derived form Component .
Canvas encapsulates a blank window upon which you can draw.
Useful methods of component class .
1. public void add(Component c)
Inserts a component on this component.
f1.setSize(400, 400);
f1.setLayout(null);
f1.setVisible(true);
}
frame1.add(label1, BorderLayout.NORTH);
frame1.add(scrollB2, BorderLayout.EAST);
frame1.add(scrollB1, BorderLayout.SOUTH);
frame1.setSize(370, 270);
frame1.setVisible(true);
}
public static void main(String... ar) {
new ScrollEx1();
}
}
public class ChoiceExample1 extends Frame{
ChoiceExample1() {
Frame f = new Frame();
Choice c = new Choice();
c.setBounds(115, 50, 150, 100); // setting the bounds of choice menu
Label l1=new Label("Select Semester");
l1.setBounds(10,25,100,100);
f.add(l1);
c.add("IT first Sem ");
c.add("IT Second Sem");
c.add("IT Third Sem");
c.add("IT Fourth Sem");
*/
public class CheckBoxApplet extends Applet{
public void init() {
Checkbox chb1=new Checkbox("First Year ");
Checkbox chb2=new Checkbox("Second Year ");
Checkbox chb3=new Checkbox("Third Year ");
add(chb1);
add(chb2);
add(chb3);
}
}
public class CheckBox1 extends Frame {
public static void main ( String []arg) {
Frame f1=new Frame();
Checkbox chb1=new Checkbox("First Year ");
Checkbox chb2=new Checkbox("Second Year ");
Checkbox chb3=new Checkbox("Third Year ");
f1.add(chb1);
f1.add(chb2);
f1.add(chb3);
f1.setVisible(true);
f1.setSize(350, 350);
f1.setLayout(new FlowLayout() );
}
}
public class ListExapmle {
public static void main(String[] args) {
Frame f1=new Frame ("List Example");
f1.add(l1);
f1.setVisible(true);
f1.setSize(500,500);
}
}
Layout Manager
LayoutManager is an interface that is implemented by all the classes of layout
managers.
The LayoutManagers are used to arrange components in a particular manner.
There are the following classes that represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
1. FlowLayout
The flow layout is the default layout manager for all Panel objects and applets.
It arranges the components as they are added when no more components fit on a
line ,it begins with next line.
By default it keeps 5 pixels between vertical and horizontal components.
FlowLayout():
creates a flow layout with centered alignment and a default 5 unit horizontal and
vertical gap.
FlowLayout(int align):
creates a flow layout with the given alignment and a default 5 unit horizontal and
vertical gap.
FlowLayout(int align, int hgap, int vgap):
creates a flow layout with the given alignment and the given horizontal and vertical
gap.
• Fields of FlowLayout class
f1.add(chb1);
f1.add(chb2);
f1.add(chb3);
f1.setVisible(true);
f1.setSize(350, 350);
f1.setLayout(new FlowLayout() );
}
}
2. BorderLayout
The BorderLayout is used to arrange the components in five regions: north,
south, east, west, and center.
BorderLayout():
creates a border layout but with no gaps between the components.
// creating buttons
Button b1 = new Button("NORTH"); // the button will be labeled as NORTH
Button b2 = new Button("SOUTH"); // the button will be labeled as SOUTH
Button b3 = new Button("EAST"); // the button will be labeled as EAST
Button b4 = new Button("WEST"); // the button will be labeled as WEST
Button b5 = new Button("CENTER"); // the button will be labeled as CENTER
f.setSize(500, 500);
f.setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutExample();
}
}
3.GridLayout
public class GridLayoutExample {
Frame f;
GridLayoutExample() {
f = new Frame();
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
// adding buttons to the frame
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);
f.add(b7);f.add(b8);f.add(b9);
GridLayout():
creates a grid layout with one column per component in a row.
The components may not be of the same size. Each GridBagLayout object
maintains a dynamic, rectangular grid of cells.
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true); }
public static void main(String args[]) { new MenuBarExample(); } }
DialogBox
The Dialog control represents a top level window with a border and a title used to
take some form of input from the user.
Frame and Dialog both inherits Window class. Frame has maximize and minimize
buttons but Dialog doesn't have.
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static Dialog d;
DialogExample() {
Frame f= new Frame();
d = new Dialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
});
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
File Dialog
FileDialog(Dialog parent)-Creates a file dialog for loading a file.
FileDilogExample(){
Frame f=new Frame();
FileDialog f1=new FileDialog(new Frame() );
f1.setVisible(true);
f1.setLayout(null);
}
public static void main(String[] args) {
new FileDilogExample();
}
}
• The end