SlideShare a Scribd company logo
AWT Classes
AWT Classes
• The classes and interfaces of the AWT are
used to develop stand alone applications and
to implement the GUI controls used by
Applets.
Window Fundamentals
• The AWT defines windows according to a class
hierarchy that adds functionality and
specificity with each level.
• The two most common windows are:
1. Panel, which is used by applets
2. Derived from Frame, which creates a
standard application window.
Frame
• Subclass of window and has a title bar,
menubar, borders and resizing corners.
• Creating Frame:
Frame f1=new Frame();
Frame f2=new Frame(“New Example”);
Frame: Setting Window’s Dimensions
void setSize(int width, int height)
void setSize(Dimension size)
Frame: Hiding and Showing
Syntax: void setVisible(boolean)
Example: void setVisible(true);
Frame: Setting Windows Title
Syntax:void setTitle(String title)
Example: void setTitle(New Example);
Closing the Frame Window
void windowClosing();
Write a program to display a frame with a button. The caption of the button should be “Change Color”. For every click of the button, the background color of the
frame should change randomly.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class ButtonFrame extends Frame
implements ActionListener
{
Button b;
Random rand;
Color c;
public ButtonFrame()
{
addWindowListener(new MyWindowAdapter());
b=new Button("Change Color");
b.addActionListener(this);
add(b);
rand=new Random();
}
public void paint(Graphics g)
{ }
public static void main(String args[])
{
ButtonFrame ob= new ButtonFrame();
ob.setSize(new Dimension(300, 200));
ob.setTitle("An AWT-Based Application--Buttons in
frame");
ob.setLayout(new FlowLayout());
ob.setVisible(true);
}
public void actionPerformed(ActionEvent ie)
{
//repaint();
float r=rand.nextFloat();
float gr=rand.nextFloat();
float b=rand.nextFloat();
c=new Color(r,gr,b);
setBackground(c);
}
}
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
Layout Manager
• The Layout Managers are used to arrange
controls in a particular manner.
• Layout Manager is an interface that is
implemented by all the classes of layout
managers.
Types of Layout Manager
• Flow Layout
• Grid Layout
• Border Layout
• CardLayout
Types of Layout Manager
• Flow Layout
• Grid Layout
• Border Layout
• CardLayout
Fields of FlowLayout
• 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
Flow Layout
• The FlowLayout is used to arrange the components in a
line, one after another (in a flow).
• It is the default layout of applet or panel.
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.
Flow Layout - Example
import java.awt.*;
public class MyFlowLayout
{
Frame f=new Frame();
MyFlowLayout()
{
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");
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//setting flow layout of right alignment
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
MyFlowLayout= new MyFlowLayout();
} }
Flow Layout - Example
GridLayout
• The GridLayout is used to arrange the components in
rectangular grid.
• Constructors of GridLayout 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
alongwith given horizontal and vertical gaps.
GridLayout
import java.awt.*;
public class MyGridLayout
{
Frame ff=new Frame();;
MyGridLayout(){
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");
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);
f.setLayout(new GridLayout(3,3));
//setting grid layout of 3 rows and 3 columns
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
MyGridLayout= new MyGridLayout();
} }
Abstract Window Toolkit_Event Handling_python
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 frame or window
Constructors - BorderLayout
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.
Example - BorderLayout
import java.awt.*;
import javax.swing.*;
public class Border {
Frame f;
Border()
{
f=new Frame();
Button b1=new Button("NORTH");;
Button b2=new Button("SOUTH");;
Button b3=new Button("EAST");;
Button b4=new Button("WEST");;
Button b5=new Button("CENTER");;
BorderLayout
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
new Border();
}
}
BorderLayout
Control Fundamentals
• Button
• Label
• CheckBox
• List
• MenuBar
AWT Button Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400); > javac ButtonExample.java
>java ButtonExample
f.setLayout(null);
f.setVisible(true);
}
}
AWT TextField Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class TextFieldExample extends Frame implements ActionListener{
TextField tf1,tf2,tf3;
Button b1,b2;
TextFieldExample(){
tf1=new TextField();
tf1.setBounds(50,50,150,20);
tf2=new TextField();
tf2.setBounds(50,100,150,20);
tf3=new TextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new Button("+");
b1.setBounds(50,200,50,50);
b2=new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText(); s1=“10” =10
String s2=tf2.getText(); s2=“5” =5
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args) {
new TextFieldExample();
}
}
TextArea
import java.awt.*;
import java.awt.event.*;
public class TextAreaExample extends Frame implements ActionListener{
Label l1,l2;
TextArea area;
Button b;
TextAreaExample(){
l1=new Label();
l1.setBounds(50,50,100,30);
l2=new Label();
l2.setBounds(160,50,100,30);
area=new TextArea();
area.setBounds(20,100,300,300);
b=new Button("Count Words");
b.setBounds(100,400,100,30);
b.addActionListener(this);
add(l1);add(l2);add(area);add(b);
setSize(400,450);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String text=area.getText();
String words[]=text.split("s");
l1.setText("Words: "+words.length);
l2.setText("Characters: "+text.length());
}
public static void main(String[] args) {
new TextAreaExample();
}
}
Choice Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class ChoiceExample
{ ChoiceExample(){
Frame f= new Frame();
Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
Button b=new Button("Show");
b.setBounds(200,100,50,20);
Choice c=new Choice();
c.setBounds(100,100, 75,75);
c.add("C");
c.add("C++");
c.add("Java");
c.add("PHP");
c.add("Android");
f.add(c);f.add(label); f.add(b);
Choice Example with ActionListener
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
String data = "Programming language Selected: "+
c.getItem(c.getSelectedIndex());
label.setText(data);
}
});
}
public static void main(String args[])
{
new ChoiceExample();
}
}
Java AWT List Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class ListExample
{ ListExample(){
Frame f= new Frame();
Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(500,100);
Button b=new Button("Show");
b.setBounds(200,150,80,30);
List l1=new List(4, false);
l1.setBounds(100,100, 70,70);
l1.add("C");
l1.add("C++");
l1.add("Java");
l1.add("PHP");
final List l2=new List(4, true);
l2.setBounds(100,200, 70,70);
l2.add("Turbo C++");
l2.add("Spring");
l2.add("Hibernate");
Java AWT List Example with ActionListener
l2.add("CodeIgniter");
f.add(l1); f.add(l2); f.add(label); f.add(b);
f.setSize(450,450);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex());
data += ", Framework Selected:";
for(String frame:l2.getSelectedItems()){
data += frame + " ";
}
label.setText(data);
}
});
}
public static void main(String args[])
{
new ListExample();
} }
Java AWT MenuItem and Menu
import java.awt.*;
class MenuExample
{
MenuExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu m=new Menu("Menu");
Menu sm=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");
m.add(i1);
m.add(i2);
m.add(i3);
sm.add(i4);
sm.add(i5);
Java AWT MenuItem and Menu
m.add(sm);
mb.add(m);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
MenuExample me= new MenuExample();
}
}
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.
• Unlike Frame, it doesn't have maximize and
minimize buttons.
Java AWT Dialog
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);
}
});
Java AWT Dialog
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();
}
}
FileNameFilter - Interface
import java.io.*;
• FileNameFilter interface has method
• boolean accept(File dir, String name)
• Should be implemented and every file is
tested for this method to be included in the
file list.
Java Swings
 Java Swing is a lightweight Graphical User
