SlideShare a Scribd company logo
GETTING STARTED WITH GUI PROGRAMMING

By .Muhammad Shebl
OBJECTIVES
1.   To create user interfaces using frames.
2.   GUI components
3.   Adding Component
4.   To understand the role of layout managers
5.   Panels
6.   Actions
7.   Set & Get
FRAMES
 Frame is a window that is not contained
 inside another window.
 Frame is the basis to contain other user
 interface components in Java GUI
 applications.
 For Swing GUI programs, use JFrame class to
 create widows.
CREATING FRAMES
 import javax.swing.*;
 public class Main extends JFrame {
    public Main() {
         setSize(200, 150);
         setTitle("MUFIX");
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setVisible(true);
    }

     public static void main(String[] args) {
        Main m=new Main();
     }
 }
CENTERING FRAMES
By default, a frame is displayed in the upper-left
 corner of the screen.
To display a frame at a specified location, you can
  use the setLocation(x,y) method in the JFrame
  class.     (0, 0)


                      Screen

                               (x, y)


                                             Frame
                                                       getHeight()   screenHeight




                                          getWidth()




                                        screenWidth
CREATING GUI OBJECTS
                                                 Label   Text    Check   Radio
                                                         field   Box     Button

                       Button

// Create a button with text OK
JButton jbtOK = new JButton("OK");



// Create a label with text "Enter your name: "
JLabel jlblName = new JLabel("Enter your name: ");

// Create a text field with text "Type Name Here"                  Combo
JTextField jtfName = new JTextField("Type Name Here");             Box

// Create a check box with text bold
JCheckBox jchkBold = new JCheckBox("Bold");

// Create a radio button with text red
JRadioButton jrbRed = new JRadioButton("Red");

// Create a combo box with choices red, green, and blue
JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"});
LAYOUT MANAGERS
 The GUI components are placed in containers. Each
 container has a layout manager to arrange the GUI
 components within the container.
 Layout managers are set in containers using the
 setLayout(LayoutManager)     method in a container.
 ◦ FlowLayout

 ◦ GridLayout

 ◦ BorderLayout
FlowLayout MANAGER
The components are arranged in the container from
left to right in the order in which they were added.
When one row becomes filled, a new row is started.
container.setLayout(newFlowLayout(FlowLayout.LEFT,5,5));
FlowLayout             CONSTRUCTORS
 public FlowLayout(int align, int hGap, int vGap)
 Constructs a new FlowLayout with a specified alignment,
 horizontal gap, and vertical gap. The gaps are the distances in
 pixel between components.
 public FlowLayout(int alignment)
 Constructs a new FlowLayout with a specified alignment
 and a default gap of five pixels for both horizontal and
 vertical.
 public FlowLayout()
 Constructs a new FlowLayout with a default center alignment and
 a default gap of five pixels for both horizontal and vertical.
GridLayout MANAGER
 The GridLayout manager arranges components in a grid
 (matrix) formation with the number of rows and columns
 container.setLayout(newGridLayout(3,2,5,5));
GridLayout CONSTRUCTORS
 public GridLayout(int rows,int columns)

 Constructs a new GridLayout with the specified
 number of rows and columns.
 public GridLayout(int rows, int columns,
 int hGap, int vGap)
 Constructs a new GridLayout with the specified
 number of rows and columns, along with specified
 horizontal and vertical gaps between components.
BorderLayout MANAGER
  The BorderLayout manager divides the container
  into five areas: East, South, West, North, and Center.
container.setLayout(new BorderLayout(5,10));
container.add(jbtOK,BorderLayout.NORTH);
USING PANELS AS SUB-CONTAINERS
 Panels act as sub-containers for grouping user interface
  components.

 It is recommended that you place the user interface
  components in panels and place the panels in a frame. You
  can also place panels in a panel.

 To add a component to JFrame, you actually add it to the
  content pane of JFrame. But to add a component to a panel,
  you add it directly to the panel using the add method.
CREATING A JPanel
 You can use
   new JPanel()
  to create a panel with a default FlowLayout manager
          new JPanel(LayoutManager)
   to create a panel with the specified layout manager.

Use method add(Component)
to add a component to the panel.
For example :
      1. JPanel p = new JPanel();
      2. p.add(new JButton("OK"));
      3.     The following statement places panel p into fram   add(p);
EXAMPLE TESTING PANELS
EVENT AND EVENT SOURCE
 When you run Java GUI programs, the program
 interacts with the user and the events drive its
 execution. An event can be defined as a signal to
 the program that something has happened. Events
 are triggered either by external user actions, such
 as mouse movements, button clicks, and
 keystrokes, or by internal program activities, such
 as a timer. The program can choose to respond to or
 ignore an event.
ADDING EVENT
 JButton jbt = new JButton("OK");
 ActionListener listener = new
 OKListener();
 jbt.addActionListener(this );
