Unit1 Arrow Java
Unit1 Arrow Java
AWT Classes
The AWT classes are contained in the java.awt package.
It is one of Java‘s largest packages.
The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton,
CheckBox, Choice, List etc.
Components
All the elements like the button, text fields, scroll bars, etc. are called components.
In Java AWT, there are classes for each component as shown in above diagram.
In order to place every component in a particular position on a screen, we need to add them to a
container.
Component class is at the top of AWT hierarchy.
It is an abstract class that encapsulates all the attributes of visual component.
A component object is responsible for remembering the current foreground and background colors
and the currently selected text font.
Types of containers:
There are four types of containers in Java AWT:
1. Window
2. Panel
3. Frame
4. Dialog
Window
The window is the container that have no borders and menu bars.
A top-level window is not contained within any other object; it sits directly on the desktop.
Generally, we won‘t create Window objects directly.
Instead, we will use a subclass of Window called frame or dialog for creating a window.
Panel
The Panel is the container that doesn't contain title bar, border or menu bar.
It is generic container for holding the components.
It can have other components like button, text field etc.
An instance of Panel class creates a container, in which we can add components.
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.
Method Description
Frame()
Constructs a new instance of Frame that is initially invisible.
Example 2: Using Frame() constructor
import java.awt.*;
class Sample extends Frame
{
Sample() //default
{
setVisible(true);
setSize(300,300);
setTitle("Welcome to Arrow");
}
public static void main(String args[])
{
// Creating the instance of Frame
Sample fr=new Sample();
}
}
import java.awt.*;
class Sample extends Frame
{
Sample(String title)
{
super(title);
}
public static void main(String args[])
{
Sample fr=new Sample("Welcome to Arrow World");
fr.setVisible(true);
fr.setSize(300,300);
}
}
Let's see a simple example of AWT where we are creating instance of Frame class.
import java.awt.*;
class Sample
{
public static void main(String args[])
{
// Creating the instance of Frame
Frame fr=new Frame();
fr.setVisible(true);
fr.setSize(300,300);
fr.setTitle("Welcome to Arrow");
}
}
2. Label(String text) It constructs a label with the given string (left justified by default).
3. Label(String text, int It constructs a label with the specified string and the specified
alignement) alignment.
1. void setText(String text) It sets the texts for label with the specified text.
2. void setAlignment(int alignment) It sets the alignment for label with the specified
alignment.
In the absence of a layout manager, the position and size of the components have to be set manually.
The setBounds() method is used in such a situation to set the position and size.
To specify the position and size of the components manually, the layout manager of the frame can be null.
The setBounds(int x-axis, int y-axis, int width, int height) method is used in the above example that sets the
position of the awt label.
import java.awt.*;
class Sample
{
public static void main(String args[])
{
1. Button( ) It constructs a new button with an empty string i.e. it has no label.
2. Button (String text) It constructs a new button with given string as its label.
1. void setLabel (String label) It sets the label of button with the specified string.
Example:
import java.awt.*;
class ButtonExample {
public static void main (String[] args) {
2. TextField(String It constructs a new text field initialized with the given string text
text) to be displayed.
Constructors of TextArea
Constructor Description
public TextArea() Creates a new TextArea..
public TextArea(String text) Creates a new TextArea with specified text.
Creates a new TextArea with specified number of rows and
public TextArea(int rows, int columns)
columns.
public TextArea(String text, int rows, int Creates a new TextArea with a text and a number of rows
columns) and columns.
import java.awt.*;
public class TextAreaExample
{
public static void main(String args[])
{
Frame f = new Frame();
// creating a text area
TextArea area = new TextArea();
// setting location of text area in frame
Label l1 = new Label("Enter Address");
area.setBounds(100, 30, 200, 100);
l1.setBounds(20, 50, 80, 30);
f.add(l1);
f.add(area);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
Constructors of Checkbox
Constructor Description
import java.awt.*;
public class CheckboxExample1
{
public static void main (String args[])
{
Frame f = new Frame("Checkbox Example");
// creating the checkboxes
Checkbox checkbox1 = new Checkbox("C++",true);
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java");
checkbox2.setBounds(150, 100, 50, 50);
Checkbox checkbox3 = new Checkbox("Python");
checkbox3.setBounds(200, 100, 50, 50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
import java.awt.*;
class ChoiceExample1
{
public static void main(String args[])
{
Frame f = new Frame();
// creating a choice component
Choice c = new Choice();
// setting the bounds of choice menu
c.setBounds(100, 100, 75, 75);
// adding items to the choice menu
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
c.add("Item 4");
c.add("Item 5");
// adding choice menu to frame
f.add(c);
// setting size, layout and visibility of frame
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}
2. List(int row_num) It constructs a new scrolling list initialized with the given
number of rows visible.
import java.awt.*;
public class ListExample1 {
public static void main(String args[]) {
Frame f = new Frame();
List l1 = new List(5);
List l2 = new List(5,true);
l1.setBounds(100, 100, 75, 75);
l2.setBounds(200, 100, 75, 75);
l1.add("C");
l1.add("C++");
l1.add("Java");
l1.add("DCC");
l1.add("SEN");
l2.add("CSS");
l2.add("AJP");
l2.add("OSY");
l2.add("STE");
l2.add("EST");
f.add(l1);
f.add(l2);
Scrollbar is a GUI component allows us to see invisible number of rows and columns.
Scrollbar()
Scrollbar(int orientation)
Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
Constructs a new scroll bar with the specified orientation, initial value, visible amount, and
minimum and maximum values.
o Value: specify the starting position of the knob of Scrollbar on its track.
o visible amount : The value range represented by the bubble.
o Minimum: specify the minimum width of track on which scrollbar is moving.
o Maximum: specify the maximum width of track on which scrollbar is moving.
A scroll bar can represent a range of values. For example, if a scroll bar is used for scrolling
through text, the width of the "bubble" (also called the "thumb" or "scroll box") can be used to
represent the amount of text that is visible. Here is an example of a scroll bar that represents a
range:
The horizontal scroll bar in this example could be created with code like the following:
public MenuBar() Creates a menu bar to which one or many menus are added.
//Creating Menu
Menu m1=new Menu("File");
m1.add(i1);
m1.add(i2);
m1.add(i3);
mb.add(m1);
f.setMenuBar(mb);
f.setSize(400,400);
import java.awt.*;
class MenuExample
{
public static void main(String args[])
{
Frame f = new Frame("MenuBar, Menu and MenuItems");
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Layout Manager
The layout manager automatically positions all the components within the container.
If we do not use layout manager then also the components are positioned by the default layout
manager.
It is possible to layout the controls by hand but it becomes very difficult because of the following
two reasons.
It is very tedious to handle a large number of controls within the container.
Oftenly the width and height information of a component is not given when we need to
arrange them.
Java provide us with various layout manager to position the controls.
The properties like size,shape and arrangement varies from one layout manager to other layout
manager.
When the size of the applet or the application window changes the size, shape and arrangement of
the components also changes in response i.e. the layout managers adapt to the dimensions of
appletviewer or the application window.
The layout manager is associated with every Container object.
- To apply Layout Manager, we use setLayout().
void setLayout(LayoutManager object);
- If you want to use setBounds() then please make sure that you have set null to setLayout() method.
Field
Following are the fields for java.awt.BorderLayout class:
static int CENTER -- This value indicates that each row of components should be centered.
static int LEADING -- This value indicates that each row of components should be justified
to the leading edge of the container's orientation, for example, to the left in left-to-right
orientations.
static int LEFT -- This value indicates that each row of components should be left-justified.
static int RIGHT -- This value indicates that each row of components should be right-
justified.
static int TRAILING -- This value indicates that each row of components should be justified
to the trailing edge of the container's orientation, for example, to the right in left-to-right
orientations.
1
FlowLayout()
Constructs a new FlowLayout with a centered alignment and a default 5-unit
horizontal and vertical gap.
2
FlowLayout(int align)
Constructs a new FlowLayout with the specified alignment and a default 5-unit
horizontal and vertical gap.
3
FlowLayout(int align, int hgap, int vgap)
Creates a new flow layout manager with the indicated alignment and the
indicated horizontal and vertical gaps.
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.add(b10);
f1.setVisible(true);
f1.setSize(500,500);
}
}
Example 2
import java.awt.*;
class FlowLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("FlowLayout Manager");
// parameterized constructor is used
// where alignment is left
// horizontal gap is 20 units and vertical gap is 20 units.
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.add(b10);
f1.setVisible(true);
f1.setSize(500,500);
}
}
1. GridLayout(): creates a grid layout with one column per component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the given rows and columns
but no gaps between the components.
3. 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.
The GridLayout() constructor creates only one row. The following example shows the usage of the
default constructor.
import java.awt.*;
class GridLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("GridLayout Manager");
GridLayout gl=new GridLayout();
f1.setLayout(gl);
Button b1=new Button("B1");
Button b2=new Button("B2");
Button b3=new Button("B3");
Button b4=new Button("B4");
Button b5=new Button("B5");
Button b6=new Button("B6");
Button b7=new Button("B7");
Button b8=new Button("B8");
Button b9=new Button("B9");
Button b10=new Button("B10");
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
import java.awt.*;
class GridLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("GridLayout Manager");
GridLayout gl=new GridLayout(3,3);
f1.setLayout(gl);
Button b1=new Button("B1");
Button b2=new Button("B2");
Button b3=new Button("B3");
Button b4=new Button("B4");
Button b5=new Button("B5");
Button b6=new Button("B6");
Button b7=new Button("B7");
Button b8=new Button("B8");
Button b9=new Button("B9");
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.setVisible(true);
Example of GridLayout class: Using GridLayout(int rows, int columns, int hgap,
int vgap) Constructor
import java.awt.*;
class GridLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("GridLayout Manager");
GridLayout gl=new GridLayout(3,3,20,30);
f1.setLayout(gl);
Button b1=new Button("B1");
Button b2=new Button("B2");
Button b3=new Button("B3");
Button b4=new Button("B4");
Button b5=new Button("B5");
Button b6=new Button("B6");
Button b7=new Button("B7");
Button b8=new Button("B8");
Button b9=new Button("B9");
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.setVisible(true);
f1.setSize(500,500);
}
}
o BorderLayout(): creates a border layout but with no gaps between the components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and
vertical gaps between the components.
import java.awt.*;
class BorderLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("BorderLayout Manager");
BorderLayout bl=new BorderLayout();
f1.setLayout(bl);
Button b1=new Button("B1");
Button b2=new Button("B2");
Button b3=new Button("B3");
Button b4=new Button("B4");
Button b5=new Button("B5");
f1.add(b1,BorderLayout.NORTH);
f1.add(b2,BorderLayout.SOUTH);
f1.add(b3,BorderLayout.EAST);
f1.add(b4,BorderLayout.WEST);
f1.add(b5,BorderLayout.CENTER);
f1.setVisible(true);
f1.setSize(500,500);
import java.awt.*;
class BorderLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("BorderLayout Manager");
BorderLayout bl=new BorderLayout(20,20);
f1.setLayout(bl);
Button b1=new Button("B1");
Button b2=new Button("B2");
Button b3=new Button("B3");
Button b4=new Button("B4");
Button b5=new Button("B5");
f1.add(b1,BorderLayout.NORTH);
f1.add(b2,BorderLayout.SOUTH);
f1.add(b3,BorderLayout.EAST);
f1.add(b4,BorderLayout.WEST);
f1.add(b5,BorderLayout.CENTER);
f1.setVisible(true);
f1.setSize(500,500);
}
}
import java.awt.*;
class BorderLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("BorderLayout Manager");
BorderLayout bl=new BorderLayout(20,20);
f1.setLayout(bl);
f1.setVisible(true);
f1.setSize(500,500);
}
}
CardLayout
The java.awt.CardLayout layout manager is significantly different from the other layout managers.
Unlike other layout managers, that display all the components within the container at once, a
CardLayout layout manager displays only one component at a time (The component could be a
component or another container).
Each component in a container with this layout fills the entire container. The
name cardlayout evolves from a stack of cards where one card is piled upon another and only one
of them is shown. In this layout, the first component that you add to the container will be at the top
of stack and therefore visible and the last one will be at the bottom.
o public void next(Container parent): is used to flip to the next card of the given container.
o public void previous(Container parent): is used to flip to the previous card of the given
container.
o public void first(Container parent): is used to flip to the first card of the given container.
o public void last(Container parent): is used to flip to the last card of the given container.
o public void show(Container parent, String name): is used to flip to the specified card with
the given name.
import java.awt.*;
import java.awt.event.*;
class CardLayoutExample extends Frame implements ActionListener
{
CardLayout card = new CardLayout(20,20);
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.
ipadx, ipady
Specifies the internal padding: how much to add to the size of the component.
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 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).
import java.awt.*;
public class GridBagLayoutExample extends Frame
{
public GridBagLayoutExample()
{
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBag Layout Example");
gbc.gridx = 1;
gbc.gridy = 0;
add(new Button("Button two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
add(new Button("Button Three"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
add(new Button("Button Five"), gbc);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args)
{
GridBagLayoutExample a = new GridBagLayoutExample();
}
import java.awt.*;
class PanelJavaExample
{
public static void main(String[] args)
{
Frame f1 = new Frame();
f1.setLayout(new FlowLayout());
Panel panel1 = new Panel();
Label lb1 = new Label("Panel with Green Background");
lb1.setForeground(Color.red);
panel1.add(lb1);
Button bt1 = new Button("1");
panel1.add(bt1);
panel1.setBackground(Color.green);
f1.add(panel1);
Class constructors
1 Dialog(Dialog owner)
Constructs an initially invisible, modeless Dialog with the specified owner
Dialog and an empty title.
Example
import java.awt.*;
import java.awt.event.*;
public class DialogExample1
{
public static void main(String args[])
{
import java.awt.*;
import java.awt.event.*;
public class DialogExample extends Frame implements ActionListener
{
Dialog d;
DialogExample f;
DialogExample()
{
d = new Dialog(f,"Dialog Example", true);
d.setLayout( new FlowLayout());
Button b = new Button ("OK");
b.addActionListener (this);
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public void actionPerformed( ActionEvent e )
{
d.setVisible(false);
}
public static void main(String args[])
{
DialogExample f = new DialogExample();
}
}
The FileDialog is a subclass of Dialog class that displays a dialog window from which the user can
select a file.
Since it is a modal dialog, when the application calls its show method to display the dialog, it
blocks the rest of the application until the user has chosen a file.
There is no Lyaout Mangaer for FileDialog.
1 FileDialog(Frame parent)
Creates a file dialog for loading a file.
Field
import java.awt.*;
class FileDialogDemo
{
public static void main(String args[])
{
Frame f1=new Frame();
f1.setVisible(true);
f1.setTitle("Parent Window");
f1.setSize(800,800);
//FileDialog fd1=new FileDialog(f1,"Save",FileDialog.SAVE);
FileDialog fd1=new FileDialog(f1,"Open",FileDialog.LOAD);
//fd1.setDirectory("C:\\");
fd1.setSize(500,500);
fd1.setVisible(true);
}
}