Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Dept. of Computer Science - SSBN Java Applets Vishnuvardhan.M.
Graphical User Interfaces (Part IV)
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
GUI’s and eventhandlers in java Martin Jagersand.
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.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
CS102--Object Oriented Programming Lecture 18: – The Swing Package Copyright © 2008 Xiaoyan Li.
Java Swing 4 th April 2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Java Programming Chapter 10 Graphical User Interfaces.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
CMSC 202 Swing. Fall Introduction to Swing Swing is a Java package used to create GUIs The Java AWT (Abstract Window Toolkit) package is the original.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Comp 249 Programming Methodology Chapter 17 Swing I Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Basics of GUI Programming Chapter 11 and Chapter 22.
PANELS. Color Class Colour constants are as follows: –Color.black –Color.blue import java.awt.* to access Color Class.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Chapter 14- More Swing, Better looking applications.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
CSC 205 Programming II Lecture 5 AWT - I.
Graphical User Interfaces
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
A First Look at GUI Applications
University of Central Florida COP 3330 Object Oriented Programming
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Programming III
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc. All.
CMSC 202 Swing.
Comp 249 Programming Methodology
Window Interfaces Using Swing Objects Chapter 12
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I

© 2006 Pearson Addison-Wesley. All rights reserved17-2 Swing I: (3/4) Reading: Pages 953 – 968 Outline –Panels –Container Class –Menu Bars, Menus & Menu Items –Nested Menus

© 2006 Pearson Addison-Wesley. All rights reserved17-3 Panels A GUI is often organized in a hierarchical fashion, with containers called panels inside other containers A panel is an object of the JPanel class that serves as a simple container –It is used to group smaller objects into a larger component (the panel) –One of the main functions of a JPanel object is to subdivide a JFrame or other container

© 2006 Pearson Addison-Wesley. All rights reserved17-4 Panels Both a JFrame and each panel in a JFrame can use different layout managers –Additional panels can be added to each panel, and each panel can have its own layout manager –This enables almost any kind of overall layout to be used in a GUI setLayout(new BorderLayout()); JPanel somePanel = new JPanel(); somePanel.setLayout(new FlowLayout()); Note in the following example that panel and button objects are given color using the setBackground method without invoking getContentPane –The getContentPane method is only used when adding color to a JFrame

© 2006 Pearson Addison-Wesley. All rights reserved17-5 Using Panels (Part 1 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-6 Using Panels (Part 2 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-7 Using Panels (Part 3 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-8 Using Panels (Part 4 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-9 Using Panels (Part 5 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-10 Using Panels (Part 6 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-11 Using Panels (Part 7 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-12 Using Panels (Part 8 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-13 The Container Class Any class that is a descendent class of the class Container is considered to be a container class –The Container class is found in the java.awt package, not in the Swing library Any object that belongs to a class derived from the Container class (or its descendents) can have components added to it The classes JFrame and JPanel are descendent classes of the class Container –Therefore they and any of their descendents can serve as a container

© 2006 Pearson Addison-Wesley. All rights reserved17-14 The JComponent Class Any descendent class of the class JComponent is called a component class –Any JComponent object or component can be added to any container class object –Because it is derived from the class Container, a JComponent can also be added to another JComponent

© 2006 Pearson Addison-Wesley. All rights reserved17-15 Objects in a Typical GUI Almost every GUI built using Swing container classes will be made up of three kinds of objects: 1.The container itself, probably a panel or window-like object 2.The components added to the container such as labels, buttons, and panels 3.A layout manager to position the components inside the container

© 2006 Pearson Addison-Wesley. All rights reserved17-16 Hierarchy of Swing and AWT Classes

© 2006 Pearson Addison-Wesley. All rights reserved17-17 Tip: Code a GUI's Look and Actions Separately The task of designing a Swing GUI can be divided into two main subtasks: 1.Designing and coding the appearance of the GUI on the screen 2.Designing and coding the actions performed in response to user actions In particular, it is useful to implement the actionPerformed method as a stub, until the GUI looks the way it should public void actionPerformed(ActionEvent e) {} This philosophy is at the heart of the technique used by the Model-View-Controller pattern

© 2006 Pearson Addison-Wesley. All rights reserved17-18 Menu Bars, Menus, and Menu Items A menu is an object of the class JMenu A choice on a menu is called a menu item, and is an object of the class JMenuItem –A menu can contain any number of menu items –A menu item is identified by the string that labels it, and is displayed in the order to which it was added to the menu The add method is used to add a menu item to a menu in the same way that a component is added to a container object

© 2006 Pearson Addison-Wesley. All rights reserved17-19 Menu Bars, Menus, and Menu Items The following creates a new menu, and then adds a menu item to it JMenu diner = new JMenu("Daily Specials"); JMenuItem lunch = new JMenuItem("Lunch Specials"); lunch.addActionListener(this); diner.add(lunch); –Note that the this parameter has been registered as an action listener for the menu item

© 2006 Pearson Addison-Wesley. All rights reserved17-20 Nested Menus The class JMenu is a descendent of the JMenuItem class –Every JMenu can be a menu item in another menu –Therefore, menus can be nested Menus can be added to other menus in the same way as menu items

© 2006 Pearson Addison-Wesley. All rights reserved17-21 Menu Bars and JFrame A menu bar is a container for menus, typically placed near the top of a windowing interface The add method is used to add a menu to a menu bar in the same way that menu items are added to a menu JMenuBar bar = new JMenuBar(); bar.add(diner); The menu bar can be added to a JFrame in two different ways 1.Using the setJMenuBar method setJMenuBar(bar); 2.Using the add method – which can be used to add a menu bar to a JFrame or any other container

© 2006 Pearson Addison-Wesley. All rights reserved17-22 A GUI with a Menu (Part 1 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-23 A GUI with a Menu (Part 2 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-24 A GUI with a Menu (Part 3 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-25 A GUI with a Menu (Part 4 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-26 A GUI with a Menu (Part 5 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-27 A GUI with a Menu (Part 6 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-28 A GUI with a Menu (Part 7 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved17-29 A GUI with a Menu (Part 8 of 8)