Chapter 7-3 (Book Chapter 14)

Slides:



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

G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Introduction to Java 2 Programming
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
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
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 
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
©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.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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.
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.
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.
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.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
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.
Graphical User Interfaces
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.
Swing JComponents.
Advanced User Interfaces
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Timer class and inner classes
Containers and Components
Creating Graphical User Interfaces
Graphical User Interface
Chapter 7-2 (Book Chapter 14)
Chapter 7 (Book Chapter 14)
Chapter 7-2 (Book Chapter 14)
GUI and Event-Driven Programming
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Chapter 7-3 (Book Chapter 14) Introduction to OOP with Java 4th Ed, C. Thomas Wu Chapter 7-3 (Book Chapter 14) GUI and Event-Driven Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. © The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu 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. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu 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. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu FlowLayout Sample This shows the placement of five buttons by using FlowLayout. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

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.

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

Intro to OOP with Java, C. Thomas Wu 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. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu BorderLayout Sample contentPane.setLayout(new BorderLayout()); 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. ©The McGraw-Hill Companies, Inc.

BorderLayout ... 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.

Intro to OOP with Java, C. Thomas Wu 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. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu GridLayout Sample ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

GridLayout ... 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); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

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.

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); 125 30 70 80 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Other Common GUI Components Intro to OOP with Java, C. Thomas Wu 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 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

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.

Other Common GUI Components 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.

Intro to OOP with Java, C. Thomas Wu 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. Almost all nontrivial GUI programs support menus. By using these three menu-related classes, we can easily add menus to our Java programs. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu Menu Components Edit View Help JMenuBar Edit View Help File JMenu The diagram shows how the menu-related objects correspond to the actual menus. JMenuItem separator ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Sequence for Creating Menus Intro to OOP with Java, C. Thomas Wu Sequence for Creating Menus Create a JMenuBar object and attach it to a frame. Create a JMenu object. Create JMenuItem objects and add them to the JMenu object. Attach the JMenu object to the JMenuBar object. This is not the only valid sequence. Other sequences are possible. We list this sequence as one possible sequence you can follow in creating menus. see Ch14MenueFrame.java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu 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 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Java Online Tutorial More information about Java GUI can be found here: http://docs.oracle.com/javase/tutorial/uiswing/index.html General Java information can be found here http://docs.oracle.com/javase/tutorial/index.html ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.