SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
» GUI
˃
˃
˃
˃

https://www.facebook.com/Oxus20

oxus20@gmail.com

GUI components
JButton, JLabel …
Layout Manager s
FlowLayout, GridLayout…

JAVA
GUI
PART II
Milad Kawesh
Agenda
» GUI Components
˃ JButton, JLabel
˃ JTextArea , JTextField
˃ JMenuBar, JMenu , JMenuItem

» Layout Managers
˃
˃
˃
˃

FlowLayout
GridLayout
BorderLayout
No Layout
2

https://www.facebook.com/Oxus20
GUI Components
» JButton
˃
˃
˃
˃

new JButton();
new JButton(String test);
new JButton(Icon icon);
new JButton(String text, Icon icon);

» JLable
˃ new JLable();
˃ new JLable(String text);
˃ new JLable(Icon image);
3

https://www.facebook.com/Oxus20
GUI Components(cont.)
» JTextField :
˃
˃
˃
˃

new JTextField ();
new JTextField (String test);
new JTextField (int columns);
new Jbutton(String text, int columns );

» JTextArea :
˃ new JTextArea();
˃ new JTextArea(String text);
˃ new JTextArea(int rows , int columns);
4

https://www.facebook.com/Oxus20
GUI Components(cont.)
» JMenuBar :
˃ new JMenuBar();

» JMenu :
˃ new JMenu();
˃ new JMenu(String text);

» JMenuItem:
˃
˃
˃
˃

new JMenuItem();
new JMenuItem(String text);
new JMenuItem(Icon icon);
new JMenuItem(String text , Icon icon);
https://www.facebook.com/Oxus20

5
GUI Components Example
import
import
import
import
import
import
import
import

java.awt.BorderLayout;
java.awt.Container;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JMenu;
javax.swing.JMenuBar;
javax.swing.JMenuItem;

public class Gui_com extends JFrame {
private Container back;
public Gui_com() {
back = this.getContentPane();
back.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
https://www.facebook.com/Oxus20

6
GUI Components Example (cont.…)
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem open = new JMenuItem("Open");
file.add(open);
JMenuItem print = new JMenuItem("Print");
file.add(print);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
back.add(new JButton("ok"), BorderLayout.CENTER);
back.add(new JLabel("Oxus20"), BorderLayout.SOUTH);

7

https://www.facebook.com/Oxus20
GUI Components Example (Cont.…)
setTitle("Oxus20 Class");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Gui_com();
}
}

8

https://www.facebook.com/Oxus20
GUI Components Example
OUTPUT

9

https://www.facebook.com/Oxus20
Layout Managers
» When adding components to container you mast uses a
layout manager to determine size and location of the

components within the container.
» A container can be assigned one layout manager, which is
done using the setLayout() method of the
java.awt.Container class:
» public void setLayout(LayoutManager m) LayoutManager

is an interface that all the layout managers’ classes must
implement.
https://www.facebook.com/Oxus20

10
FlowLayout Manager
» Components have their preferred size.
» The order in which the components are added determines
their order in the container.
» If the container is not wide enough to display all of the

components, the components wrap around to a new line.
» You can control whether the components are centered,

left-justified, or right-justified and vertical and horizontal
gap between components.
https://www.facebook.com/Oxus20

11
FlowLayout Constructors
» public FlowLayout(). Creates a new object that centers
the components with a horizontal and vertical gap of 5
units .
» public FlowLayout(int align). Creates a new object with
one of specified alignment: FlowLayout.CENTER
,FlowLayout.RIGHT, or FlowLayout.LEFT. And 5 units for horizontal
and vertical gap.
» public FlowLayout(int align, int hgap, int vgap). Creates
a FlowLayout object with the specified alignment,
horizontal gap, and vertical gap.
12

https://www.facebook.com/Oxus20
FlowLayout example
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutDemo {
public static void main(String[] args) {
JFrame window = new JFrame("Oxus20 Class");
window.setLayout(new FlowLayout());
JButton[] btns = new JButton[10];
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton(String.format("%d", i + 1));
window.add(btns[i]);
}
13

https://www.facebook.com/Oxus20
FlowLayout example (Cont.…)
window.setSize(300, 100);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}

