SlideShare une entreprise Scribd logo
1  sur  39
Télécharger pour lire hors ligne
Programming in JAVA
Topic: GUI Basics (AWT)
By
Ravi Kant Sahu
Asst. Professor

Lovely Professional University, Punjab
Introduction
• AWT is a java API that is used to develop GUI applications in Java.
• API is a collection of classes and interfaces that provide the basic
infrastructure or core functionalities for developing specific type of
application.
•
1)
2)
3)

There are three types of GUI classes:
Container classes
Component classes
Helper classes

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Component Classes
• An instance of Component can be displayed on the screen.
• Component is the root class of all the user-interface classes including
container classes, and JComponent is the root class of all the
lightweight Swing components.
• Button, Label, TextField etc.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Container Classes
• An instance of Container can hold instances of Component.
• Container classes are GUI components that are used to contain other
GUI components.
• Window, Panel, Applet, Frame, and Dialog are the container classes
for AWT components.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Helper Classes
• The helper classes, such as Graphics, Color, Font, FontMetrics,
Dimension, and LayoutManager, are not subclasses of Component.
•

They are used to describe the properties of GUI components, such as
graphics context, colors, fonts, and dimension etc.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
AWT classes
Class

Description

AWTEvent

Encapsulates AWT events

Button

Creates a push button control

Canvas

A blank, semantics-free window

Checkbox

Creates a check box control

CheckboxGroup

Creates a group of check box controls

Color

Manages colors in a portable, platform-independent fashion

Font

Encapsulates a type font

Frame
Graphics

Creates a standard window that has a title bar, resize corners,
and a menu bar
Encapsulates the graphics context. This context is used by the
various output methods to display output in a window.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
AWT classes
Class

Description

TextField

Creates a single-line edit control

TextArea

Creates a multiline edit control

Window

Creates a window with no frame, no menu bar, and no title

Scrollbar

Creates a scroll bar control

MenuItem

Creates a menu item

Menu

Creates a pull-down menu

MenuBar

Creates a menu bar

MenuShortcut
ScrollPane

Encapsulates a keyboard shortcut for a menu item

A container that provides horizontal and/or vertical scroll bars
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
for another component
Component Class Hierarchy of AWT
Component
Container
(Non-Container)
Window
Frame

Button

Label

Panel
Applet

Text Field

Check-Box

