GUI Programming In Java


Published on

GUI Programming In Java

Published in: Technology, Education
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • GUI Programming In Java

    1. 1. GUI Programming in Java Presented by Thanh Pham [email_address] 06/2007 B070038 – NIIT Quang Trung Because Learning Never Stop!
    2. 2. Contents Basic Concepts 1 AWT and Swing Control Components 2 Layout Manager 3 Event Handling 4 Other References 5
    3. 3. Basic Concepts Basic Concepts Exercises Demos AWT vs. Swing GUI?
    4. 4. GUI? <ul><li>Graphical User Interface (pronounced &quot;GOO-ee“). </li></ul><ul><li>Presents a user-friendly mechanism for interacting with an application. </li></ul><ul><li>Users can learn how to use an app quickly and use it more productively. </li></ul><ul><ul><li>Ex: Internet Explorer, MS Word, JCreator… </li></ul></ul><ul><li>GUIs are built from GUI components (controls or widgets-window gadget). </li></ul><ul><li>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. </li></ul>
    5. 5. Abstract Window Toolkit (AWT) vs. Swing <ul><li>Similarities: </li></ul><ul><ul><li>Tools provided by Java for developing interactive GUI applications </li></ul></ul><ul><ul><li>Provides GUI components that can be used in creating Java applications and applets </li></ul></ul>
    6. 6. Abstract Window Toolkit (AWT) vs. Swing <ul><li>AWT (Abstract Window Toolkit) </li></ul><ul><ul><li>Some AWT components use native code </li></ul></ul><ul><ul><li>Platform-dependent </li></ul></ul><ul><ul><li>Ensure that the look and feel of an application run on different machines be comparable </li></ul></ul><ul><li>Swing </li></ul><ul><ul><li>Written entirely using the Java programming language </li></ul></ul><ul><ul><li>Platform-independent </li></ul></ul><ul><ul><li>Ensures applications deployed across different platforms have the same appearance </li></ul></ul><ul><ul><li>Built around a number of APIs that implement various parts of the AWT </li></ul></ul><ul><ul><ul><li>Can be used with AWT </li></ul></ul></ul>
    7. 7. Demo <ul><li>MessageBox </li></ul><ul><li>InputBox </li></ul><ul><li>Create Frame (AWT) </li></ul><ul><li>Create Frame (Swing) </li></ul><ul><li>CenterFrame </li></ul>
    8. 8. Exercises <ul><li>Display two input-boxes that accepts two numbers then shows the result. </li></ul><ul><li>Accepts three numbers, displays the largest, smallest. </li></ul><ul><ul><li>(Assume that all input values are valid numbers) </li></ul></ul><ul><li>Create a simple frame </li></ul>
    9. 9. AWT and Swing Control Components AWT and Swing Control Components Exercises Demos Swing AWT
    10. 10. AWT Control Components <ul><li>An AWT control is a component that enables end users to interact with applications created in Java. </li></ul><ul><li>All AWT controls in Java are subclasses of the Component class. </li></ul><ul><li>The Component class provides the add() method to add AWT components to containers, such as an applet or a window. </li></ul>
    11. 11. AWT Control Components <ul><li>TextField </li></ul><ul><li>TextArea </li></ul><ul><li>Button </li></ul><ul><li>List </li></ul><ul><li>CheckBox </li></ul><ul><li>Choice </li></ul><ul><li>Labels </li></ul>
    12. 12. Demo <ul><li>FrameWithControl </li></ul>
    13. 13. Swing Control Components <ul><li>Swing components contain the Pluggable Look and Feel (PL&F) feature that allows applications to have the same behavior on various platforms. </li></ul><ul><li>Identifying the Swing Component Class Hierarchy </li></ul><ul><ul><li>The JComponent class is the root of the Swing hierarchy, which is an extension of the AWT container class. </li></ul></ul><ul><ul><li>The class hierarchy of the Swing components is categorized into: </li></ul></ul><ul><ul><ul><li>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. </li></ul></ul></ul><ul><ul><ul><li>Intermediate-level Swing Containers: Placed on the top-level containers and contains atomic components. </li></ul></ul></ul><ul><ul><ul><li>Atomic Components: Placed on the intermediate-level swing containers. Atomic components are used to accept input from a user. </li></ul></ul></ul>
    14. 14. Swing Control Components <ul><li>Using the Top-level Swing Containers </li></ul><ul><ul><li>JApplet </li></ul></ul><ul><ul><ul><li>The JApplet class is an extension of the AWT applet class. </li></ul></ul></ul><ul><ul><ul><li>The Swing components that contain an applet need to extend the JApplet class. </li></ul></ul></ul><ul><ul><ul><li>The JApplet() constructor enables you to create a swing applet instance when you create an instance of the JApplet class. </li></ul></ul></ul><ul><ul><li>JFrame: </li></ul></ul><ul><ul><ul><li>The JFrame class is an extension of the AWT Frame class. </li></ul></ul></ul><ul><ul><ul><li>You cannot add components directly to JFrame. </li></ul></ul></ul>
    15. 15. Swing Control Components <ul><li>Using the Top-level Swing Containers (Cont.) </li></ul><ul><ul><li>JDialog </li></ul></ul><ul><ul><ul><li>The JDialog class is an extension of the AWT java.awt.Dialog class. </li></ul></ul></ul><ul><li>Using the Intermediate Level Swing Containers </li></ul><ul><ul><li>JPanel </li></ul></ul><ul><ul><ul><li>JPanel class is an extension of the JComponent class that provides a replacement for the AWT Panel class. </li></ul></ul></ul><ul><ul><ul><li>You create a panel and add various components to it. </li></ul></ul></ul><ul><ul><ul><li>The panel is further added to the content pane, which represents the display area of a window, a dialog, or a frame. </li></ul></ul></ul><ul><ul><ul><li>JPanel supports all layout managers of AWT. </li></ul></ul></ul><ul><ul><ul><li>By default, JPanel applies the flow layout manager. </li></ul></ul></ul>
    16. 16. Swing Control Components <ul><li>Using the Intermediate Level Swing Containers (Contd.) </li></ul><ul><ul><li>JTabbedPane: </li></ul></ul><ul><ul><ul><li>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. </li></ul></ul></ul><ul><ul><ul><li>Tabs are added to the JTabbedPane object by using the addTab() method. </li></ul></ul></ul><ul><ul><ul><li>The JTabbedPane class enables you to add multiple components but it displays only a single component at a time. </li></ul></ul></ul><ul><li>Using the Atomic Components </li></ul><ul><ul><li>JButton </li></ul></ul><ul><ul><li>JTextField </li></ul></ul><ul><ul><li>JCheckBox </li></ul></ul><ul><ul><li>JComboBox </li></ul></ul><ul><ul><li>JLabel </li></ul></ul><ul><ul><li>JRadioButton </li></ul></ul>
    17. 17. Demo <ul><li>JFrameWithControl </li></ul>
    18. 18. Exercises
    19. 19. Layout Managers Layout Managers Demo Grid Layout Border Layout Flow Layout
    20. 20. Layout Managers <ul><li>The layout managers are used to position the components, such as an applet, a panel, or a frame in a container. </li></ul><ul><li>The layout managers implement the java.awt.LayoutManager interface. </li></ul><ul><li>A layout manager is an instance of the LayoutManager interface in Java. </li></ul><ul><li>You can use the following method to apply the desired layout to the components: </li></ul><ul><ul><li>void setLayout(layoutManager obj) </li></ul></ul><ul><ul><li>In the preceding syntax, obj is the reference to the desired layout manager. </li></ul></ul><ul><ul><li>Java has various predefined classes of layout managers. </li></ul></ul><ul><ul><li>All layout managers make use of the setLayout() method to set the layout of a container. </li></ul></ul><ul><ul><li>If the setLayout() method is not used, then the default layout of the container is set. </li></ul></ul>
    21. 21. Layout Managers <ul><li>The different types of layout managers are: </li></ul><ul><ul><li>FlowLayout Manager </li></ul></ul><ul><ul><ul><li>The flow layout is the default layout manager used for the Applet class. </li></ul></ul></ul><ul><ul><ul><li>In the flow layout manager, the components are placed in a container window in a sequence one after the other in rows. </li></ul></ul></ul><ul><ul><ul><li>Java provides the FlowLayout class to apply flow layout to the various components that you are inserting in an applet. </li></ul></ul></ul><ul><ul><ul><li>You can use the following constructors to create an instance of the FlowLayout class: </li></ul></ul></ul><ul><ul><ul><ul><li>FlowLayout() </li></ul></ul></ul></ul><ul><ul><ul><ul><li>FlowLayout(int align) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>FlowLayout(int align, int hgap,int vgap) </li></ul></ul></ul></ul>
    22. 22. Layout Managers <ul><li>BorderLayout Manager </li></ul><ul><ul><li>BorderLayout is the default layout of the Frame class. </li></ul></ul><ul><ul><li>The BorderLayout layout manager divides the container into north, south, east, west, and centre regions. </li></ul></ul><ul><ul><li>You can place five components or controls in each part. </li></ul></ul><ul><ul><li>Java provides the BorderLayout class to apply the border layout to the components. </li></ul></ul><ul><ul><li>The setLayout() method is used for applying border layout to a container. </li></ul></ul><ul><ul><li>You specify the directions for the BorderLayout using the BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, and BorderLayout.CENTER constants. </li></ul></ul><ul><ul><li>You can use the following constructors to create an instance of the BorderLayout class: </li></ul></ul><ul><ul><li>BorderLayout() </li></ul></ul><ul><ul><li>BorderLayout(int h, int v) </li></ul></ul>
    23. 23. Layout Managers <ul><li>GridLayout Manager </li></ul><ul><ul><li>The grid layout is the layout that divides the container into rows and columns. </li></ul></ul><ul><ul><li>The intersection of a row and a column of the grid layout is called cell. </li></ul></ul><ul><ul><li>The GridLayout class of Java enables you to create a grid layout. </li></ul></ul><ul><ul><li>All the components in a grid are of the same size. </li></ul></ul><ul><ul><li>You can use the following constructors to create an instance of the GridLayout class: </li></ul></ul><ul><ul><ul><li>GridLayout() </li></ul></ul></ul><ul><ul><ul><li>GridLayout(int r, int c) </li></ul></ul></ul><ul><ul><ul><li>GridLayout(int r, int c, int h, int v) </li></ul></ul></ul>
    24. 24. Demo <ul><li>FlowLayout </li></ul><ul><li>BorderLayout </li></ul><ul><li>GridLayout </li></ul>
    25. 25. To be continued <ul><li>To be continued…. </li></ul>
    26. 26. References <ul><li>Java Passion from Sun Microsystems </li></ul><ul><li>Java Tutorial from Sun Microsystems </li></ul><ul><li>Core Java 2 Volume I Fundamentals 7 th Edition </li></ul><ul><li>Java How to Program 6 th Edition </li></ul><ul><li>Java Swing 2 nd Edition </li></ul>
    27. 27. Thank You !