You are on page 1of 43

Advanced Java Programming (22517)

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.*;

AWT features include:

1. A set of native user interface components.


2. A robust event-handling model.
3. Graphics and imaging tools, including shape, color, and font classes.
4. Layout managers, for flexible window layouts that do not depend on a
particular window size or screen resolution.
5. Data transfer classes, for cut-and-paste through the native platform clipboard
Component
Terminology used in awt.
1. Component
Component is an object having a graphical representation that can be displayed on
the screen and that can interact with the user.

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.

2. public void setSize(int width, int height)


Sets the size (width and height) of the component.

3. public void setLayout(LayoutManager m)


Defines the layout manager for the component.

4. public void setVisible(boolean status)

Changes the visibility of the component, by default false.

5. public void setBounds(int x, int y, int width, int height)


The first two arguments are x and y coordinates of the top-left corner of the
component, the third argument is the width of the component and the fourth
argument is the height of the component.
Frame class Functions from docs
import java.awt.*;
public class Frame_Example extends Frame {
Frame_Example() {
setSize(300, 300);
setTitle("This is our basic AWT example");
setLayout(null);
setVisible(true);
}
public static void main(String args[]) {
Frame_Example f = new Frame_Example();
}
}
import java.awt.*;

public class ButtonExample {


public static void main(String args[]) {
Frame f = new Frame("Button Example");
Button b = new Button("Click Here");
b.setBounds(50, 100, 80, 30);
f.add(b);
// set size, layout and visibility of frame
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}
public class CheckBox_Example {
CheckBox_Example() {
Frame f1 = new Frame(); // title as arg can be passed
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java", true);
//setting location of checkbox in frame
checkbox2.setBounds(100, 150, 50, 50);
//adding checkboxes to frame
f1.add(checkbox1);
f1.add(checkbox2);

f1.setSize(400, 400);
f1.setLayout(null);
f1.setVisible(true);
}

public static void main(String[] args) {


CheckBox_Example c1 = new CheckBox_Example();
}
}
CheckBox Constructors
import java.awt.*;
public class TextFieldExample {
public static void main(String args[]) {
Frame f = new Frame("TextField Example");
TextField t1, t2;
// setting the location of those objects in the frame
t1 = new TextField("Welcome to Javatpoint.");
t1.setBounds(50, 100, 200, 30);
t2 = new TextField( );
t2.setBounds(50, 150, 200, 30);
// adding the components to frame
f.add(t1); f.add(t2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
public class ScrollEx1 {

Frame frame1; Panel panel;


Label label1, label2;
ScrollEx1() {
frame1 = new Frame("Scrollbar");
panel = new Panel();
label1 = new Label(“Scrollbar in the Frame", Label.CENTER);
Scrollbar scrollB1 = new Scrollbar(Scrollbar.HORIZONTAL, 10, 40, 0, 100);
Scrollbar scrollB2 = new Scrollbar(Scrollbar.VERTICAL, 10, 60, 0, 100);

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");

f.add(c); // adding choice menu to frame


// setting size, layout and visibility of frame
f.setSize(400, 400);
f.setLayout( null );
f.setVisible(true);
}
public static void main(String args[]) {
new ChoiceExample1();
}
}
Applet Programs
import java.applet.*;
import java.awt.*;
/*
<applet code="CheckBoxApplet" width=300 height=300>
</applet>

*/
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");

List l1=new List();


l1.add("Roll no1 ");
l1.add("Roll no2 ");
l1.add("Roll no3 ");
l1.add("Roll no4 ");

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.

Constructors of FlowLayout class

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

• public static final int LEFT


• public static final int RIGHT
• public static final int CENTER
• public static final int LEADING
• public static final int TRAILING
public class CheckBoxExample extends Frame {
public static void main ( String []arg) {
Frame f1=new Frame();
Label l1[]=new Label[15];
for(int i=0;i<15;i++) {
l1[i]=new Label(i+"Year ");
}

for (int j=0;j<15;j++) {


f1.add(l1[j]);
}
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() );
}
}
2. BorderLayout
The BorderLayout is used to arrange the components in five regions: north,
south, east, west, and center.

Each region (area) may contain one component only.

It is the default layout of a frame or window.

The BorderLayout provides five constants for each region:


public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
Constructors of BorderLayout class:

BorderLayout():
creates a border layout but with no gaps between the components.

BorderLayout(int hgap, int vgap):


creates a border layout with the given horizontal and vertical gaps between the
components.
public class BorderLayoutExample
{
Frame f;
BorderLayoutExample(){
f = new Frame("Border Layout Manager example");

// 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.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction


f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the 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);

// setting grid layout of 3 rows and 3 columns


f.setLayout(new GridLayout(3, 3));
f.setSize(300, 300);
f.setVisible(true);
}

public static void main(String[] args) {


new GridLayoutExample();
}
}
Constructors of Grid Layout class

GridLayout():
creates a grid layout with one column per component in a row.

GridLayout(int rows, int columns):


creates a grid layout with the given rows and columns but no gaps between the
components.

GridLayout(int rows, int columns, int hgap, int vgap):


creates a grid layout with the given rows and columns along with given horizontal
and vertical gaps.
GridBagLayout
It 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 associates an instance of GridBagConstraints. With the help of


the constraints object, we arrange the component's display area on the grid.
Fields in GridBagLayout
public class GridBagLayoutExample extends JFrame
{ this.add(new Button("Button Three"), gbc);
public static void main(String[] args) { gbc.gridx = 1;
GridBagLayoutExample a = new gbc.gridy = 1;
GridBagLayoutExample();
}
this.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
public GridBagLayoutExample() { gbc.gridy = 2;
GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.HORIZONTAL;
setLayout(new GridBagLayout()); gbc.gridwidth = 2;
setTitle("GridBag Layout Example"); this.add(new Button("Button Five"), gbc);
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout); setSize(300, 300);
setPreferredSize(getSize());
gbc.fill = GridBagConstraints.HORIZONTAL; setVisible(true);
gbc.gridx = 0; setDefaultCloseOperation(EXIT_ON_CLOSE);
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);
}
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button two"), gbc);
}
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
4. CradLayout
The CardLayout class manages the components in such a manner that only one
component is visible at a time.
It treats each component as a card that is why it is known as CardLayout.

Constructors of CardLayout Class


CardLayout(): creates a card layout with zero horizontal and vertical gap.
CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and
vertical gap.

Methods of CardLayout Class


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.
public class CardLayoutExample extends JFrame implements ActionListener {
CardLayout crd; Button btn1, btn2, btn3; Container cPane;
CardLayoutExample() {
cPane = getContentPane();
crd = new CardLayout();
cPane.setLayout(crd);
btn1 = new Button("Apple");
btn2 = new Button("Boy");
btn3 = new Button("Cat");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
cPane.add("a", btn1); // first card is the button btn1
cPane.add("b", btn2); // first card is the button btn2
cPane.add(“c", btn3); // first card is the button btn3
}
public void actionPerformed(ActionEvent e) { crd.next(cPane); }

public static void main(String argvs[]) {


CardLayoutExample crdl = new CardLayoutExample();
crdl.setSize(300, 300);
crdl.setVisible(true);
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
MenuBar
import java.awt.*;
class MenuBarExample {
MenuBarExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");

menu.add(i1); menu.add(i2); menu.add(i3); submenu.add(i4); submenu.add(i5);


menu.add(submenu); mb.add(menu);

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.

It inherits the Window class.

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.

FileDialog(Dialog parent, String title)-Creates a file dialog window


with the specified title for loading a file.

FileDialog(Dialog parent, String title, int mode)-Creates a file


dialog window with the specified title for loading or saving a file.

FileDialog(Frame parent)-Creates a file dialog for loading a file.

FileDialog(Frame parent, String title)-Creates a file dialog window


with the specified title for loading a file.

FileDialog(Frame parent, String title, int mode)-Creates a file


dialog window with the specified title for loading or saving a file.
import java.awt.*;
import java.awt.FileDialog;
public class FileDilogExample {

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

You might also like