Text Area etc.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• Component is an abstract class that describes the basic
functionality supported by all AWT components.
• Container is an abstract sub-class of Component that adds the
functionality of containership to a component.
• A Container component can contain another container or noncontainer components.
• Window and Panel are two non-abstract sub-classes of
Container.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Component Cont…
• Panel represents a rectangular region that does not have a
border and title bar.
• Window is a Panel with border and title bar.
• Window can independently exist whereas panel can’t.
• Frame is a sub-class of Window .
• Button, Label, TextField, CheckBox etc. are non-container
components.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Component
• Component is an abstract class that encapsulates all of the
attributes of a visual component.
• All user interface elements that are displayed on the screen and
that interact with the user are subclasses of Component.
• It defines over a hundred public methods that are responsible for
managing events, such as mouse and keyboard input, positioning
and sizing the window, and repainting.
• A Component object is responsible for remembering the current
foreground and background colors and the currently selected text
font.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Container
• Container class is a subclass of Component.
• It has additional methods that allow other Component objects to be
nested within it.
• Other Container objects can be stored inside of a Container (since
they are themselves instances of Component).
• This makes for a multileveled containment system.
• A container is responsible for laying out (that is, positioning) any
components that it contains. It does this through the use of various
layout managers.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Panel
• Panel is a window that does not contain a title bar, menu bar, or
border.
• The screen output of an applet is drawn on the surface of a Panel
object. but it is not visible when applet is run inside a browser.
• When we run an applet using an applet viewer, the applet viewer
provides the title and border.
• Other components can be added to a Panel object by its add( )
method (inherited from Container).
• We can position and resize the components of a panel manually
using the setLocation( ), setSize( ), setPreferredSize( ), or
setBounds( ) methods defined by Component.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Window
• Window class creates a top-level window.
• A top-level window is not contained within any other object; it
sits directly on the desktop.
• Generally, we won’t create Window objects directly. Instead, we
use a subclass of Window called Frame.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Frame
• It is a subclass of Window and has a title bar, menu bar, borders,
and resizing corners.
• Frame encapsulates what is commonly thought of as a
“window.”

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Frame
Constructor:
public Frame ()
public Frame (String Title)
Methods:
public String getTitle ()
public void setTitle (String Title)
public void setVisibile(boolean Visibility)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Commonly Used Methods of Component Class
• setBackground(): used to change the background color of a
component.
public void setBackground ( Color c)
Note: color is a helper class.
• setForeground(): used to change the foreground color of a
component.
public void setForeground ( Font obj)
• Font is represented by java.awt.Font.
public Font (String FontName, int Style, int Size)
e.g. : Font f = (“Times New Roman”, Font.BOLD, 20);

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• setBounds(): used to specify size and position of component in
a container.
public void setBounds (int left, int top, int width, int height)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Commonly used methods of Container Class
• add(): used to add components to a container.
public void add (Component c)
• setSize(): used to specify the size of a container.
public void setSize(int width, int height)
• setLayout(): used to specify the layout manager for a container.
public void setLayout (LayoutManager mgr)
• setVisible(boolean visibility): used to set the visibility of
container.
public void setVisible (boolean visibility)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Label
• Used to describe other components or to display information on
a container.
Constructors:
public Label()
public Label (String s)
Methods:
• public String getText(): used to display the text on a Label.
• public void setText(): used to change the text.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Button
• Buttons are used to initiate action.
Constructor:
public Button ()
public Button (String Name)
Methods:
public String getLabel()
public void setLabel(String Name)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
TextField
• Used to receive input and display the output/result.
Constructor:
public TextField ()
public TextField (String Text)
public TextField (int No_of Chars)

Methods:
public String getText ()
public void setText (String Text)
public void setEchoChar ( char x)
public void setEditable (boolean editability)
public boolean isEditable()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
TextArea
• Used to display multiple lines of output/result.
Constructor:
public TextArea()
public TextArea (String Text)
public TextArea (int numLines, int numChars)
public TextArea (String str, int numLines, int numChars, int sBars)

sBars Constants: (SCROLLBARS_NONE, SCROLLBARS_BOTH,
SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_VERTICAL_ONLY)

Methods:
Supports all the methods of TextArea.
void append(String str)
void insert(String str, int index)
void replaceRange(String str, int startIndex, int endIndex)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Checkbox and RadioButtons
• Checkbox and CheckboxGroup classes are used to create
RadioButtons.
public Checkbox (String Text)
public Checkbox (String Text, Boolean State)
public Checkbox (String Text, Boolean State,
CheckboxGroup cbg )
public Checkbox (String Text, CheckboxGroup cbg, Boolean
State, )

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods
•
•
•
•

boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
CheckboxGroup
• Used to create a set of mutually exclusive check boxes in which
one and only one check box in the group can be checked at a
time.
Constructor:
public CheckboxGroup()
Methods:
Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Menu Bars and Menus
•

Menu is implemented in AWT by the following classes:
MenuBar, Menu, and MenuItem.

•

A menu bar displays a list of top-level menu choices. Each choice is
associated with a drop-down menu.

•

A menu bar contains one or more Menu objects. Each Menu object contains
a list of MenuItem objects.

•

Each MenuItem object represents something that can be selected by the user.

•

Since Menu is a subclass of MenuItem, a hierarchy of nested submenus can
be created.

•