SET & GET TEXT :
 getText()
 Returns the string from the text field.
 ex:txt1.getText();

 setText(String text)
Puts the given string in the text field.
 ex:txt1.setText(“MUFIX”);
QUESTIONS ?!!
TASK : CALCULATOR
Thank You

More Related Content

What's hot (8)

PDF
Flash Tutorial
senthil4seo
 
PDF
Евгений Обрезков "Behind the terminal"
Fwdays
 
PDF
The AWT and Swing
adil raja
 
PDF
Maya
senthil4seo
 
PDF
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
DOCX
Lenguaje de programación
aldair fernandez
 
PDF
120 tips about_photoshop
Galmher
 
PDF
Google Earth User Guide
F Blanco
 
Flash Tutorial
senthil4seo
 
Евгений Обрезков "Behind the terminal"
Fwdays
 
The AWT and Swing
adil raja
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
Lenguaje de programación
aldair fernandez
 
120 tips about_photoshop
Galmher
 
Google Earth User Guide
F Blanco
 

Viewers also liked (15)

PDF
Emerald Ise, Powai - E-Brochure
RiteshJuthani
 
PPTX
NYC Venues
lkittredge
 
DOCX
20 de março início do outono
Jesilaine Vinhais
 
PPTX
Grace tech tool
20gball
 
PPT
Political.pptpspsls
tyonnahartsfield
 
PPT
Lecture17aam
deepti0312
 
PDF
ponovna povezava
dinnerplier9
 
PDF
Assedio moral nas escolas - SEPE
Adriana De Moraes
 
PDF
Getting started with GUI programming in Java_2
Muhammad Shebl Farag
 
PDF
Cartilha do Servidor Estatutário do Município do RJ/ 2015
Adriana De Moraes
 
PDF
Módulo 01 - 9 ano- Matemática / Ens.Fundamental
Adriana De Moraes
 
DOC
Avaliação eja 2° período
Jesilaine Vinhais
 
DOCX
Avaliação bimestral de ciências 2º e 3º ano - 1º bimestre
Jesilaine Vinhais
 
PDF
Módulo 01 - 8 ano / Ens.Fundamental
Adriana De Moraes
 
DOCX
Borang kaji selidik sek
Nona Naza
 
Emerald Ise, Powai - E-Brochure
RiteshJuthani
 
NYC Venues
lkittredge
 
20 de março início do outono
Jesilaine Vinhais
 
Grace tech tool
20gball
 
Political.pptpspsls
tyonnahartsfield
 
Lecture17aam
deepti0312
 
ponovna povezava
dinnerplier9
 
Assedio moral nas escolas - SEPE
Adriana De Moraes
 
Getting started with GUI programming in Java_2
Muhammad Shebl Farag
 
Cartilha do Servidor Estatutário do Município do RJ/ 2015
Adriana De Moraes
 
Módulo 01 - 9 ano- Matemática / Ens.Fundamental
Adriana De Moraes
 
Avaliação eja 2° período
Jesilaine Vinhais
 
Avaliação bimestral de ciências 2º e 3º ano - 1º bimestre
Jesilaine Vinhais
 
Módulo 01 - 8 ano / Ens.Fundamental
Adriana De Moraes
 
Borang kaji selidik sek
Nona Naza
 
Ad

Similar to Getting started with GUI programming in Java_1 (20)

PDF
Lecture 6.pdf
SakhilejasonMsibi
 
PPT
Graphical User Interface (GUI) - 1
PRN USM
 
PPT
Graphical User Interface in JAVA
suraj pandey
 
PPT
Windows Programming with AWT
backdoor
 
PDF
Java GUI Programming for beginners-graphics.pdf
PBMaverick
 
PPT
Swing basics
Medi-Caps University
 
PPTX
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
utkarshabhope
 
PPTX
Chapter 11.3
sotlsoc
 
PPT
java swing
Waheed Warraich
 
PDF
Session 9_AWT in java with all demonstrations.pdf
tabbu23
 
PDF
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
PPT
Chap1 1 1
Hemo Chella
 
PPT
Chap1 1.1
Hemo Chella
 
PPT
Swing and AWT in java
Adil Mehmoood
 
PDF
Swingpre 150616004959-lva1-app6892
renuka gavli
 
PPTX
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
PPTX
ch20.pptx
EnriqueMartinezTelle2
 
PPT
13457272.ppt
aptechaligarh
 
PPT
28 awt
Prachi Vijh
 
Lecture 6.pdf
SakhilejasonMsibi
 
Graphical User Interface (GUI) - 1
PRN USM
 
Graphical User Interface in JAVA
suraj pandey
 
Windows Programming with AWT
backdoor
 
Java GUI Programming for beginners-graphics.pdf
PBMaverick
 
Swing basics
Medi-Caps University
 
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
utkarshabhope
 
Chapter 11.3
sotlsoc
 
