0% found this document useful (0 votes)
54 views31 pages

Awt and Swing

Uploaded by

manojsathe123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views31 pages

Awt and Swing

Uploaded by

manojsathe123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

GUI Programming

What is AWT?
• AWT stands for Abstract Window Toolkit, a Java API that allows developers to
create graphical user interfaces (GUIs) for window-based applications.

• you cannot use AWT (Abstract Window Toolkit) for web applications; it is
specifically designed for creating graphical user interfaces (GUIs) on
desktop applications and is not compatible with web browsers
The package used for Swing
is java.awt
AWT Components
AWT? Components
An AWT component can be considered as an object that can be made visible on a graphical
interface screen and through which interaction can be performed.

In java.awt package, the following components are available:


AWT Components
1. Containers: As the name suggests, this awt component is used to hold other components.
Basically, there are the following different types of containers available in java.awt package:

a. Window: This is a top-level container and an instance of a window class that does not contain
a border or title.
b. Frame: Frame is a Window class child and comprises the title bar, border and menu bars.
Therefore, the frame provides a resizable canvas and is the most widely used container used for
developing AWT-based applications. Various components such as buttons, text fields, scrollbars
etc., can be accommodated inside the frame container.

2. Button: This is used to create a button on the user interface with a specified label. We can
design code to execute some logic on the click event of a button using listeners.

3. Text Fields: This component of java AWT creates a text box of a single line to enter text data.
AWT Components

5. Canvas: This generally signifies an area that allows you to draw shapes on a graphical user
interface.
AWT Components
6. Choice: This AWT component represents a pop-up menu having multiple choices. The option
which the user selects is displayed on top of the menu.
AWT Components
7. Scroll Bar: This is used for providing horizontal or vertical scrolling feature on the GUI.

8. List: This component can hold a list of text items. This component allows a user to choose
one or more options from all available options in the list.
AWT Components
9. Checkbox: This component is used to create a checkbox of GUI whose state can be either
checked or unchecked.

Checkbox CheckboxGrou
p
Swing Components
The package used for Swing
is javax.swing
9.1 Designing Graphical User Interfaces in Java:
Java LayoutManagers:

The LayoutManagers are used to


1.java.awt.BorderLayout
arrange components in a particular 2.java.awt.FlowLayout

manner. The Java 3.java.awt.GridLayout


4.javax.swing.BoxLayout
LayoutManagers facilitates us to
5.java.awt.CardLayout
control the positioning and size of the 6.java.awt.GridBagLayout

components in GUI forms. 7.javax.swing.GroupLayout


8.javax.swing.ScrollPaneLayout
LayoutManager is an interface
9.javax.swing.SpringLayout etc.
that is implemented by all the classes

of layout managers. There are the

following classes that represent the


9.1 Designing Graphical User Interfaces in Java:
Java LayoutManagers: 1. BorderLayout
Java 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:
1.public static final int NORTH
2.public static final int SOUTH
3.public static final int EAST
4.public static final int WEST
5.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.
9.1 Designing Graphical User Interfaces in Java:
Java 1. BorderLayout
LayoutManagers
import java.awt.*;
: import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be label
ed as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be label
ed as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled
as EAST
JButton b4 = new JButton("WEST");; // the button will be labele
d as WEST
JButton b5 = new JButton("CENTER");; // the button will be labe
led as CENTER
f.setLayout(new BorderLayout(20, 15));
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the Nort
h Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the Sout
h Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East D
irection
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West
Direction
9.1 Designing Graphical User Interfaces in Java:
Java 2. GridLayout
LayoutManagers
:
The Java GridLayout class is used to arrange the components in a rectangular grid. One component is
displayed in each rectangle.

Constructors of GridLayout class


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.
9.1 Designing Graphical User Interfaces in Java:
Java 2. GridLayout
LayoutManagers
:import java.awt.*;
import javax.swing.*; // setting the grid layout using the parameterless constructor
frameObj.setLayout(new GridLayout());
public class GridLayoutExample
{ frameObj.setSize(600, 300);
JFrame frameObj; frameObj.setVisible(true);
}
// constructor
GridLayoutExample() // main method
{ public static void main(String argvs[])
frameObj = new JFrame(); {
new GridLayoutExample();
JButton btn1 = new JButton("1"); }
JButton btn2 = new JButton("2"); }
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");

frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);


frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
9.1 Designing Graphical User Interfaces in Java:
Java 2. GridLayout
LayoutManagers
:import java.awt.*; frame.setLayout(new GridLayout(3,3))
import javax.swing.*;
frameObj.setSize(600, 300);
frameObj.setVisible(true);
public class MyGridLayout
}
{
JFrame frameObj;
// main method
public static void main(String argvs[])
// constructor
{
MyGridLayout()
new MyGridLayout();
{
}
frameObj = new JFrame();
}
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");

frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);


frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
9.1 Designing Graphical User Interfaces in Java:
Java 3. FlowLayout
LayoutManagers
:The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It
is the default layout of the applet or panel.
frameObj.setLayout(new FlowLayout());
import java.awt.*;
import javax.swing.*;
frameObj.setSize(300, 300);
frameObj.setVisible(true);
public class FlowLayoutExample
}
{
// main method
JFrame frameObj;
public static void main(String argvs[])
{
// constructor
new FlowLayoutExample();
FlowLayoutExample()
}
{
}
frameObj = new JFrame();

JButton b1 = new JButton("1");


JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("10");

// adding the buttons to frame


frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);
frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frameObj.add(b8);
frameObj.add(b9); frameObj.add(b10);
9.1 Designing Graphical User Interfaces in Java:
Java 4. BoxLayout
LayoutManagers
:The Java BoxLayout class is used to arrange the components either vertically or horizontally. For
this purpose, the BoxLayout class provides followong constants. They are as follows:

1.public static final int X_AXIS: Alignment of the components are horizontal

from left to right.

Constructor 2.public static


of BoxLayout final int Y_AXIS: Alignment of the components are vertical
class
1.BoxLayout(Container c, int axis): creates a box layout that arranges the components with the
given axis. from top to bottom.
9.1 Designing Graphical User Interfaces in Java:
Java Example of BoxLayout class with Y-AXIS:
4. BoxLayout
LayoutManagers
: import java.awt.*;
import javax.swing.*;

public class BoxLayoutExample1 extends Frame {


Button buttons[];

public BoxLayoutExample1 () {
buttons = new Button [5];

for (int i = 0;i<5;i++) {


buttons[i] = new Button ("Button " + (i + 1));
// adding the buttons so that it can be displayed
add (buttons[i]);
}

// the buttons will be placed horizontally


setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}

// main method
public static void main(String args[]){
BoxLayoutExample1 b=new BoxLayoutExample1();
}
}
9.1 Designing Graphical User Interfaces in Java:
Design Registration Form

You might also like