SlideShare una empresa de Scribd logo
1 de 39
Programming in JAVA
Topic: GUI Basics (AWT)
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.
• There are three types of GUI classes:
1) Container classes
2) Component classes
3) Helper classes
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.
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.
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.
AWT Classes
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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 Creates a standard window that has a title bar, resize corners,
and a menu bar
Graphics Encapsulates the graphics context. This context is used by the
various output methods to display output in a window.
AWT classes
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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 Encapsulates a keyboard shortcut for a menu item
ScrollPane A container that provides horizontal and/or vertical scroll bars
for another component
Component Class Hierarchy of AWT
Component
Container
(Non-Container)
Window Panel
Frame Applet
Button Label Text Field Check-Box Text Area etc.
• Component is an abstract class that describes the basic
functionality supported by all AWT components.
• Container is a sub-class of Component that adds the
functionality of containership to a component.
• A Container component can contain another container or non-
container components.
• Window and Panel are two non-abstract sub-classes of
Container.
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.
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.
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);
• setBounds(): used to specify size and position of component in
a container.
public void setBounds (int left, int top, int width, int height)
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.
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)
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.
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.
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.”
Frame
Constructor:
public Frame ()
public Frame (String Title)
Methods:
public String getTitle ()
public void setTitle (String Title)
public void setVisibile(boolean Visibility)
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.
Button
• Buttons are used to initiate action.
Constructor:
public Button ()
public Button (String Name)
Methods:
public String getLabel()
public void setLabel(String Name)
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()
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)
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, )
Methods
• boolean getState( )
• void setState(boolean on)
• String getLabel( )
• void setLabel(String str)
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)
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.
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
• 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( )
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.
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()
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() String[] getSelectedItems()
int getSelectedIndex() int[] getSelectedIndexes()
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.
FlowLayout
• Arranges the components in left to right, top to bottom fashion.
• It is the default layout manager for Panel.
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);
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);
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);
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)
Steps to Create a Frame-based Interface
1) Create a Frame object.
2) Create Component Objects.
3) Add the Component objects to Frame.
4) Set the size of the Frame and make it visible.
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);
}
}

Más contenido relacionado

La actualidad más candente

Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDrRajeshreeKhande
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and ComponentsSohanur63
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Javasuraj pandey
 
Complete java swing
Complete java swingComplete java swing
Complete java swingjehan1987
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solutionMazenetsolution
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1PRN USM
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)Bilal Amjad
 

La actualidad más candente (20)

25 awt
25 awt25 awt
25 awt
 
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
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
 
Gui
GuiGui
Gui
 
Swing
SwingSwing
Swing
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and Components
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
java swing
java swingjava swing
java swing
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 
Java swing
Java swingJava swing
Java swing
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 

Destacado (8)

Swing is not dead
Swing is not deadSwing is not dead
Swing is not dead
 
Java AWT
Java AWTJava AWT
Java AWT
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
java applets
java appletsjava applets
java applets
 
Applets
AppletsApplets
Applets
 
Java applets
Java appletsJava applets
Java applets
 
Event handling
Event handlingEvent handling
Event handling
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
 

Similar a tL19 awt

Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01Ankit Dubey
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01JONDHLEPOLY
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examplesAbdii Rashid
 
Python Graphical User Interface and design
Python Graphical User Interface and designPython Graphical User Interface and design
Python Graphical User Interface and designVardhanKulkarni
 
Chapter iii(building a simple user interface)
Chapter iii(building a simple user interface)Chapter iii(building a simple user interface)
Chapter iii(building a simple user interface)Chhom Karath
 
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window ToolkitRutvaThakkar1
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Elizabeth alexander
 
Unit-1 awt advanced java programming
Unit-1 awt advanced java programmingUnit-1 awt advanced java programming
Unit-1 awt advanced java programmingAmol Gaikwad
 
Chap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxChap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxTadeseBeyene
 
Windows Programming with AWT
Windows Programming with AWTWindows Programming with AWT
Windows Programming with AWTbackdoor
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxssuser10ef65
 

Similar a tL19 awt (20)