Checkable menu items are menu options of type CheckboxMenuItem and
will have a check mark next to them when they are selected.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating Menus
• To create a menu bar, first create an instance of MenuBar. This class
only defines the default constructor.
• Next, create instances of Menu that will define the selections
displayed on the bar. Following are the constructors for Menu:
Menu( ) throws HeadlessException
Menu(String optionName) throws HeadlessException
• Individual menu items are of type MenuItem. It defines these
constructors:
MenuItem( ) throws HeadlessException
MenuItem(String itemName) throws HeadlessException
MenuItem(String itemName, MenuShortcut keyAccel) throws
HeadlessException
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• We can disable or enable a menu item by using the setEnabled( )
method.
void setEnabled (boolean enabledFlag)
• If the argument enabledFlag is true, the menu item is enabled. If false,
the menu item is disabled.
• We can determine an item’s status by calling isEnabled( ).
boolean isEnabled( )
• isEnabled( ) returns true if the menu item on which it is called is
enabled. Otherwise, it returns false.
• We can change the name of a menu item by calling setLabel( ). We
can retrieve the current name by using getLabel( ).
void setLabel(String newName)
String getLabel( )
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
CheckboxMenuItem
• We can create a checkable menu item by using a subclass of
MenuItem called CheckboxMenuItem.
CheckboxMenuItem( ) throws HeadlessException
CheckboxMenuItem(String itemName) throws HeadlessException
CheckboxMenuItem(String itemName, boolean on) throws
HeadlessException
• We can obtain the status of a checkable item by calling getState( ).
You can set it to a known state by using setState( ).
boolean getState( )
void setState(boolean checked)
Note: HeadlessException is Thrown when code that is dependent on a
keyboard, display, or mouse is called in an environment that does not
support a keyboard, display, or mouse.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Choice
• The Choice class presents a pop-up menu of choices.
• The current choice is displayed as the title of the menu.
Choice c = new Choice();
// Only default constructor available
c.add(“B. Tech.”); c.add(“BCA”); c.add(“B. Arch.”);

Methods:
String getItem(int index)
int getItemCount()
String getSelectedItem()
int getSelectedIndex()

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
List
• The List class provides a compact, multiple-choice, scrolling
selection list.

Constructors:
List( )
List(int numRows)
List(int numRows, boolean multipleSelect)

Methods:
void add(String name)
void add(String name, int index)
String getSelectedItem()
int getSelectedIndex()

String[] getSelectedItems()
int[] getSelectedIndexes()

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Layout Manager classes
LayoutManager class

Description

BorderLayout

The border layout manager. Border layouts use five
components: North, South, East, West, and Center

CardLayout

The card layout manager. Card layouts emulate index
cards. Only the one on top is showing.

GridLayout

The grid layout manager. Grid layout displays
components in a two-dimensional grid.

FlowLayout

The flow layout manager. Flow layout positions
components left to right, top to bottom.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
FlowLayout
•
•

Arranges the components in left to right, top to bottom fashion.
It is the default layout manager for Frame.
public FlowLayout ()
public FlowLayout (int alignment)
public FlowLayout (int alignment, int H_Gap, int V_Gap)

•

Alignments are specified as:
FlowLayout.LEFT
FlowLayout.RIGHT
FlowLayout.CENTER

•

By default 5 pixels is used as horizontal and vertical gap between
components.
FlowLayout fl = new FlowLayout (FlowLayout.CENTER, 30, 20);
frm.setLayout(fl);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
GridLayout
• It divides a container into a grid of specified rows and columns.
Each to display one component.
• Size of component is changed according to the size of the cell.
public GridLayout (int row, int column)
public GridLayout (int row, int column, int H_Gap, int V_Gap)
GridLayout gl = new GridLayout (3, 4);
frm.setLayout (gl);
GridLayout gl1 = new GridLayout (3, 4, 20, 30);
frm.setLayout (gl2);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
BorderLayout
• It divides the container into 5 regions ( NORTH, SOUTH,
EAST, WEST and CENTER).
• It is the default layout manager for Window.
public BorderLayout()
• To add a component at a specific region, following method is
used:
public void add ( Component c, int Region)
Example:
Button btn = new Button(“OK”);
frm.add( btn, BorderLayout.EAST);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
CardLayout
• It is used to arrange containers in the form of deck of cards.
Methods:
first() / last()/ next()/ previous(): is used to make the first/ last/
next/ previous card visible.
show(): is used to make a specified card visible.
public void show ( Container deck, String CardName)
• To give a name to the container while it is added to the deck:
public void add ( Container card, String CardName)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Steps to Create a Frame-based Interface
1)
2)
3)
4)