Interface (GUI) toolkit that includes a rich set
of widgets.
It includes package to create GUI components
for your Java applications
It is platform independent.
Java Swings
 Swing API is a set of extensible GUI
Components to ease the developer's life to
create JAVA based Front End/GUI Applications.
Swing component follows a Model-View-
Controller architecture to fulfill the following
criteria.
 A single API is to be sufficient to support
multiple look and feel.
• Unlike AWT, Java Swing provides platform-
independent and lightweight components.
• The javax.swing package provides classes for java
swing API such as JButton, JTextField, JTextArea,
JRadioButton, JCheckbox, JMenu, JColorChooser
etc.
import javax.swing.*;
Java Swings
Java Swings- Features
Light Weight
Rich Controls - Swing provides a rich set of
advanced controls like Tree, TabbedPane, slider,
colorpicker, and table controls.
Highly Customizable - Swing controls can be
customized in a very easy way as visual
apperance is independent of internal
representation.
Pluggable look-and-feel - SWING based GUI
Application look and feel can be changed at run-
time, based on available values.
Abstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_python
MVC - Model View Controller
Abstract Window Toolkit_Event Handling_python

More Related Content

PDF
swingbasics
Arjun Shanka
 
PPTX
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
utkarshabhope
 
PPT
Windows Programming with AWT
backdoor
 
