©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 1 Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.

Slides:



Advertisements
Similar presentations
Chapter 14 Advanced GUI ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 : Advanced GUI  Mouse Events  JPanels  Layout Managers 
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Chapter 14 Advanced GUI. Topics Mouse Events –MouseListener –MouseMotionListener JPanels Layout Managers –Flow Layout –Border Layout –GridLayout –BoxLayout.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 14 GUI and Event-Driven Programming.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and event-driven programming An introduction.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CSE 219 Computer Science III Graphical User Interface.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Graphical User Interface in Java
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Object-Oriented Software Engineering
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
3461 Laying Out Components Interior Design for GUIs.
DCS2133 Object Oriented Programming Graphical User Interface & Event-Driven Programming.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Graphical User Interfaces Tonga Institute of Higher Education.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 7 A First Look at GUI Applications Layout Managers.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout.
Advanced User Interfaces
Modern Programming Language Java
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Creating Graphical User Interfaces
Graphical User Interface
Chapter 7-3 (Book Chapter 14)
GUI and Event-Driven Programming
Advanced GUIs and Graphics
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Layout Managers The layout manager determines how the GUI components are added to the container (such as the content pane of a frame) Among the many different layout managers, the common ones are –FlowLayout (see Ch14FlowLayoutSample.java) –BorderLayout (see Ch14BorderLayoutSample.java) –GridLayout (see Ch14GridLayoutSample.java)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter FlowLayout In using this layout, GUI components are placed in left-to-right order. –When the component does not fit on the same line, left- to-right placement continues on the next line. As a default, components on each line are centered, but you can change it to left or right alignment. When the frame containing the component is resized, the placement of components is adjusted accordingly.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter FlowLayout Sample This shows the placement of five buttons by using FlowLayout.

FlowLayout... contentPane = getContentPane( ); contentPane.setBackground( Color.WHITE ); contentPane.setLayout(new FlowLayout()); button1 = new JButton("button 1"); button2 = new JButton("button 2"); button3 = new JButton("button 3"); button4 = new JButton("button 4"); button5 = new JButton("button 5"); contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); contentPane.add(button4); contentPane.add(button5);... ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter To change the center alignment: contentPane.setLayout(new FlowLayout(FlowLayout.LEFT)); contentPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); FlowLayout FlowLayout - Left Alignment FlowLayout - Right Alignment

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter BorderLayout This layout manager divides the container into five regions: center, north, south, east, and west. If the window is enlarged, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space. Not all regions have to be occupied.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter BorderLayout Sample

BorderLayout ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter contentPane = getContentPane( ); contentPane.setBackground( Color.WHITE ); contentPane.setLayout(new BorderLayout()); button1 = new JButton("button 1"); button2 = new JButton("button 2"); button3 = new JButton("button 3"); button4 = new JButton("button 4"); button5 = new JButton("button 5"); contentPane.add(button1, BorderLayout.NORTH); contentPane.add(button2, BorderLayout.SOUTH); contentPane.add(button3, BorderLayout.EAST); contentPane.add(button4, BorderLayout.WEST); contentPane.add(button5, BorderLayout.CENTER);...

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter GridLayout This layout manager placesGUI components on equal-size N by M grids. Components are placed in top-to-bottom, left-to- right order. The number of rows and columns remains the same after the frame is resized, but the width and height of each region will change.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter GridLayout Sample

GridLayout ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter contentPane = getContentPane( ); contentPane.setBackground( Color.WHITE ); contentPane.setLayout(new GridLayout(2,3)); button1 = new JButton("button 1"); button2 = new JButton("button 2"); button3 = new JButton("button 3"); button4 = new JButton("button 4"); button5 = new JButton("button 5"); contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); contentPane.add(button4); contentPane.add(button5);...

Absolute Positioning Here we do not use any layout manager. We place the GUI objects on the frame’s content pane by explicitly specifying their position and size. This is called absolute positioning ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

Absolute Positioning... private static final int BUTTON_WIDTH = 80; private static final int BUTTON_HEIGHT = 30; contentPane.setLayout( null ); contentPane.setBackground( Color.WHITE ); //create and place a button on the frame's content pane okButton = new JButton("OK"); okButton.setBounds(70, 125, BUTTON_WIDTH, BUTTON_HEIGHT); contentPane.add(okButton); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Other Common GUI Components JCheckBox Useful in representing a collection of binary (yes/no, true/false) options. A JCheckBox object generates action events (like a JButton), but also generates item events, when the state of a check-box button changes (selected or deselected). –see Ch14JCheckBoxSample1.java and Ch14JCheckBoxSample2.java

Other Common GUI Components JRadioButton The radio button is similar to the check-box, but only one button can be selected from the group. When you select a new item, the currently selected radio button will get deselected. Thus, the JRadioButton must be added to a group. A JRadioButton generates both action events and item events. –see Ch14JRadioButtonSample.java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

Other Common GUI Components ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter JComboBox (drop-down list) Similar to JRadioButton, but the choices are presented to the user in a form of a drop-down list –See Ch14JComboBoxSample.java Jlist Used to display a list of items You can highlight one or more selection –see Ch14JListSample.java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Menus The javax.swing package contains three menu-related classes: JMenuBar, JMenu, and JMenuItem. JMenuBar is a bar where the menus are placed. There is one menu bar per frame. JMenu (such as File or Edit) is a group of menu choices. JMenuBar may include many JMenu objects. JMenuItem (such as Copy, Cut, or Paste) is an individual menu choice in a JMenu object. Only the JMenuItem objects generate events.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Menu Components EditViewHelp JMenuBar EditViewHelpFile JMenu JMenuItem separator

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Sequence for Creating Menus 1.Create a JMenuBar object and attach it to a frame. 2.Create a JMenu object. 3.Create JMenuItem objects and add them to the JMenu object. 4.Attach the JMenu object to the JMenuBar object. –see Ch14MenueFrame.java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Handling Mouse Events Mouse events include such user interactions as –moving the mouse –dragging the mouse (moving the mouse while the mouse button is being pressed) –clicking the mouse buttons. The MouseListener interface handles mouse button –mouseClicked, mouseEntered, mouseExited, mousePressed, and mouseReleased The MouseMotionListener interface handles mouse movement –mouseDragged and mouseMoved. - See Ch14TrackMouseFrame.java and Ch14SketchPad.java

Java Online Tutorial More information about Java GUI can be found here: tml General Java information can be found here ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter