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

PPT
introduction to JAVA awt programmin .ppt
PPT
Unit 1- awt(Abstract Window Toolkit) .ppt
PPT
1.Abstract windowing toolkit.ppt of AJP sub
PPT
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
PPT
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
PPT
awt.ppt java windows programming lecture
PPTX
Java swing
PPT
Chap1 1 4
introduction to JAVA awt programmin .ppt
Unit 1- awt(Abstract Window Toolkit) .ppt
1.Abstract windowing toolkit.ppt of AJP sub
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
awt.ppt java windows programming lecture
Java swing
Chap1 1 4

Similar to Abstract Window Toolkit_Event Handling_python (20)

PPT
Chap1 1.4
PPTX
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
PPT
13457272.ppt
PPTX
Creating GUI.pptx Gui graphical user interface
PDF
Abstract Window Toolkit
PPTX
tL19 awt
PDF
Unit-1 awt advanced java programming
PPT
Md10 building java gu is
PPTX
AWT New-3.pptx
PPTX
Awt, Swing, Layout managers
PPTX
LAYOUT.pptx
PPT
Graphical User Interface (GUI) - 1
PPTX
U5 JAVA.pptx
PPTX
PPTX
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
PDF
Getting started with GUI programming in Java_1
PPSX
Dr. Rajeshree Khande :Introduction to Java AWT
PDF
swingbasics
PDF
GUI.pdf
PPT
Unit4 AWT, Swings & Layouts power point presentation
Chap1 1.4
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
13457272.ppt
Creating GUI.pptx Gui graphical user interface
Abstract Window Toolkit
tL19 awt
Unit-1 awt advanced java programming
Md10 building java gu is
AWT New-3.pptx
Awt, Swing, Layout managers
LAYOUT.pptx
Graphical User Interface (GUI) - 1
U5 JAVA.pptx
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
Getting started with GUI programming in Java_1
Dr. Rajeshree Khande :Introduction to Java AWT
swingbasics
GUI.pdf
Unit4 AWT, Swings & Layouts power point presentation
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Structure & Organelles in detailed.
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Complications of Minimal Access Surgery at WLH
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Basic Mud Logging Guide for educational purpose
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Structure & Organelles in detailed.
Renaissance Architecture: A Journey from Faith to Humanism
Complications of Minimal Access Surgery at WLH
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Basic Mud Logging Guide for educational purpose
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Module 4: Burden of Disease Tutorial Slides S2 2025
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
O5-L3 Freight Transport Ops (International) V1.pdf
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
Supply Chain Operations Speaking Notes -ICLT Program
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
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