Create a Frame object.
Create Component Objects.
Add the Component objects to Frame.
Set the size of the Frame and make it visible.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
My First Application
import java.awt.*;
class MyFirstApp
{
static TextField t1,t2,t3; static Button b1;
public static void main(String arr[])
{
Frame f = new Frame ("My Calculator");
Label l1 = new Label (“1st Number"); Label l2 = new Label (“2nd Number");
Label l3 = new Label ("Result");
t1 = new TextField(20); t2 = new TextField(20);
t3 = new TextField(20); t3.setEditable(false);
b1 = new Button("Add");
f.setLayout(new FlowLayout());
f.add(l1); f.add(t1); f.add(l2); f.add(t2); f.add(l3); f.add(t3); f.add(b1);
f.setSize(200,250); f.setVisible(true);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Contenu connexe

Tendances

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examplesbindur87
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in javaTharuniDiddekunta
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1İbrahim Kürce
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi Kant Sahu
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsAnil Kumar
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Javaparag
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 

Tendances (20)

Exception handling
Exception handlingException handling
Exception handling
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Java interface
Java interfaceJava interface
Java interface
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 

En vedette (20)

Basic IO
Basic IOBasic IO
Basic IO
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Java awt
Java awtJava awt
Java awt
 
Java AWT
Java AWTJava AWT
Java AWT
 
Event handling
Event handlingEvent handling
Event handling
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Awt
AwtAwt
Awt
 
Questions for Class I & II
Questions for Class I & IIQuestions for Class I & II
Questions for Class I & II
 
Mental models
Mental modelsMental models
Mental models
 
Generics
GenericsGenerics
Generics
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
List classes
List classesList classes
List classes
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)
 
Networking
NetworkingNetworking
Networking
 
Java Unicode with Live GUI Examples
Java Unicode with Live GUI ExamplesJava Unicode with Live GUI Examples
Java Unicode with Live GUI Examples
 
Java AWT
Java AWTJava AWT
Java AWT
 
12 gui concepts 1
12 gui concepts 112 gui concepts 1
12 gui concepts 1
 

Similaire à Gui programming (awt)

Similaire à Gui programming (awt) (20)

tL19 awt
tL19 awttL19 awt
tL19 awt
 
Java Applet
Java AppletJava Applet
Java Applet
 
AWT.pptx
AWT.pptxAWT.pptx
AWT.pptx
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
The smartpath information systems java
The smartpath information systems javaThe smartpath information systems java
The smartpath information systems java
 
java.pptx
java.pptxjava.pptx
java.pptx
 
Gui
GuiGui
Gui
 
Introdu.awt
Introdu.awtIntrodu.awt
Introdu.awt
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Eclipse - GUI Palette
Eclipse - GUI Palette Eclipse - GUI Palette
Eclipse - GUI Palette
 
Iterator pattern
Iterator patternIterator pattern
Iterator pattern
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
 
Write code that writes code!
Write code that writes code!Write code that writes code!
Write code that writes code!
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 

Plus de Ravi_Kant_Sahu

Plus de Ravi_Kant_Sahu (15)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Event handling
Event handlingEvent handling
Event handling
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Packages
PackagesPackages
Packages
 
Array
ArrayArray
Array
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Jdbc
JdbcJdbc
Jdbc
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 

Dernier

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Dernier (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Gui programming (awt)

  • 1. Programming in JAVA Topic: GUI Basics (AWT) By Ravi Kant Sahu Asst. Professor Lovely Professional University, Punjab
  • 2. Introduction • AWT is a java API that is used to develop GUI applications in Java. • API is a collection of classes and interfaces that provide the basic infrastructure or core functionalities for developing specific type of application. • 1) 2) 3) There are three types of GUI classes: Container classes Component classes Helper classes Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Component Classes • An instance of Component can be displayed on the screen. • Component is the root class of all the user-interface classes including container classes, and JComponent is the root class of all the lightweight Swing components. • Button, Label, TextField etc. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Container Classes • An instance of Container can hold instances of Component. • Container classes are GUI components that are used to contain other GUI components. • Window, Panel, Applet, Frame, and Dialog are the container classes for AWT components. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. Helper Classes • The helper classes, such as Graphics, Color, Font, FontMetrics, Dimension, and LayoutManager, are not subclasses of Component. • They are used to describe the properties of GUI components, such as graphics context, colors, fonts, and dimension etc. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. AWT classes Class Description AWTEvent Encapsulates AWT events Button Creates a push button control Canvas A blank, semantics-free window Checkbox Creates a check box control CheckboxGroup Creates a group of check box controls Color Manages colors in a portable, platform-independent fashion Font Encapsulates a type font Frame Graphics Creates a standard window that has a title bar, resize corners, and a menu bar Encapsulates the graphics context. This context is used by the various output methods to display output in a window. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. AWT classes Class Description TextField Creates a single-line edit control TextArea Creates a multiline edit control Window Creates a window with no frame, no menu bar, and no title Scrollbar Creates a scroll bar control MenuItem Creates a menu item Menu Creates a pull-down menu MenuBar Creates a menu bar MenuShortcut ScrollPane Encapsulates a keyboard shortcut for a menu item A container that provides horizontal and/or vertical scroll bars Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India) for another component
  • 8. Component Class Hierarchy of AWT Component Container (Non-Container) Window Frame Button Label Panel Applet Text Field Check-Box Text Area etc. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. • Component is an abstract class that describes the basic functionality supported by all AWT components. • Container is an abstract sub-class of Component that adds the functionality of containership to a component. • A Container component can contain another container or noncontainer components. • Window and Panel are two non-abstract sub-classes of Container. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. Component Cont… • Panel represents a rectangular region that does not have a border and title bar. • Window is a Panel with border and title bar. • Window can independently exist whereas panel can’t. • Frame is a sub-class of Window . • Button, Label, TextField, CheckBox etc. are non-container components. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. Component • Component is an abstract class that encapsulates all of the attributes of a visual component. • All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. • It defines over a hundred public methods that are responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting. • A Component object is responsible for remembering the current foreground and background colors and the currently selected text font. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Container • Container class is a subclass of Component. • It has additional methods that allow other Component objects to be nested within it. • Other Container objects can be stored inside of a Container (since they are themselves instances of Component). • This makes for a multileveled containment system. • A container is responsible for laying out (that is, positioning) any components that it contains. It does this through the use of various layout managers. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. Panel • Panel is a window that does not contain a title bar, menu bar, or border. • The screen output of an applet is drawn on the surface of a Panel object. but it is not visible when applet is run inside a browser. • When we run an applet using an applet viewer, the applet viewer provides the title and border. • Other components can be added to a Panel object by its add( ) method (inherited from Container). • We can position and resize the components of a panel manually using the setLocation( ), setSize( ), setPreferredSize( ), or setBounds( ) methods defined by Component. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Window • Window class creates a top-level window. • A top-level window is not contained within any other object; it sits directly on the desktop. • Generally, we won’t create Window objects directly. Instead, we use a subclass of Window called Frame. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Frame • It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. • Frame encapsulates what is commonly thought of as a “window.” Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. Frame Constructor: public Frame () public Frame (String Title) Methods: public String getTitle () public void setTitle (String Title) public void setVisibile(boolean Visibility) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Commonly Used Methods of Component Class • setBackground(): used to change the background color of a component. public void setBackground ( Color c) Note: color is a helper class. • setForeground(): used to change the foreground color of a component. public void setForeground ( Font obj) • Font is represented by java.awt.Font. public Font (String FontName, int Style, int Size) e.g. : Font f = (“Times New Roman”, Font.BOLD, 20); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. • setBounds(): used to specify size and position of component in a container. public void setBounds (int left, int top, int width, int height) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Commonly used methods of Container Class • add(): used to add components to a container. public void add (Component c) • setSize(): used to specify the size of a container. public void setSize(int width, int height) • setLayout(): used to specify the layout manager for a container. public void setLayout (LayoutManager mgr) • setVisible(boolean visibility): used to set the visibility of container. public void setVisible (boolean visibility) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. Label • Used to describe other components or to display information on a container. Constructors: public Label() public Label (String s) Methods: • public String getText(): used to display the text on a Label. • public void setText(): used to change the text. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. Button • Buttons are used to initiate action. Constructor: public Button () public Button (String Name) Methods: public String getLabel() public void setLabel(String Name) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. TextField • Used to receive input and display the output/result. Constructor: public TextField () public TextField (String Text) public TextField (int No_of Chars) Methods: public String getText () public void setText (String Text) public void setEchoChar ( char x) public void setEditable (boolean editability) public boolean isEditable() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. TextArea • Used to display multiple lines of output/result. Constructor: public TextArea() public TextArea (String Text) public TextArea (int numLines, int numChars) public TextArea (String str, int numLines, int numChars, int sBars) sBars Constants: (SCROLLBARS_NONE, SCROLLBARS_BOTH, SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_VERTICAL_ONLY) Methods: Supports all the methods of TextArea. void append(String str) void insert(String str, int index) void replaceRange(String str, int startIndex, int endIndex) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. Checkbox and RadioButtons • Checkbox and CheckboxGroup classes are used to create RadioButtons. public Checkbox (String Text) public Checkbox (String Text, Boolean State) public Checkbox (String Text, Boolean State, CheckboxGroup cbg ) public Checkbox (String Text, CheckboxGroup cbg, Boolean State, ) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 25. Methods • • • • boolean getState( ) void setState(boolean on) String getLabel( ) void setLabel(String str) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 26. CheckboxGroup • Used to create a set of mutually exclusive check boxes in which one and only one check box in the group can be checked at a time. Constructor: public CheckboxGroup() Methods: Checkbox getSelectedCheckbox( ) void setSelectedCheckbox(Checkbox which) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 27. Menu Bars and Menus • Menu is implemented in AWT by the following classes: MenuBar, Menu, and MenuItem. • A menu bar displays a list of top-level menu choices. Each choice is associated with a drop-down menu. • A menu bar contains one or more Menu objects. Each Menu object contains a list of MenuItem objects. • Each MenuItem object represents something that can be selected by the user. • Since Menu is a subclass of MenuItem, a hierarchy of nested submenus can be created. • Checkable menu items are menu options of type CheckboxMenuItem and will have a check mark next to them when they are selected. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 28. Creating Menus • To create a menu bar, first create an instance of MenuBar. This class only defines the default constructor. • Next, create instances of Menu that will define the selections displayed on the bar. Following are the constructors for Menu: Menu( ) throws HeadlessException Menu(String optionName) throws HeadlessException • Individual menu items are of type MenuItem. It defines these constructors: MenuItem( ) throws HeadlessException MenuItem(String itemName) throws HeadlessException MenuItem(String itemName, MenuShortcut keyAccel) throws HeadlessException Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 29. • We can disable or enable a menu item by using the setEnabled( ) method. void setEnabled (boolean enabledFlag) • If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is disabled. • We can determine an item’s status by calling isEnabled( ). boolean isEnabled( ) • isEnabled( ) returns true if the menu item on which it is called is enabled. Otherwise, it returns false. • We can change the name of a menu item by calling setLabel( ). We can retrieve the current name by using getLabel( ). void setLabel(String newName) String getLabel( ) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 30. CheckboxMenuItem • We can create a checkable menu item by using a subclass of MenuItem called CheckboxMenuItem. CheckboxMenuItem( ) throws HeadlessException CheckboxMenuItem(String itemName) throws HeadlessException CheckboxMenuItem(String itemName, boolean on) throws HeadlessException • We can obtain the status of a checkable item by calling getState( ). You can set it to a known state by using setState( ). boolean getState( ) void setState(boolean checked) Note: HeadlessException is Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 31. Choice • The Choice class presents a pop-up menu of choices. • The current choice is displayed as the title of the menu. Choice c = new Choice(); // Only default constructor available c.add(“B. Tech.”); c.add(“BCA”); c.add(“B. Arch.”); Methods: String getItem(int index) int getItemCount() String getSelectedItem() int getSelectedIndex() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 32. List • The List class provides a compact, multiple-choice, scrolling selection list. Constructors: List( ) List(int numRows) List(int numRows, boolean multipleSelect) Methods: void add(String name) void add(String name, int index) String getSelectedItem() int getSelectedIndex() String[] getSelectedItems() int[] getSelectedIndexes() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 33. Layout Manager classes LayoutManager class Description BorderLayout The border layout manager. Border layouts use five components: North, South, East, West, and Center CardLayout The card layout manager. Card layouts emulate index cards. Only the one on top is showing. GridLayout The grid layout manager. Grid layout displays components in a two-dimensional grid. FlowLayout The flow layout manager. Flow layout positions components left to right, top to bottom. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 34. FlowLayout • • Arranges the components in left to right, top to bottom fashion. It is the default layout manager for Frame. public FlowLayout () public FlowLayout (int alignment) public FlowLayout (int alignment, int H_Gap, int V_Gap) • Alignments are specified as: FlowLayout.LEFT FlowLayout.RIGHT FlowLayout.CENTER • By default 5 pixels is used as horizontal and vertical gap between components. FlowLayout fl = new FlowLayout (FlowLayout.CENTER, 30, 20); frm.setLayout(fl); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 35. GridLayout • It divides a container into a grid of specified rows and columns. Each to display one component. • Size of component is changed according to the size of the cell. public GridLayout (int row, int column) public GridLayout (int row, int column, int H_Gap, int V_Gap) GridLayout gl = new GridLayout (3, 4); frm.setLayout (gl); GridLayout gl1 = new GridLayout (3, 4, 20, 30); frm.setLayout (gl2); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 36. BorderLayout • It divides the container into 5 regions ( NORTH, SOUTH, EAST, WEST and CENTER). • It is the default layout manager for Window. public BorderLayout() • To add a component at a specific region, following method is used: public void add ( Component c, int Region) Example: Button btn = new Button(“OK”); frm.add( btn, BorderLayout.EAST); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 37. CardLayout • It is used to arrange containers in the form of deck of cards. Methods: first() / last()/ next()/ previous(): is used to make the first/ last/ next/ previous card visible. show(): is used to make a specified card visible. public void show ( Container deck, String CardName) • To give a name to the container while it is added to the deck: public void add ( Container card, String CardName) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 38. Steps to Create a Frame-based Interface 1) 2) 3) 4) Create a Frame object. Create Component Objects. Add the Component objects to Frame. Set the size of the Frame and make it visible. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 39. My First Application import java.awt.*; class MyFirstApp { static TextField t1,t2,t3; static Button b1; public static void main(String arr[]) { Frame f = new Frame ("My Calculator"); Label l1 = new Label (“1st Number"); Label l2 = new Label (“2nd Number"); Label l3 = new Label ("Result"); t1 = new TextField(20); t2 = new TextField(20); t3 = new TextField(20); t3.setEditable(false); b1 = new Button("Add"); f.setLayout(new FlowLayout()); f.add(l1); f.add(t1); f.add(l2); f.add(t2); f.add(l3); f.add(t3); f.add(b1); f.setSize(200,250); f.setVisible(true); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)