14

https://www.facebook.com/Oxus20
FlowLayout OUTPUT

15

https://www.facebook.com/Oxus20
BorderLayout Manager
» BorderLayout Divides a container into five regions, allowing
one component to be added to each region. Frame and the

content pane of a JFrame have BorderLayout by default.
» When adding a component to a container you can use one
of possible static values (NORTH, SOUTH, EAST, WEST, and
CENTER ) from BorderLayout class.
» Only one component can be added to a given region, and

the size of the component is determined by the region it
appears in.
16

https://www.facebook.com/Oxus20
BorderLayout Constructors
» public BorderLayout(). Creates a new BorderLayout

with a horizontal and vertical gap of five units between
components.

» public BorderLayout(int hgap, int vgap). Creates a
BorderLayout object with the specified horizontal and
vertical gap.
17

https://www.facebook.com/Oxus20
BorderLayout example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BorderlayoutDemo {
public static void main(String[] args) {
JFrame window = new JFrame("Oxus20 Class");
window.setLayout(new BorderLayout());
JButton up = new JButton("up");
JButton down = new JButton("down");
JButton right = new JButton("right");
JButton left = new JButton("left");
18

https://www.facebook.com/Oxus20
BorderLayout example (Cont.…)
JButton center = new JButton("center");
window.add(up, BorderLayout.NORTH);
window.add(right, BorderLayout.EAST);
window.add(center, BorderLayout.CENTER);
window.add(left, BorderLayout.WEST);
window.add(down, BorderLayout.SOUTH);

window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
19

https://www.facebook.com/Oxus20
BorderLayout OUTPUT

20

https://www.facebook.com/Oxus20
GridLayout Manager
» Divides a container into a grid of rows and columns,
only one component can be added to each region of
the grid and each component having the same size.
» The order in which components are added determines
their locations in the grid.
» No components get their preferred height or width.
21

https://www.facebook.com/Oxus20
GridLayout Constructors
» public GridLayout(int rows, int cols). Creates new
object with the specified number of rows and columns.
The horizontal and vertical gap between components is
five units.
» public GridLayout(int rows, int cols, int hgap, int vgap).

Creates new object with the specified number of rows
and columns and also with the specified horizontal and
vertical gap.
» public GridLayout(). Creates new object with one row
and any number of columns.
22

https://www.facebook.com/Oxus20
GridLayout example
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutDemo {
public static void main(String[] args) {
JFrame window = new JFrame("Oxus20 Class");
window.setLayout(new GridLayout(3, 4));
JButton[] btns = new JButton[10];
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton(String.format("%d", i + 1));
window.add(btns[i]);
}

23

https://www.facebook.com/Oxus20
GridLayout example (Cont.…)
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}

24

https://www.facebook.com/Oxus20
GridLayout OUTPUT

25

https://www.facebook.com/Oxus20
No Layout Manager
» You can create a GUI with components in the exact

location and size that you want.
» To do this, you set the layout manager of the container
to null
» Set the bounds for each component within the
container by using setBounds(x, y, width, height)

method.
26

https://www.facebook.com/Oxus20
No Layout example
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class NoLayout {
public static void main(String[] args) {
JFrame window = new JFrame("Oxus20 Class");
window.setLayout(null);
JButton btn_oxus20 = new JButton("Oxus20");
btn_oxus20.setBounds(100, 100, 100, 30);
window.add(btn_oxus20);
27

https://www.facebook.com/Oxus20
No Layout example (Cont.…)
JLabel lbl_Oxus20 = new JLabel("Oxus20 ");
lbl_Oxus20.setBounds(10, 150, 100, 40);
window.add(lbl_Oxus20);
window.setSize(300, 300);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
28

https://www.facebook.com/Oxus20
No Layout OUTPUT

29

https://www.facebook.com/Oxus20
END

30

https://www.facebook.com/Oxus20

Más contenido relacionado

La actualidad más candente

Database development life cycle
Database development life cycleDatabase development life cycle
Database development life cycleAfrasiyab Haider
 
Project Scope Management - PMBOK6
Project Scope Management - PMBOK6Project Scope Management - PMBOK6
Project Scope Management - PMBOK6Agus Suhanto
 
Project scope management
Project scope managementProject scope management
Project scope managementJody R Flower
 
Object Oriented Analysis Design using UML
Object Oriented Analysis Design using UMLObject Oriented Analysis Design using UML
Object Oriented Analysis Design using UMLAjit Nayak
 
Implement text editor
Implement text editorImplement text editor
Implement text editorAmaan Shaikh
 
Software engineering project management
Software engineering project managementSoftware engineering project management
Software engineering project managementjhudyne
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textareamyrajendra
 
Formal Specification Ian Sommerville 9th Edition
Formal Specification Ian Sommerville 9th EditionFormal Specification Ian Sommerville 9th Edition
Formal Specification Ian Sommerville 9th EditionRupeshShrestha28
 
Project Scope Management -
Project Scope Management - Project Scope Management -
Project Scope Management - dyaksa hanindito
 
Software Cost Estimation Techniques
Software Cost Estimation TechniquesSoftware Cost Estimation Techniques
Software Cost Estimation TechniquesSanthi thi
 
Ch 3 event driven programming
Ch 3 event driven programmingCh 3 event driven programming
Ch 3 event driven programmingChaffey College
 
Earned value management for Beginners
Earned value management for Beginners Earned value management for Beginners
Earned value management for Beginners Shenin Hassan
 
Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6Jeanie Arnoco
 
08 state diagram and activity diagram
08 state diagram and activity diagram08 state diagram and activity diagram
08 state diagram and activity diagramBaskarkncet
 

La actualidad más candente (20)

Database development life cycle
Database development life cycleDatabase development life cycle
Database development life cycle
 
Project Scope Management - PMBOK6
Project Scope Management - PMBOK6Project Scope Management - PMBOK6
Project Scope Management - PMBOK6
 
Project scope management
Project scope managementProject scope management
Project scope management
 
Object Oriented Analysis Design using UML
Object Oriented Analysis Design using UMLObject Oriented Analysis Design using UML
Object Oriented Analysis Design using UML
 
Implement text editor
Implement text editorImplement text editor
Implement text editor
 
Software engineering project management
Software engineering project managementSoftware engineering project management
Software engineering project management
 
Project quality management
Project quality managementProject quality management
Project quality management
 
Asp.net file types
Asp.net file typesAsp.net file types
Asp.net file types
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textarea
 
Formal Specification Ian Sommerville 9th Edition
Formal Specification Ian Sommerville 9th EditionFormal Specification Ian Sommerville 9th Edition
Formal Specification Ian Sommerville 9th Edition
 
Requirement Analysis - Software Enigneering
Requirement Analysis - Software EnigneeringRequirement Analysis - Software Enigneering
Requirement Analysis - Software Enigneering
 
Project Scope Management -
Project Scope Management - Project Scope Management -
Project Scope Management -
 
05 project scope management
05 project scope management05 project scope management
05 project scope management
 
1 introduction of OOAD
1 introduction of OOAD1 introduction of OOAD
1 introduction of OOAD
 
Software Cost Estimation Techniques
Software Cost Estimation TechniquesSoftware Cost Estimation Techniques
Software Cost Estimation Techniques
 
Object modeling
Object modelingObject modeling
Object modeling
 
Ch 3 event driven programming
Ch 3 event driven programmingCh 3 event driven programming
Ch 3 event driven programming
 
Earned value management for Beginners
Earned value management for Beginners Earned value management for Beginners
Earned value management for Beginners
 
Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6Introduction to programming using Visual Basic 6
Introduction to programming using Visual Basic 6
 
08 state diagram and activity diagram
08 state diagram and activity diagram08 state diagram and activity diagram
08 state diagram and activity diagram
 

Destacado

JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART IIIOXUS 20
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A ReviewFernando Torres
 
java swing programming
java swing programming java swing programming
java swing programming Ankit Desai
 
Java Swing
Java SwingJava Swing
Java SwingShraddha
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
011 more swings_adv
011 more swings_adv011 more swings_adv
011 more swings_advChaimaa Kabb
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4sotlsoc
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event HandlingJava Programming
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transferAnkit Desai
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Javababak danyal
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
TKP Java Notes for Teaching Kids Programming
TKP Java Notes for Teaching Kids ProgrammingTKP Java Notes for Teaching Kids Programming
TKP Java Notes for Teaching Kids ProgrammingLynn Langit
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaOXUS 20
 

Destacado (20)

JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART III
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
java swing programming
java swing programming java swing programming
java swing programming
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java Swing
Java SwingJava Swing
Java Swing
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Jtextarea
JtextareaJtextarea
Jtextarea
 
011 more swings_adv
011 more swings_adv011 more swings_adv
011 more swings_adv
 
Swing
SwingSwing
Swing
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Power editor basics
Power editor basicsPower editor basics
Power editor basics
 
Java swings
Java swingsJava swings
Java swings
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event Handling
 
JDBC
JDBCJDBC
JDBC
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transfer
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
TKP Java Notes for Teaching Kids Programming
TKP Java Notes for Teaching Kids ProgrammingTKP Java Notes for Teaching Kids Programming
TKP Java Notes for Teaching Kids Programming
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
 

Similar a Java GUI PART II

Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Muhammad Shebl Farag
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3sotlsoc
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVAsuraj pandey
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1PRN USM
 
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepOXUS 20
 
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsbabhishekmathuroffici
 
To change this template
To change this templateTo change this template
To change this templatekio1985
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and GraphicsOXUS 20
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFCSunil OS
 
Layout managementand event handling
Layout managementand event handlingLayout managementand event handling
Layout managementand event handlingCharli Patel
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonEric Wendelin
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonMatthew McCullough
 

Similar a Java GUI PART II (20)

Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1
 
Chap1 1 4
Chap1 1 4Chap1 1 4
Chap1 1 4
 
Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
 
swingbasics
swingbasicsswingbasics
swingbasics
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
java swing
java swingjava swing
java swing
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVA
 
Java swing
Java swingJava swing
Java swing
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Swing
SwingSwing
Swing
 
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
 
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
 
To change this template
To change this templateTo change this template
To change this template
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
Layout managementand event handling
Layout managementand event handlingLayout managementand event handling
Layout managementand event handling
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 

Más de OXUS 20

Java Arrays
Java ArraysJava Arrays
Java ArraysOXUS 20
 
Java Methods
Java MethodsJava Methods
Java MethodsOXUS 20
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement OXUS 20
 
Structure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryStructure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryOXUS 20
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationOXUS 20
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersOXUS 20
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsOXUS 20
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesOXUS 20
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesOXUS 20
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesOXUS 20
 
Java Regular Expression PART II
Java Regular Expression PART IIJava Regular Expression PART II
Java Regular Expression PART IIOXUS 20
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART IOXUS 20
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number TutorialOXUS 20
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIOXUS 20
 
Object Oriented Programming with Real World Examples
Object Oriented Programming with Real World ExamplesObject Oriented Programming with Real World Examples
Object Oriented Programming with Real World ExamplesOXUS 20
 
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticObject Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticOXUS 20
 

Más de OXUS 20 (16)

Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement
 
Structure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryStructure programming – Java Programming – Theory
Structure programming – Java Programming – Theory
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail Explanation
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and Answers
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and Relationships
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and Technologies
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI Examples
 
Java Regular Expression PART II
Java Regular Expression PART IIJava Regular Expression PART II
Java Regular Expression PART II
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
 
Object Oriented Programming with Real World Examples
Object Oriented Programming with Real World ExamplesObject Oriented Programming with Real World Examples
Object Oriented Programming with Real World Examples
 
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticObject Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
 

Último

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 

Último (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 

Java GUI PART II

  • 1. » GUI ˃ ˃ ˃ ˃ https://www.facebook.com/Oxus20 oxus20@gmail.com GUI components JButton, JLabel … Layout Manager s FlowLayout, GridLayout… JAVA GUI PART II Milad Kawesh
  • 2. Agenda » GUI Components ˃ JButton, JLabel ˃ JTextArea , JTextField ˃ JMenuBar, JMenu , JMenuItem » Layout Managers ˃ ˃ ˃ ˃ FlowLayout GridLayout BorderLayout No Layout 2 https://www.facebook.com/Oxus20
  • 3. GUI Components » JButton ˃ ˃ ˃ ˃ new JButton(); new JButton(String test); new JButton(Icon icon); new JButton(String text, Icon icon); » JLable ˃ new JLable(); ˃ new JLable(String text); ˃ new JLable(Icon image); 3 https://www.facebook.com/Oxus20
  • 4. GUI Components(cont.) » JTextField : ˃ ˃ ˃ ˃ new JTextField (); new JTextField (String test); new JTextField (int columns); new Jbutton(String text, int columns ); » JTextArea : ˃ new JTextArea(); ˃ new JTextArea(String text); ˃ new JTextArea(int rows , int columns); 4 https://www.facebook.com/Oxus20
  • 5. GUI Components(cont.) » JMenuBar : ˃ new JMenuBar(); » JMenu : ˃ new JMenu(); ˃ new JMenu(String text); » JMenuItem: ˃ ˃ ˃ ˃ new JMenuItem(); new JMenuItem(String text); new JMenuItem(Icon icon); new JMenuItem(String text , Icon icon); https://www.facebook.com/Oxus20 5
  • 6. GUI Components Example import import import import import import import import java.awt.BorderLayout; java.awt.Container; javax.swing.JButton; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JMenu; javax.swing.JMenuBar; javax.swing.JMenuItem; public class Gui_com extends JFrame { private Container back; public Gui_com() { back = this.getContentPane(); back.setLayout(new BorderLayout()); JMenuBar menuBar = new JMenuBar(); this.setJMenuBar(menuBar); https://www.facebook.com/Oxus20 6
  • 7. GUI Components Example (cont.…) JMenu file = new JMenu("File"); menuBar.add(file); JMenuItem open = new JMenuItem("Open"); file.add(open); JMenuItem print = new JMenuItem("Print"); file.add(print); JMenuItem exit = new JMenuItem("Exit"); file.add(exit); back.add(new JButton("ok"), BorderLayout.CENTER); back.add(new JLabel("Oxus20"), BorderLayout.SOUTH); 7 https://www.facebook.com/Oxus20
  • 8. GUI Components Example (Cont.…) setTitle("Oxus20 Class"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new Gui_com(); } } 8 https://www.facebook.com/Oxus20
  • 10. Layout Managers » When adding components to container you mast uses a layout manager to determine size and location of the components within the container. » A container can be assigned one layout manager, which is done using the setLayout() method of the java.awt.Container class: » public void setLayout(LayoutManager m) LayoutManager is an interface that all the layout managers’ classes must implement. https://www.facebook.com/Oxus20 10
  • 11. FlowLayout Manager » Components have their preferred size. » The order in which the components are added determines their order in the container. » If the container is not wide enough to display all of the components, the components wrap around to a new line. » You can control whether the components are centered, left-justified, or right-justified and vertical and horizontal gap between components. https://www.facebook.com/Oxus20 11
  • 12. FlowLayout Constructors » public FlowLayout(). Creates a new object that centers the components with a horizontal and vertical gap of 5 units . » public FlowLayout(int align). Creates a new object with one of specified alignment: FlowLayout.CENTER ,FlowLayout.RIGHT, or FlowLayout.LEFT. And 5 units for horizontal and vertical gap. » public FlowLayout(int align, int hgap, int vgap). Creates a FlowLayout object with the specified alignment, horizontal gap, and vertical gap. 12 https://www.facebook.com/Oxus20
  • 13. FlowLayout example import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; public class FlowLayoutDemo { public static void main(String[] args) { JFrame window = new JFrame("Oxus20 Class"); window.setLayout(new FlowLayout()); JButton[] btns = new JButton[10]; for (int i = 0; i < btns.length; i++) { btns[i] = new JButton(String.format("%d", i + 1)); window.add(btns[i]); } 13 https://www.facebook.com/Oxus20
  • 14. FlowLayout example (Cont.…) window.setSize(300, 100); window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } } 14 https://www.facebook.com/Oxus20
  • 16. BorderLayout Manager » BorderLayout Divides a container into five regions, allowing one component to be added to each region. Frame and the content pane of a JFrame have BorderLayout by default. » When adding a component to a container you can use one of possible static values (NORTH, SOUTH, EAST, WEST, and CENTER ) from BorderLayout class. » Only one component can be added to a given region, and the size of the component is determined by the region it appears in. 16 https://www.facebook.com/Oxus20
  • 17. BorderLayout Constructors » public BorderLayout(). Creates a new BorderLayout with a horizontal and vertical gap of five units between components. » public BorderLayout(int hgap, int vgap). Creates a BorderLayout object with the specified horizontal and vertical gap. 17 https://www.facebook.com/Oxus20
  • 18. BorderLayout example import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; public class BorderlayoutDemo { public static void main(String[] args) { JFrame window = new JFrame("Oxus20 Class"); window.setLayout(new BorderLayout()); JButton up = new JButton("up"); JButton down = new JButton("down"); JButton right = new JButton("right"); JButton left = new JButton("left"); 18 https://www.facebook.com/Oxus20
  • 19. BorderLayout example (Cont.…) JButton center = new JButton("center"); window.add(up, BorderLayout.NORTH); window.add(right, BorderLayout.EAST); window.add(center, BorderLayout.CENTER); window.add(left, BorderLayout.WEST); window.add(down, BorderLayout.SOUTH); window.setSize(300, 300); window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } } 19 https://www.facebook.com/Oxus20
  • 21. GridLayout Manager » Divides a container into a grid of rows and columns, only one component can be added to each region of the grid and each component having the same size. » The order in which components are added determines their locations in the grid. » No components get their preferred height or width. 21 https://www.facebook.com/Oxus20
  • 22. GridLayout Constructors » public GridLayout(int rows, int cols). Creates new object with the specified number of rows and columns. The horizontal and vertical gap between components is five units. » public GridLayout(int rows, int cols, int hgap, int vgap). Creates new object with the specified number of rows and columns and also with the specified horizontal and vertical gap. » public GridLayout(). Creates new object with one row and any number of columns. 22 https://www.facebook.com/Oxus20
  • 23. GridLayout example import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; public class GridLayoutDemo { public static void main(String[] args) { JFrame window = new JFrame("Oxus20 Class"); window.setLayout(new GridLayout(3, 4)); JButton[] btns = new JButton[10]; for (int i = 0; i < btns.length; i++) { btns[i] = new JButton(String.format("%d", i + 1)); window.add(btns[i]); } 23 https://www.facebook.com/Oxus20
  • 24. GridLayout example (Cont.…) window.setSize(300, 300); window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } } 24 https://www.facebook.com/Oxus20
  • 26. No Layout Manager » You can create a GUI with components in the exact location and size that you want. » To do this, you set the layout manager of the container to null » Set the bounds for each component within the container by using setBounds(x, y, width, height) method. 26 https://www.facebook.com/Oxus20
  • 27. No Layout example import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class NoLayout { public static void main(String[] args) { JFrame window = new JFrame("Oxus20 Class"); window.setLayout(null); JButton btn_oxus20 = new JButton("Oxus20"); btn_oxus20.setBounds(100, 100, 100, 30); window.add(btn_oxus20); 27 https://www.facebook.com/Oxus20
  • 28. No Layout example (Cont.…) JLabel lbl_Oxus20 = new JLabel("Oxus20 "); lbl_Oxus20.setBounds(10, 150, 100, 40); window.add(lbl_Oxus20); window.setSize(300, 300); window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } } 28 https://www.facebook.com/Oxus20