SlideShare a Scribd company logo
1 of 24
NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE
DEPARTMENT COMPUTER SCIENCE&INFORMATION TECHOLOGY
V.VANMATHY
I-MSC(CS)
Layout manager
Java LayoutManagers:
 The LayoutManagers are used to
arrange components in a particular
manner.
 The Java LayoutManagers facilitates
us to control the positioning and size of
the components in GUI forms.
LayoutManager is an interface that is
implemented by all the classes of layout
managers.
 There are the following classes that
represent the layout managers:
Layout manager
 java.awt.BorderLayout
 java.awt.FlowLayout
 java.awt.GridLayout
 java.awt.CardLayout
 java.awt.GridBagLayout
 javax.swing.BoxLayout
 javax.swing.GroupLayout
 javax.swing.ScrollPaneLayout
 javax.swing.SpringLayout etc.
Grid layout
Java GridLayout:
 The Java GridLayout class is used
to arrange the components in a
rectangular grid.
 One component is displayed in each
rectangle.
Grid layout
Constructors of GridLayout class:
GridLayout(): creates a grid layout with
one column per component in a row.
GridLayout(int rows, int columns):
creates a grid layout with the given rows
and columns but no gaps between the
components.
GridLayout(int rows, int columns, int
hgap, int vgap): creates a grid layout
with the given rows and columns along
with given horizontal and vertical gaps.
Example 1
Example of GridLayout class: Using
GridLayout() Constructor
The GridLayout() constructor creates
only
one row. The following example shows
the
usage of the parameterless constructor.
FileName: GridLayoutExample.java
// import statements
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample
{
JFrame frameObj;
// constructor
GridLayoutExample()
{
frameObj = new JFrame();
// creating 9 buttons
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
// adding buttons to the frame
// since, we are using the parameterless constructor, therf
ore;
// the number of columns is equal to the number of
buttons we
// are adding to the frame. The row count remains o
ne.
frameObj.add(btn1);
frameObj.add(btn2);
frameObj.add(btn3);
frameObj.add(btn4);
frameObj.add(btn5);
frameObj.add(btn6);
frameObj.add(btn7);
frameObj.add(btn8);
frameObj.add(btn9);
// setting the grid layout using the parameterless
constuctor
frameObj.setLayout(new GridLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new GridLayoutExample();
}
}
Output:
Example 2
Example of GridLayout class: Using
GridLayout(int rows, int columns)
Constructor
FileName: MyGridLayout.java
import java.awt.*;
import javax.swing.*;
public class MyGridLayout
{
JFrame f;
MyGridLayout()
{
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
// adding buttons to the frame
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
// setting grid layout of 3 rows and 3 columns
f.setLayout(new GridLayout(3,3));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
new MyGridLayout();
}
}
Output:
Example 3
Example of GridLayout class: Using
GridLayout(int rows, int columns, int
hgap, int vgap) Constructor
The following example inserts horizontal
and vertical gaps between buttons
using the
parameterized constructor GridLayout
(introws, int columns, int hgap, int
vgap).
FileName: GridLayoutExample1.java
// import statements
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample1
{
JFrame frameObj;
// constructor
GridLayoutExample1()
{
frameObj = new JFrame();
// creating 9 buttons
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
// adding buttons to the frame
// since, we are using the parameterless constructor, there
forer
// the number of columns is equal to the number of buttons
// are adding to the frame. The row count remains one.
frameObj.add(btn1);
frameObj.add(btn2);
frameObj.add(btn3);
frameObj.add(btn4);
frameObj.add(btn5);
frameObj.add(btn6);
frameObj.add(btn7);
frameObj.add(btn8);
frameObj.add(btn9);
// setting the grid layout
// a 3 * 3 grid is created with the horizontal gap 20
// and vertical gap 25
frameObj.setLayout(new GridLayout(3, 3, 20, 25));
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new GridLayoutExample();
}
}
Output:
Advanced Java programming

More Related Content

Similar to Advanced Java programming

Similar to Advanced Java programming (20)

Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
LAYOUT.pptx
LAYOUT.pptxLAYOUT.pptx
LAYOUT.pptx
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdfJEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
 
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
 
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event HandlingB.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
 
Module 2
Module 2Module 2
Module 2
 
Advanced Java programming
Advanced Java programmingAdvanced Java programming
Advanced Java programming
 
Layout manager
Layout managerLayout manager
Layout manager
 
Unit-1 awt advanced java programming
Unit-1 awt advanced java programmingUnit-1 awt advanced java programming
Unit-1 awt advanced java programming
 
Bài 12: JSF-2 - Lập Trình Mạng Nâng Cao
Bài 12:  JSF-2 - Lập Trình Mạng Nâng CaoBài 12:  JSF-2 - Lập Trình Mạng Nâng Cao
Bài 12: JSF-2 - Lập Trình Mạng Nâng Cao
 
[C++ GUI Programming with Qt4] chap6
[C++ GUI Programming with Qt4] chap6[C++ GUI Programming with Qt4] chap6
[C++ GUI Programming with Qt4] chap6
 
LayoutManager_Lec1.pptx
LayoutManager_Lec1.pptxLayoutManager_Lec1.pptx
LayoutManager_Lec1.pptx
 
Hello Android
Hello AndroidHello Android
Hello Android
 
Grid gain paper
Grid gain paperGrid gain paper
Grid gain paper
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 

More from vanmathy1

VANMATHY V cloud computing
VANMATHY V cloud computingVANMATHY V cloud computing
VANMATHY V cloud computingvanmathy1
 
VANMATHY V.pptx
VANMATHY V.pptxVANMATHY V.pptx
VANMATHY V.pptxvanmathy1
 
vanmathy v DIP.pptx
vanmathy v DIP.pptxvanmathy v DIP.pptx
vanmathy v DIP.pptxvanmathy1
 
Compiler design
Compiler design Compiler design
Compiler design vanmathy1
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithmvanmathy1
 

More from vanmathy1 (6)

VANMATHY V cloud computing
VANMATHY V cloud computingVANMATHY V cloud computing
VANMATHY V cloud computing
 
VANMATHY V
VANMATHY  VVANMATHY  V
VANMATHY V
 
VANMATHY V.pptx
VANMATHY V.pptxVANMATHY V.pptx
VANMATHY V.pptx
 
vanmathy v DIP.pptx
vanmathy v DIP.pptxvanmathy v DIP.pptx
vanmathy v DIP.pptx
 
Compiler design
Compiler design Compiler design
Compiler design
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 

Recently uploaded

Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 

Recently uploaded (20)

Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

Advanced Java programming

  • 1. NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE DEPARTMENT COMPUTER SCIENCE&INFORMATION TECHOLOGY V.VANMATHY I-MSC(CS)
  • 2.
  • 3. Layout manager Java LayoutManagers:  The LayoutManagers are used to arrange components in a particular manner.  The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers.  There are the following classes that represent the layout managers:
  • 4. Layout manager  java.awt.BorderLayout  java.awt.FlowLayout  java.awt.GridLayout  java.awt.CardLayout  java.awt.GridBagLayout  javax.swing.BoxLayout  javax.swing.GroupLayout  javax.swing.ScrollPaneLayout  javax.swing.SpringLayout etc.
  • 5. Grid layout Java GridLayout:  The Java GridLayout class is used to arrange the components in a rectangular grid.  One component is displayed in each rectangle.
  • 6. Grid layout Constructors of GridLayout class: GridLayout(): creates a grid layout with one column per component in a row. GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.
  • 7. Example 1 Example of GridLayout class: Using GridLayout() Constructor The GridLayout() constructor creates only one row. The following example shows the usage of the parameterless constructor. FileName: GridLayoutExample.java
  • 8. // import statements import java.awt.*; import javax.swing.*; public class GridLayoutExample { JFrame frameObj; // constructor GridLayoutExample() { frameObj = new JFrame();
  • 9. // creating 9 buttons JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4"); JButton btn5 = new JButton("5"); JButton btn6 = new JButton("6"); JButton btn7 = new JButton("7"); JButton btn8 = new JButton("8"); JButton btn9 = new JButton("9"); // adding buttons to the frame // since, we are using the parameterless constructor, therf ore;
  • 10. // the number of columns is equal to the number of buttons we // are adding to the frame. The row count remains o ne. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3); frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6); frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
  • 11. // setting the grid layout using the parameterless constuctor frameObj.setLayout(new GridLayout()); frameObj.setSize(300, 300); frameObj.setVisible(true); } // main method public static void main(String argvs[]) { new GridLayoutExample(); } }
  • 13. Example 2 Example of GridLayout class: Using GridLayout(int rows, int columns) Constructor FileName: MyGridLayout.java import java.awt.*; import javax.swing.*; public class MyGridLayout { JFrame f;
  • 14. MyGridLayout() { f=new JFrame(); JButton b1=new JButton("1"); JButton b2=new JButton("2"); JButton b3=new JButton("3"); JButton b4=new JButton("4"); JButton b5=new JButton("5"); JButton b6=new JButton("6"); JButton b7=new JButton("7"); JButton b8=new JButton("8"); JButton b9=new JButton("9");
  • 15. // adding buttons to the frame f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); f.add(b6); f.add(b7); f.add(b8); f.add(b9); // setting grid layout of 3 rows and 3 columns
  • 18. Example 3 Example of GridLayout class: Using GridLayout(int rows, int columns, int hgap, int vgap) Constructor The following example inserts horizontal and vertical gaps between buttons using the parameterized constructor GridLayout (introws, int columns, int hgap, int vgap). FileName: GridLayoutExample1.java
  • 19. // import statements import java.awt.*; import javax.swing.*; public class GridLayoutExample1 { JFrame frameObj; // constructor GridLayoutExample1() { frameObj = new JFrame();
  • 20. // creating 9 buttons JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4"); JButton btn5 = new JButton("5"); JButton btn6 = new JButton("6"); JButton btn7 = new JButton("7"); JButton btn8 = new JButton("8"); JButton btn9 = new JButton("9");
  • 21. // adding buttons to the frame // since, we are using the parameterless constructor, there forer // the number of columns is equal to the number of buttons // are adding to the frame. The row count remains one. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3); frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6); frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
  • 22. // setting the grid layout // a 3 * 3 grid is created with the horizontal gap 20 // and vertical gap 25 frameObj.setLayout(new GridLayout(3, 3, 20, 25)); frameObj.setSize(300, 300); frameObj.setVisible(true); } // main method public static void main(String argvs[]) { new GridLayoutExample(); } }