PDF
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
PPT
GUI Programming In Java
yht4ever
 
PPTX
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
abhishekmathuroffici
 
PPT
java swing
Waheed Warraich
 
PPTX
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
swingbasics
Arjun Shanka
 
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
utkarshabhope
 
Windows Programming with AWT
backdoor
 
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
GUI Programming In Java
yht4ever
 
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
abhishekmathuroffici
 
java swing
Waheed Warraich
 
Creating GUI.pptx Gui graphical user interface
pikachu02434
 

Similar to Abstract Window Toolkit_Event Handling_python (20)

PPTX
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
 
PPT
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
PPT
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
PPT
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
PPT
awt.ppt java windows programming lecture
kavitamittal18
 
PPT
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
PPT
introduction to JAVA awt programmin .ppt
bgvthm
 
PPTX
AWT New-3.pptx
SarthakSrivastava70
 
PPTX
LAYOUT.pptx
aparna14patil
 
PPT
Chap1 1 4
Hemo Chella
 
PPT
Graphical User Interface in JAVA
suraj pandey
 
PPTX
AWT.pptx
MumtazAli889808
 
PPT
Chap1 1.4
Hemo Chella
 
PPTX
Java swing
ssuser3a47cb
 
PPT
Graphical User Interface (GUI) - 1
PRN USM
 
PDF
Unit-1 awt advanced java programming
Amol Gaikwad
 
PPT
28 awt
Prachi Vijh
 
PPTX
JAVA Programming: Topic -AWT(Abstract Window Tool )
Navya Francis
 
PPT
events,key,life cycle-abstract window tool kit,abstract class
Rohit Kumar
 
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
 
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
awt.ppt java windows programming lecture
kavitamittal18
 
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
introduction to JAVA awt programmin .ppt
bgvthm
 
AWT New-3.pptx
SarthakSrivastava70
 
LAYOUT.pptx
aparna14patil
 
Chap1 1 4
Hemo Chella
 
Graphical User Interface in JAVA
suraj pandey
 
AWT.pptx
MumtazAli889808
 
Chap1 1.4
Hemo Chella
 
Java swing
ssuser3a47cb
 
Graphical User Interface (GUI) - 1
PRN USM
 
Unit-1 awt advanced java programming
Amol Gaikwad
 
28 awt
Prachi Vijh
 
JAVA Programming: Topic -AWT(Abstract Window Tool )
Navya Francis
 
events,key,life cycle-abstract window tool kit,abstract class
Rohit Kumar
 
Ad

Recently uploaded (20)

PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Trends in pediatric nursing .pptx
AneetaSharma15
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Ad

