Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
Dept. of Computer Science - SSBN Java Applets Vishnuvardhan.M.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
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.
1 Chapter 4 l Menus l Making GUIs Pretty (and More Functional) l Box Containers and Box Layout Managers l More on Events and Listeners l Another Look at.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Happy Tax Day! l Project 8 due on Thursday.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Chapter 131 Applets and HTML Chapter Reminders Project 8 due Dec 10:30 pm Project 6 regrades due by midnight tonight –Submit all files (including.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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 Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Menus l Making GUIs Pretty (and More Functional) l Box Containers.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
IM103 week 11 Part 2 P532 Case Study part 2: the Airport GUI Learning objectives By the end of this lecture you should be able to:  use the JTabbedPane.
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.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and.
Review_6 AWT, Swing, ActionListener, and Graphics.
Chapter 131 Applets and HTML Chapter Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Chapter 141 More Swing Chapter Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Developing GUIs With the Eclipse Visual Editor, Swing/AWT Edition David Gallardo.
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
Chapter 14 Applets. Applets: Introduction Java programs are divided into two main categories, applets and applications An application is an ordinary Java.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Java Applets.
A First Look at GUI Applications
Advanced User Interfaces
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation on Friday (review for Final Exam) l Project 8 due Thursday April 28 l Final Exam »Tuesday, May 3: 1:00 - 3:00 PM (Phys 112) »Cumulative (weighted towards recent material) »40 MC (2 points each), 6 programming (20 points each) = 200 points total

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Chapter 13 l HTML l Applets Applets and HTML

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Gotcha : Using an Old Web Browser l Updates to browsers are usually much later than updates to core Java language. »Web browsers don't use the same Java interpreter that runs Java applications. l Test your applets in different browsers and in different versions of browsers. »Can't expect everyone to have most recent browser. »Might need to use older Applet class if you want your applet to run in older browsers.

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 4 The Older Applet Class Older versions of web browsers can't run applets that use JApplet and other Swing classes. Using the old Applet class instead of JApplet will allow your applet to run in more browsers. To convert a JApplet program to an Applet program: »Replace: import javax.swing.*; with import java.applet.*; »Replace Swing classes that start with J ( JButton, JPanel, etc.) with AWT classes that don't start with J ( Button, Panel, etc.) »Remove references to the content pane of the applet.

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Applets and Security l Applets are someone else's program running on your computer. l Security issues: »viruses »reading confidential information »corrupting operating system l Security features of applets to help avoid these problems: »Applets cannot run any programs on your computer. »Applets cannot read or write to files on your computer (unless the applet originated on your computer).

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box Containers and Box Layout Managers l More on Events and Listeners l Another Look at the Swing Class Hierarchy More Swing

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 7 Menus l Three Swing classes used to put a menu in a program: »JMenuBar »JMenu »JMenuItem l Menu items behave in the same way as buttons AbstractButton JButton JMenu JMenuItem

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 8 A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m);... JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Create a menu Create a menu item A menu item uses an action listener the same way a button does.

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 9 A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m);... JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Each menu item is added to the menu. The menu is added to the menu bar. One way to add a menu bar to a JFrame

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Nested Menus JMenu is a descendant of JMenuItem Every JMenu object is also a JMenuItem A JMenu can be a menu item in another menu l This allows nested menus l Clicking on a nested menu shows the items in the nested menu and allows them to be selected. AbstractButton JButton JMenu JMenuItem

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 11 The JScrollPane Class for Scroll Bars l A view port is used when not all information can be displayed on screen at once. l Scroll bars move a view port around to show different parts of the information. JScrollPane is a class that can provide a view port with scroll bars. An example using JScrollPane with a JTextArea called theText and a JPanel called textPanel : JScrollPane scrolledText = new JScrollPane(theText); textPanel.add(scrolledText);

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 12 JScrollPane Example JPanel textPanel = new Jpanel(); theText = new JTextArea(10, 40); JScrollPane scrolledText = new JScrollPane(theText); scrolledText.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrolledText.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); textPanel.add(scrolledText); contentPane.add(textPanel);

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 13 The CardLayout Manager l Allows a set of views (components) to choose among »Only one view is visible at a time. »Can go through views in order or jump to any view. »Often the components added to a CardLayout will be panels. Each component added to a CardLayout has a string associated with it that works like a name: deckPanel.add("start", startCardPanel); »The string can be used later to display that component (or view): dealer.show(deckPanel, "start"); l Need reference to layout manager to change views, so do not use anonymous object: deckPanel.setLayout(new CardLayout()); legal but useless

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 14 CardLayoutDemo deckPanel = new JPanel(); dealer = new CardLayout(); deckPanel.setLayout(dealer);... deckPanel.add("start", startCardPanel);... deckPanel.add("green", greenCardPanel);... deckPanel.add("red", redCardPanel);... dealer.show(deckPanel, "red");... dealer.next(deckPanel); if redCardPanel is currently displayed, will show startCardPanel Only one of these three panels will be visible at a time. will show redCardPanel

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Changing Components l A program can add or remove components after a GUI has been displayed, but that is beyond the scope of the book. l Making components visible or not visible gives a similar effect. The setVisible method is used in the VisibleDemo program to make only one of the red and green labels visible at a time. (code on next slide)

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Changing Components public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(“Red”)) { colorPanel.setBackground(Color.red); stopLabel.setVisible(false); goLabel.setVisible(true); validate(); }... } There is similar code for when the Green button is pressed, which turns the background green and hides the go label. The actionPerformed method from the VisibleDemo program Visibility changes won’t occur until the validate method is called.

Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Summary You can add icons to JButtons, JLabels, and JMenuItems. A JMenuBar can be added to a JFrame with the method setJMenuBar or with the usual add method. Both buttons and menu items fire action events and so should have an ActionListener registered with them. You can use the class JScrollPane to add scroll bars to a text area. You can define a window listener class by having it implement the WindowListener interface. If you want a close-button to do something other than close the window, you must use SetDefaultCloseOperation. If you change the visibility of a component you should use the validate method to update the GUI.