13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
AWT.pptx
AWT.pptxAWT.pptx
AWT.pptx
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
17625-1.pptx
17625-1.pptx17625-1.pptx
17625-1.pptx
 
Python Graphical User Interface and design
Python Graphical User Interface and designPython Graphical User Interface and design
Python Graphical User Interface and design
 
Chapter iii(building a simple user interface)
Chapter iii(building a simple user interface)Chapter iii(building a simple user interface)
Chapter iii(building a simple user interface)
 
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window Toolkit
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Unit-1 awt advanced java programming
Unit-1 awt advanced java programmingUnit-1 awt advanced java programming
Unit-1 awt advanced java programming
 
UNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdfUNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdf
 
Gui programming
Gui programmingGui programming
Gui programming
 
Chap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxChap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptx
 
Windows Programming with AWT
Windows Programming with AWTWindows Programming with AWT
Windows Programming with AWT
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptx
 
unit3.3.pptx
unit3.3.pptxunit3.3.pptx
unit3.3.pptx
 

Más de teach4uin

Master pages
Master pagesMaster pages
Master pagesteach4uin
 
.Net framework
.Net framework.Net framework
.Net frameworkteach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languagesteach4uin
 
State management
State managementState management
State managementteach4uin
 
security configuration
security configurationsecurity configuration
security configurationteach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tagsteach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tagsteach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentationteach4uin
 
.Net overview
.Net overview.Net overview
.Net overviewteach4uin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lessonteach4uin
 
storage clas
storage classtorage clas
storage clasteach4uin
 

Más de teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
enums
enumsenums
enums
 
memory
memorymemory
memory
 
array
arrayarray
array
 
storage clas
storage classtorage clas
storage clas
 

Último

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Último (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

tL19 awt

  • 1. Programming in JAVA Topic: GUI Basics (AWT)
  • 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. • There are three types of GUI classes: 1) Container classes 2) Component classes 3) Helper classes
  • 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.
  • 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.
  • 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.
  • 6. AWT Classes Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India) 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 Creates a standard window that has a title bar, resize corners, and a menu bar Graphics Encapsulates the graphics context. This context is used by the various output methods to display output in a window.
  • 7. AWT classes Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India) 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 Encapsulates a keyboard shortcut for a menu item ScrollPane A container that provides horizontal and/or vertical scroll bars for another component
  • 8. Component Class Hierarchy of AWT Component Container (Non-Container) Window Panel Frame Applet Button Label Text Field Check-Box Text Area etc.
  • 9. • Component is an abstract class that describes the basic functionality supported by all AWT components. • Container is a sub-class of Component that adds the functionality of containership to a component. • A Container component can contain another container or non- container components. • Window and Panel are two non-abstract sub-classes of Container.
  • 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.
  • 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.
  • 12. 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);
  • 13. • setBounds(): used to specify size and position of component in a container. public void setBounds (int left, int top, int width, int height)
  • 14. 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.
  • 15. 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)
  • 16. 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.
  • 17. 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.
  • 18. 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.”
  • 19. Frame Constructor: public Frame () public Frame (String Title) Methods: public String getTitle () public void setTitle (String Title) public void setVisibile(boolean Visibility)
  • 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.
  • 21. Button • Buttons are used to initiate action. Constructor: public Button () public Button (String Name) Methods: public String getLabel() public void setLabel(String Name)
  • 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()
  • 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)
  • 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, )
  • 25. Methods • boolean getState( ) • void setState(boolean on) • String getLabel( ) • void setLabel(String str)
  • 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)
  • 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.
  • 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
  • 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( )
  • 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.
  • 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()
  • 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() String[] getSelectedItems() int getSelectedIndex() int[] getSelectedIndexes()
  • 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.
  • 34. FlowLayout • Arranges the components in left to right, top to bottom fashion. • It is the default layout manager for Panel. 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);
  • 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);
  • 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);
  • 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)
  • 38. Steps to Create a Frame-based Interface 1) Create a Frame object. 2) Create Component Objects. 3) Add the Component objects to Frame. 4) Set the size of the Frame and make it visible.
  • 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); } }