Gui Programming in Java523
-
Upload
kavitha-anbukkarasu -
Category
Documents
-
view
229 -
download
0
Transcript of Gui Programming in Java523
![Page 1: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/1.jpg)
GUI Programming
in JavaPresented by Thanh Pham
[email protected]/2007
B070038 – NIIT Quang Trung
Because Learning Never Stop!
![Page 2: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/2.jpg)
Contents
Basic Concepts1
AWT and Swing Control Components2
Layout Manager3
Event Handling4
Other References5
![Page 3: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/3.jpg)
Basic Concepts
Basic ConceptsBasic Concepts
ExercisesDemosAWT vs. Swing
GUI?
![Page 4: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/4.jpg)
GUI?
Graphical User Interface (pronounced "GOO-ee“).
Presents a user-friendly mechanism for interacting with an application.
Users can learn how to use an app quickly and use it more productively. Ex: Internet Explorer, MS Word, JCreator…
GUIs are built from GUI components (controls or widgets-window gadget).
A GUI component is an object with which the user interacts via the mouse, the keyboard or another form of input, such as voice recognition.
![Page 5: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/5.jpg)
Abstract Window Toolkit (AWT) vs. Swing
Similarities: Tools provided by Java for developing
interactive GUI applications Provides GUI components that can be used in
creating Java applications and applets
![Page 6: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/6.jpg)
Abstract Window Toolkit (AWT) vs. Swing
AWT (Abstract Window Toolkit) Some AWT components use native code Platform-dependent Ensure that the look and feel of an application run on
different machines be comparableSwing
Written entirely using the Java programming language Platform-independent Ensures applications deployed across different
platforms have the same appearance Built around a number of APIs that implement various
parts of the AWT• Can be used with AWT
![Page 7: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/7.jpg)
Demo
MessageBoxInputBoxCreate Frame (AWT)Create Frame (Swing)CenterFrame
![Page 8: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/8.jpg)
Exercises
Display two input-boxes that accepts two numbers then shows the result.
Accepts three numbers, displays the largest, smallest.(Assume that all input values are valid
numbers)Create a simple frame
![Page 9: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/9.jpg)
AWT and Swing Control Components
AWT and Swing Control Components
ExercisesDemosSwingAWT
![Page 10: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/10.jpg)
AWT Control Components
An AWT control is a component that enables end users to interact with applications created in Java.
All AWT controls in Java are subclasses of the Component class.
The Component class provides the add() method to add AWT components to containers, such as an applet or a window.
![Page 11: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/11.jpg)
AWT Control Components
TextFieldTextAreaButtonListCheckBoxChoiceLabels
![Page 12: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/12.jpg)
Demo
FrameWithControl
![Page 13: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/13.jpg)
Swing Control Components
Swing components contain the Pluggable Look and Feel (PL&F) feature that allows applications to have the same behavior on various platforms.
Identifying the Swing Component Class Hierarchy The JComponent class is the root of the Swing hierarchy, which
is an extension of the AWT container class. The class hierarchy of the Swing components is categorized
into: • Top-level Swing Containers: Acts as a container for placing the
intermediate-level and atomic swing components, such as panels, frames, buttons, and check boxes.
• Intermediate-level Swing Containers: Placed on the top-level containers and contains atomic components.
• Atomic Components: Placed on the intermediate-level swing containers. Atomic components are used to accept input from a user.
![Page 14: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/14.jpg)
Swing Control Components
Using the Top-level Swing Containers JApplet
• The JApplet class is an extension of the AWT applet class. • The Swing components that contain an applet need to
extend the JApplet class.• The JApplet() constructor enables you to create a swing
applet instance when you create an instance of the JApplet class.
JFrame:• The JFrame class is an extension of the AWT Frame class. • You cannot add components directly to JFrame.
![Page 15: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/15.jpg)
Swing Control Components
Using the Top-level Swing Containers (Cont.) JDialog
• The JDialog class is an extension of the AWT java.awt.Dialog class.
Using the Intermediate Level Swing Containers JPanel
• JPanel class is an extension of the JComponent class that provides a replacement for the AWT Panel class.
• You create a panel and add various components to it. • The panel is further added to the content pane, which
represents the display area of a window, a dialog, or a frame. • JPanel supports all layout managers of AWT. • By default, JPanel applies the flow layout manager.
![Page 16: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/16.jpg)
Swing Control Components
Using the Intermediate Level Swing Containers (Contd.) JTabbedPane:
• The JTabbedPane class is used to create a tabbed pane component that enables you to switch between groups of components by clicking a tab with a given label.
• Tabs are added to the JTabbedPane object by using the addTab() method.
• The JTabbedPane class enables you to add multiple components but it displays only a single component at a time.
Using the Atomic Components JButton JTextField JCheckBox JComboBox JLabel JRadioButton
![Page 17: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/17.jpg)
Demo
JFrameWithControl
![Page 18: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/18.jpg)
Exercises
![Page 19: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/19.jpg)
Layout Managers
Layout Managers
DemoGridLayout
Border Layout
FlowLayout
![Page 20: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/20.jpg)
Layout Managers
The layout managers are used to position the components, such as an applet, a panel, or a frame in a container.
The layout managers implement the java.awt.LayoutManager interface.
A layout manager is an instance of the LayoutManager interface in Java.
You can use the following method to apply the desired layout to the components: void setLayout(layoutManager obj) In the preceding syntax, obj is the reference to the desired layout
manager. Java has various predefined classes of layout managers. All layout managers make use of the setLayout() method to set the
layout of a container. If the setLayout() method is not used, then the default layout of the
container is set.
![Page 21: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/21.jpg)
Layout Managers
The different types of layout managers are: FlowLayout Manager
• The flow layout is the default layout manager used for the Applet class.
• In the flow layout manager, the components are placed in a container window in a sequence one after the other in rows.
• Java provides the FlowLayout class to apply flow layout to the various components that you are inserting in an applet.
• You can use the following constructors to create an instance of the FlowLayout class:
– FlowLayout()– FlowLayout(int align)– FlowLayout(int align, int hgap,int vgap)
![Page 22: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/22.jpg)
Layout Managers
BorderLayout Manager BorderLayout is the default layout of the Frame class. The BorderLayout layout manager divides the container into
north, south, east, west, and centre regions. You can place five components or controls in each part. Java provides the BorderLayout class to apply the border layout
to the components. The setLayout() method is used for applying border layout to a
container. You specify the directions for the BorderLayout using the
BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, and BorderLayout.CENTER constants.
You can use the following constructors to create an instance of the BorderLayout class:
BorderLayout() BorderLayout(int h, int v)
![Page 23: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/23.jpg)
Layout Managers
GridLayout Manager The grid layout is the layout that divides the container
into rows and columns. The intersection of a row and a column of the grid
layout is called cell. The GridLayout class of Java enables you to create a
grid layout. All the components in a grid are of the same size. You can use the following constructors to create an
instance of the GridLayout class: • GridLayout()• GridLayout(int r, int c)• GridLayout(int r, int c, int h, int v)
![Page 24: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/24.jpg)
Demo
FlowLayoutBorderLayoutGridLayout
![Page 25: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/25.jpg)
To be continued
To be continued….
![Page 26: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/26.jpg)
References
Java Passion from Sun MicrosystemsJava Tutorial from Sun MicrosystemsCore Java 2 Volume I Fundamentals 7th
EditionJava How to Program 6th EditionJava Swing 2nd Edition
![Page 27: Gui Programming in Java523](https://reader036.fdocuments.us/reader036/viewer/2022081507/577ccd161a28ab9e788b751a/html5/thumbnails/27.jpg)
www.niit-vn.comwww.niit-vn.com/4rum