Abstract Window Toolkit_Event Handling_python

  • 2. AWT Classes • The classes and interfaces of the AWT are used to develop stand alone applications and to implement the GUI controls used by Applets.
  • 3. Window Fundamentals • The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. • The two most common windows are: 1. Panel, which is used by applets 2. Derived from Frame, which creates a standard application window.
  • 4. Frame • Subclass of window and has a title bar, menubar, borders and resizing corners. • Creating Frame: Frame f1=new Frame(); Frame f2=new Frame(“New Example”);
  • 5. Frame: Setting Window’s Dimensions void setSize(int width, int height) void setSize(Dimension size)
  • 6. Frame: Hiding and Showing Syntax: void setVisible(boolean) Example: void setVisible(true); Frame: Setting Windows Title Syntax:void setTitle(String title) Example: void setTitle(New Example);
  • 7. Closing the Frame Window void windowClosing();
  • 8. Write a program to display a frame with a button. The caption of the button should be “Change Color”. For every click of the button, the background color of the frame should change randomly. import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class ButtonFrame extends Frame implements ActionListener { Button b; Random rand; Color c;
  • 9. public ButtonFrame() { addWindowListener(new MyWindowAdapter()); b=new Button("Change Color"); b.addActionListener(this); add(b); rand=new Random(); } public void paint(Graphics g) { }
  • 10. public static void main(String args[]) { ButtonFrame ob= new ButtonFrame(); ob.setSize(new Dimension(300, 200)); ob.setTitle("An AWT-Based Application--Buttons in frame"); ob.setLayout(new FlowLayout()); ob.setVisible(true); }
  • 11. public void actionPerformed(ActionEvent ie) { //repaint(); float r=rand.nextFloat(); float gr=rand.nextFloat(); float b=rand.nextFloat(); c=new Color(r,gr,b); setBackground(c); } }
  • 12. class MyWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } }
  • 13. Layout Manager • The Layout Managers are used to arrange controls in a particular manner. • Layout Manager is an interface that is implemented by all the classes of layout managers.
  • 14. Types of Layout Manager • Flow Layout • Grid Layout • Border Layout • CardLayout
  • 15. Types of Layout Manager • Flow Layout • Grid Layout • Border Layout • CardLayout
  • 16. Fields of FlowLayout • 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
  • 17. Flow Layout • The FlowLayout is used to arrange the components in a line, one after another (in a flow). • It is the default layout of applet or panel. 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.
  • 18. Flow Layout - Example import java.awt.*; public class MyFlowLayout { Frame f=new Frame(); MyFlowLayout() { 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"); f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5); f.setLayout(new FlowLayout(FlowLayout.RIGHT)); //setting flow layout of right alignment f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { MyFlowLayout= new MyFlowLayout(); } }
  • 19. Flow Layout - Example
  • 20. GridLayout • The GridLayout is used to arrange the components in rectangular grid. • Constructors of GridLayout 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 alongwith given horizontal and vertical gaps.
  • 21. GridLayout import java.awt.*; public class MyGridLayout { Frame ff=new Frame();; MyGridLayout(){ 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"); 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); f.setLayout(new GridLayout(3,3)); //setting grid layout of 3 rows and 3 columns f.setSize(300,300); f.setVisible(true); } public static void main(String[] args) { MyGridLayout= new MyGridLayout(); } }
  • 23. 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 frame or window
  • 24. Constructors - BorderLayout 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.
  • 25. Example - BorderLayout import java.awt.*; import javax.swing.*; public class Border { Frame f; Border() { f=new Frame(); Button b1=new Button("NORTH");; Button b2=new Button("SOUTH");; Button b3=new Button("EAST");; Button b4=new Button("WEST");; Button b5=new Button("CENTER");;
  • 28. Control Fundamentals • Button • Label • CheckBox • List • MenuBar
  • 29. AWT Button Example with ActionListener import java.awt.*; import java.awt.event.*; public class ButtonExample { public static void main(String[] args) { Frame f=new Frame("Button Example"); TextField tf=new TextField(); tf.setBounds(50,50, 150,20); Button b=new Button("Click Here"); b.setBounds(50,100,60,30); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ tf.setText("Welcome to Javatpoint."); } }); f.add(b);f.add(tf); f.setSize(400,400); > javac ButtonExample.java >java ButtonExample f.setLayout(null); f.setVisible(true); } }
  • 30. AWT TextField Example with ActionListener import java.awt.*; import java.awt.event.*; public class TextFieldExample extends Frame implements ActionListener{ TextField tf1,tf2,tf3; Button b1,b2; TextFieldExample(){ tf1=new TextField(); tf1.setBounds(50,50,150,20); tf2=new TextField(); tf2.setBounds(50,100,150,20); tf3=new TextField(); tf3.setBounds(50,150,150,20); tf3.setEditable(false); b1=new Button("+"); b1.setBounds(50,200,50,50); b2=new Button("-"); b2.setBounds(120,200,50,50); b1.addActionListener(this); b2.addActionListener(this);
  • 31. add(tf1);add(tf2);add(tf3);add(b1);add(b2); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e) { String s1=tf1.getText(); s1=“10” =10 String s2=tf2.getText(); s2=“5” =5 int a=Integer.parseInt(s1); int b=Integer.parseInt(s2); int c=0; if(e.getSource()==b1){ c=a+b; }else if(e.getSource()==b2){ c=a-b; } String result=String.valueOf(c); tf3.setText(result); } public static void main(String[] args) { new TextFieldExample(); } }
  • 32. TextArea import java.awt.*; import java.awt.event.*; public class TextAreaExample extends Frame implements ActionListener{ Label l1,l2; TextArea area; Button b; TextAreaExample(){ l1=new Label(); l1.setBounds(50,50,100,30); l2=new Label(); l2.setBounds(160,50,100,30); area=new TextArea(); area.setBounds(20,100,300,300); b=new Button("Count Words"); b.setBounds(100,400,100,30); b.addActionListener(this); add(l1);add(l2);add(area);add(b); setSize(400,450); setLayout(null); setVisible(true); }
  • 33. public void actionPerformed(ActionEvent e){ String text=area.getText(); String words[]=text.split("s"); l1.setText("Words: "+words.length); l2.setText("Characters: "+text.length()); } public static void main(String[] args) { new TextAreaExample(); } }
  • 34. Choice Example with ActionListener import java.awt.*; import java.awt.event.*; public class ChoiceExample { ChoiceExample(){ Frame f= new Frame(); Label label = new Label(); label.setAlignment(Label.CENTER); label.setSize(400,100); Button b=new Button("Show"); b.setBounds(200,100,50,20); Choice c=new Choice(); c.setBounds(100,100, 75,75); c.add("C"); c.add("C++"); c.add("Java"); c.add("PHP"); c.add("Android"); f.add(c);f.add(label); f.add(b);
  • 35. Choice Example with ActionListener f.setSize(400,400); f.setLayout(null); f.setVisible(true); b.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex()); label.setText(data); } }); } public static void main(String args[]) { new ChoiceExample(); } }
  • 36. Java AWT List Example with ActionListener import java.awt.*; import java.awt.event.*; public class ListExample { ListExample(){ Frame f= new Frame(); Label label = new Label(); label.setAlignment(Label.CENTER); label.setSize(500,100); Button b=new Button("Show"); b.setBounds(200,150,80,30); List l1=new List(4, false); l1.setBounds(100,100, 70,70); l1.add("C"); l1.add("C++"); l1.add("Java"); l1.add("PHP"); final List l2=new List(4, true); l2.setBounds(100,200, 70,70); l2.add("Turbo C++"); l2.add("Spring"); l2.add("Hibernate");
  • 37. Java AWT List Example with ActionListener l2.add("CodeIgniter"); f.add(l1); f.add(l2); f.add(label); f.add(b); f.setSize(450,450); f.setLayout(null); f.setVisible(true); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex()); data += ", Framework Selected:"; for(String frame:l2.getSelectedItems()){ data += frame + " "; } label.setText(data); } }); } public static void main(String args[]) { new ListExample(); } }
  • 38. Java AWT MenuItem and Menu import java.awt.*; class MenuExample { MenuExample(){ Frame f= new Frame("Menu and MenuItem Example"); MenuBar mb=new MenuBar(); Menu m=new Menu("Menu"); Menu sm=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"); m.add(i1); m.add(i2); m.add(i3); sm.add(i4); sm.add(i5);
  • 39. Java AWT MenuItem and Menu m.add(sm); mb.add(m); f.setMenuBar(mb); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public static void main(String args[]) { MenuExample me= new MenuExample(); } }
  • 40. 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. • Unlike Frame, it doesn't have maximize and minimize buttons.
  • 41. Java AWT Dialog 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); } });
  • 42. Java AWT Dialog 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(); } }
  • 43. FileNameFilter - Interface import java.io.*; • FileNameFilter interface has method • boolean accept(File dir, String name) • Should be implemented and every file is tested for this method to be included in the file list.
  • 44. Java Swings  Java Swing is a lightweight Graphical User Interface (GUI) toolkit that includes a rich set of widgets. It includes package to create GUI components for your Java applications It is platform independent.
  • 45. Java Swings  Swing API is a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications. Swing component follows a Model-View- Controller architecture to fulfill the following criteria.  A single API is to be sufficient to support multiple look and feel.
  • 46. • Unlike AWT, Java Swing provides platform- independent and lightweight components. • The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc. import javax.swing.*; Java Swings
  • 47. Java Swings- Features Light Weight Rich Controls - Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, and table controls. Highly Customizable - Swing controls can be customized in a very easy way as visual apperance is independent of internal representation. Pluggable look-and-feel - SWING based GUI Application look and feel can be changed at run- time, based on available values.
  • 50. MVC - Model View Controller