java swing
Waheed Warraich
 
Session 9_AWT in java with all demonstrations.pdf
tabbu23
 
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
Chap1 1 1
Hemo Chella
 
Chap1 1.1
Hemo Chella
 
Swing and AWT in java
Adil Mehmoood
 
Swingpre 150616004959-lva1-app6892
renuka gavli
 
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
13457272.ppt
aptechaligarh
 
28 awt
Prachi Vijh
 
Ad

Recently uploaded (20)

PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
community health nursing question paper 2.pdf
Prince kumar
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 

Getting started with GUI programming in Java_1

  • 1. GETTING STARTED WITH GUI PROGRAMMING By .Muhammad Shebl
  • 2. OBJECTIVES 1. To create user interfaces using frames. 2. GUI components 3. Adding Component 4. To understand the role of layout managers 5. Panels 6. Actions 7. Set & Get
  • 3. FRAMES  Frame is a window that is not contained inside another window.  Frame is the basis to contain other user interface components in Java GUI applications.  For Swing GUI programs, use JFrame class to create widows.
  • 4. CREATING FRAMES import javax.swing.*; public class Main extends JFrame { public Main() { setSize(200, 150); setTitle("MUFIX"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { Main m=new Main(); } }
  • 5. CENTERING FRAMES By default, a frame is displayed in the upper-left corner of the screen. To display a frame at a specified location, you can use the setLocation(x,y) method in the JFrame class. (0, 0) Screen (x, y) Frame getHeight() screenHeight getWidth() screenWidth
  • 6. CREATING GUI OBJECTS Label Text Check Radio field Box Button Button // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblName = new JLabel("Enter your name: "); // Create a text field with text "Type Name Here" Combo JTextField jtfName = new JTextField("Type Name Here"); Box // Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blue JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"});
  • 7. LAYOUT MANAGERS  The GUI components are placed in containers. Each container has a layout manager to arrange the GUI components within the container.  Layout managers are set in containers using the setLayout(LayoutManager) method in a container. ◦ FlowLayout ◦ GridLayout ◦ BorderLayout
  • 8. FlowLayout MANAGER The components are arranged in the container from left to right in the order in which they were added. When one row becomes filled, a new row is started. container.setLayout(newFlowLayout(FlowLayout.LEFT,5,5));
  • 9. FlowLayout CONSTRUCTORS  public FlowLayout(int align, int hGap, int vGap) Constructs a new FlowLayout with a specified alignment, horizontal gap, and vertical gap. The gaps are the distances in pixel between components.  public FlowLayout(int alignment) Constructs a new FlowLayout with a specified alignment and a default gap of five pixels for both horizontal and vertical.  public FlowLayout() Constructs a new FlowLayout with a default center alignment and a default gap of five pixels for both horizontal and vertical.
  • 10. GridLayout MANAGER  The GridLayout manager arranges components in a grid (matrix) formation with the number of rows and columns container.setLayout(newGridLayout(3,2,5,5));
  • 11. GridLayout CONSTRUCTORS  public GridLayout(int rows,int columns) Constructs a new GridLayout with the specified number of rows and columns.  public GridLayout(int rows, int columns, int hGap, int vGap) Constructs a new GridLayout with the specified number of rows and columns, along with specified horizontal and vertical gaps between components.
  • 12. BorderLayout MANAGER  The BorderLayout manager divides the container into five areas: East, South, West, North, and Center. container.setLayout(new BorderLayout(5,10)); container.add(jbtOK,BorderLayout.NORTH);
  • 13. USING PANELS AS SUB-CONTAINERS  Panels act as sub-containers for grouping user interface components.  It is recommended that you place the user interface components in panels and place the panels in a frame. You can also place panels in a panel.  To add a component to JFrame, you actually add it to the content pane of JFrame. But to add a component to a panel, you add it directly to the panel using the add method.
  • 14. CREATING A JPanel  You can use  new JPanel() to create a panel with a default FlowLayout manager  new JPanel(LayoutManager) to create a panel with the specified layout manager. Use method add(Component) to add a component to the panel. For example : 1. JPanel p = new JPanel(); 2. p.add(new JButton("OK")); 3. The following statement places panel p into fram add(p);
  • 16. EVENT AND EVENT SOURCE  When you run Java GUI programs, the program interacts with the user and the events drive its execution. An event can be defined as a signal to the program that something has happened. Events are triggered either by external user actions, such as mouse movements, button clicks, and keystrokes, or by internal program activities, such as a timer. The program can choose to respond to or ignore an event.
  • 17. ADDING EVENT  JButton jbt = new JButton("OK");  ActionListener listener = new OKListener(); jbt.addActionListener(this );
  • 18. SET & GET TEXT :  getText() Returns the string from the text field. ex:txt1.getText();  setText(String text) Puts the given string in the text field. ex:txt1.setText(“MUFIX”);