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

PDF
Java GUI PART II
PPT
Md10 building java gu is
PPT
Java Swing JFC
PDF
swingbasics
PDF
JAVA GUI PART I
PPTX
Programming a guide gui
PDF
Ingles 2do parcial
PPTX
Gui in matlab :
Java GUI PART II
Md10 building java gu is
Java Swing JFC
swingbasics
JAVA GUI PART I
Programming a guide gui
Ingles 2do parcial
Gui in matlab :

What's hot (8)

PDF
Flash Tutorial
PDF
Евгений Обрезков "Behind the terminal"
PDF
The AWT and Swing
PDF
PDF
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOCX
Lenguaje de programación
PDF
120 tips about_photoshop
PDF
Google Earth User Guide
Flash Tutorial
Евгений Обрезков "Behind the terminal"
The AWT and Swing
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
Lenguaje de programación
120 tips about_photoshop
Google Earth User Guide
Ad

Viewers also liked (15)

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

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

PDF
Lecture 6.pdf
PPT
Graphical User Interface (GUI) - 1
PPT
Graphical User Interface in JAVA
PPT
Windows Programming with AWT
PDF
Java GUI Programming for beginners-graphics.pdf
PPT
Swing basics
PPTX
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
PPTX
Chapter 11.3
PPT
java swing
PDF
Session 9_AWT in java with all demonstrations.pdf
PDF
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
PPT
Chap1 1 1
PPT
Chap1 1.1
PPT
Swing and AWT in java
PDF
Swingpre 150616004959-lva1-app6892
PPT
24-BuildingGUIs Complete Materials in Java.ppt
PPTX
Abstract Window Toolkit_Event Handling_python
PPTX
PPT
13457272.ppt
Lecture 6.pdf
Graphical User Interface (GUI) - 1
Graphical User Interface in JAVA
Windows Programming with AWT
Java GUI Programming for beginners-graphics.pdf
Swing basics
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
Chapter 11.3
java swing
Session 9_AWT in java with all demonstrations.pdf
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
Chap1 1 1
Chap1 1.1
Swing and AWT in java
Swingpre 150616004959-lva1-app6892
24-BuildingGUIs Complete Materials in Java.ppt
Abstract Window Toolkit_Event Handling_python
13457272.ppt

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNIZATION PROGRAMME pptx
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
Landforms and landscapes data surprise preview
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
O7-L3 Supply Chain Operations - ICLT Program
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
102 student loan defaulters named and shamed – Is someone you know on the list?
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Open Quiz Monsoon Mind Game Final Set.pptx
Onica Farming 24rsclub profitable farm business
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNIZATION PROGRAMME pptx
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
How to Manage Loyalty Points in Odoo 18 Sales
human mycosis Human fungal infections are called human mycosis..pptx
Introduction and Scope of Bichemistry.pptx
Landforms and landscapes data surprise preview
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
UPPER GASTRO INTESTINAL DISORDER.docx

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”);