1 / 26

Chapter 9: Visual Programming Basics

Chapter 9: Visual Programming Basics. Object-Oriented Program Development Using Java: A Class-Centered Approach. Objectives. Event-Based Programming Creating a Swing-Based Window Adding a Window Closing Event Handler Adding a Button Component Common Programming Errors.

ecooks
Download Presentation

Chapter 9: Visual Programming Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach

  2. Objectives • Event-Based Programming • Creating a Swing-Based Window • Adding a Window Closing Event Handler • Adding a Button Component • Common Programming Errors

  3. Event-Based Programming • Event-based programs provide fully functioning GUIs • An event is initiated by a user action • A program must: • Correctly assess which specific event has occurred • Provide the appropriate code to perform an action based on the identified event

  4. Event-Based Programming (continued) • Actions that trigger events include: • Placing the mouse pointer over a button and clicking the left mouse button • Using the TAB key until the desired button is highlighted with a dotted line then pushing the Enter key • Pressing an accelerator key (shortcut key) • The sequence of events in a program are controlled by the user

  5. Event-Based Programming (continued) • Programmer provides: • Code to create GUI • Code to appropriately process events • Java provides a set of objects for coding GUIs: • AWT (abstract window toolkit) • Older GUI components • Swing • Newer GUI components

  6. Swing Components Examples • Top-Level Container – JFrame, JApplet, JWindow • Intermediate Container – JPanel, JInternalFrame • Atomic – JButton, JCheckBox, JTextField

  7. The Event-Based Model • Operating system: • Has total control of computer • Never relinquishes control to any executing programs • Most executing programs spend the majority of their time in a sleep type of mode • When an event occurs: • The operating system passes event information to the appropriate application • Permits the application to take action

  8. Containment Hierarchy • Hierarchy of component placement • Consists of one and only one top-level container • Any number of other intermediate containers • And/or atomic components • JFrame is most commonly used as a top-level container • Heavyweight components (earlier Java term = top-level containers) are responsible for interfacing with the operating system

  9. Containment Hierarchy (continued) • A content pane (next level of hierarchy) is an internal component provided by each top-level container • All of the visible components displayed by a GUI must be placed on a content pane • Menu bar: • Can be optionally added to a top-level container • Placed outside of a content pane

  10. Containment Hierarchy (continued) • Layout manager: • Defines how components are positioned and sized within a container’s content pane • Default placement can always be changed • By explicitly specifying another layout manager • Lightweight components: • Intermediate containers and atomic components • Do not interface with the operating system

  11. JFrame automatically provides a root pane, which in turn contains the content pane. Generally, not concerned with root pane.

  12. Layout managers Each top and intermediate-level containers has a default layout manager that defines the components position and size withing the container’s content pane. You can always change the default layout by specifying another layout manager (chapter 10). The six layout managers currently available are:

  13. Lightweight Components • intermediate containers and atomic components • do not interface directly with the operating system (hence lightweight)

  14. Creating a Swing-Based Window • Two predominant approaches: • Construct a GUI as a separate class using Swing components • Construct a GUI object using Swing components from within the main() method

  15. Creating a Swing-Based Window (continued) • Create JFrame: • JFrame mainFrame = new JFrame("First GUI Window"); • Setting size: • syntax: objectReferenceName.setSize(width, height) • mainFrame.setSize(300,150);

  16. Could also use a single statement jFrame mainFrame = new JFrame(“First Gui Window”);

  17. Creating a Swing-Based Window (continued) • Display JFrame: • Use show() • Or setVisible(true)

  18. import javax.swing.*;public class FirstWindow extends JFrame{ private JFrame mainFrame; public FirstWindow() // a constructor { mainFrame = new JFrame("First GUI Window"); mainFrame.setSize(300,150); mainFrame.show(); } public static void main(String[]args){ new FirstWindow();}} // end of class Provides access to all public and protected methods in the JFrame class

  19. Look and Feel • Refers to: • How a GUI appears on screen • How a user interacts with it • Swing package: • Supports four look and feel types • If no look and feel is specified, the default Java look and feel is used

  20. To implement a specific look and feel, this code must be contained in a try and catch block.

More Related