More Swing. CS180 Recitation 12/(04,05)/08

Similar documents
CSE 8B Intro to CS: Java

Chapter 14. More Swing

Window Interfaces Using Swing Objects

Systems Programming Graphical User Interfaces

Swing from A to Z Using Focus in Swing, Part 2. Preface

More Swing. Chapter 14. Chapter 14 1

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Swing from A to Z Some Simple Components. Preface

Summary Chapter 25 GUI Components: Part 2

Chapter 1 GUI Applications

Window Interfaces Using Swing Objects

Building Graphical User Interfaces. Overview

Chapter 13 Lab Advanced GUI Applications

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

CS 251 Intermediate Programming GUIs: Components and Layout

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Introduction to the JAVA UI classes Advanced HCI IAT351

Building Graphical User Interfaces. GUI Principles

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Attempt FOUR questions Marking Scheme Time: 120 mins

A Simple Text Editor Application

Graphical User Interfaces in Java - SWING

1005ICT Object Oriented Programming Lecture Notes

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Programming graphics

Basicsof. JavaGUI and SWING

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Event Driven Programming

Layouts and Components Exam

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

NEW YORK CITY COLLEGE OF TECHNOLOGY/CUNY Computer Systems Technology Department

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

CS 134 Programming Exercise 7:

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Swing Programming Example Number 2

Recursion. Chapter 11

Today. cisc3120-fall2012-parsons-lectiii.3 2

First Name: AITI 2004: Exam 2 July 19, 2004

Advanced Java. Guide 108. Version 1.2

CS180 Recitation. More about Objects and Methods

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Dr. Hikmat A. M. AbdelJaber

Handling Mouse and Keyboard Events

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Chapter 12 Advanced GUIs and Graphics

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CS Exam 1 Review Suggestions

Graphic User Interfaces. - GUI concepts - Swing - AWT

First Name: AITI 2004: Exam 2 July 19, 2004

Programming Language Concepts: Lecture 8

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Inheritance (cont) Abstract Classes

Implementing Graphical User Interfaces

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Part 3: Graphical User Interface (GUI) & Java Applets

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

JLayeredPane. Depth Constants in JLayeredPane

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Graphical User Interface (Part-3) Supplementary Material for CPSC 233

G51PRG: Introduction to Programming Second semester Applets and graphics

Defining Classes and Methods

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Programming Languages and Techniques (CIS120e)

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Chapter 6: Graphical User Interfaces

JAVA NOTES GRAPHICAL USER INTERFACES

Lecture 5: Java Graphics

Packages: Putting Classes Together

SINGLE EVENT HANDLING

Graphical User Interfaces (GUIs)

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

AP CS Unit 11: Graphics and Events

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

COURSE DESCRIPTION. John Lewis and William Loftus; Java: Software Solutions; Addison Wesley

Java Programming Unit 6. Inner Classes. Intro to Apples. Installing Apache Tomcat Server.

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Java: Graphical User Interfaces (GUI)

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

Transcription:

More Swing CS180 Recitation 12/(04,05)/08

Announcements No lecture/labs next week Recitations and evening consulting hours will be held as usual. Debbie's study group on tuesday and office hours on thursday as usual. Attend recitations and study group for final exam help and tips.

Applets Remember to import both java.awt.* and java.awt.event.* You can add icons as ImageIcon objects that represent a picture. There are no restrictions on the size of the ImageIcon.

HTML Language for writing webpages. Command format <Command> </Command> e.g. <head> <title> </title> </head> You can think of these commands as 'containers', just like Java uses JFrames, JApplets and JPanels as containers.

Displaying a Picture HTML documents can contain both text and pictures. Common to place picture files in images/picture_file.

Applets in HTML Applets may be imbedded in HTML documents, listing 14.6a JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 6 6

Applets in HTML Applets may be imbedded in HTML documents listing 14.6b JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 7 7

Browser Output Listing 14.6c JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 8 8

Applets and Security In Slide 7, when embedding an applet, code= AdderApplet.class and code= AdderApplet are equivalent. Applets may not access files or run programs on a remote machine. Although this restricts what applets can do, its important for security reasons. None of us want code someone else wrote to have access to our files! The McGraw-Hill Companies, Inc. Permiss 9

Menus The javax.swing package contains three menurelated 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. http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html The McGraw-Hill Companies, Inc. Permiss 10

Menu Components Edit View Help JMenuBar File Edit View Help JMenu JMenuItem separator The McGraw-Hill Companies, Inc. Permiss 11

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. The McGraw-Hill Companies, Inc. Permiss 12

Inside a Jframe: JMenu filemenu = new JMenu("File"); JMenuItem Menu Creation Example item; //Add New Option item = new JMenuItem("New"); item.addactionlistener( this ); filemenu.add( item );... //add a horizontal separator line filemenu.addseparator();... JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); //Method of JFrame menubar.add(filemenu); The McGraw-Hill Companies, Inc. Permiss 13

http://java.sun.com/docs/books/tutorial/uiswing/layout/box.htm BoxLayout Aligns components in a line Horizontally or vertically Can install a BoxLayout into a Jcomponent comp.setlayout( new BoxLayout( comp,boxlayout.y_axis ); Or create a Box component Box.create[Vertical/Horizontal]Box(); Box.create[Vertical/Horizontal]Strut( int ); Box.create[Vertical/Horizontal]Glue(); See Sun tutorial: The McGraw-Hill Companies, Inc. Permiss 14

BoxLayout Example P1 P2 C1 C4 C2 C5 C3 C6 The McGraw-Hill Companies, Inc. Permiss 15

CardLayout Helps you manage two or more components (usually JPanel instances) that share the same display space. Conceptually, each component a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html The McGraw-Hill Companies, Inc. Permiss 16

CardLayout Example //Where instance variables are declared: J Pa n e l c a r d s ; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JTextField"; //Where the components controlled by the CardLayout are initialized: //Create t h e "cards". JPanel card1 = new JPanel();... JPanel card2 = new JPanel();... //Create the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add(card1, BUTTONPANEL); cards.add(card2, TEXTPANEL); The McGraw-Hill Companies, Inc. Permiss 17

Inner Class A class defined within a class Generally used as a private utility class that does not need to be seen by others Operates as a sub-part of the outer class Can have constructors, instance variables, and methods just like a regular class The McGraw-Hill Companies, Inc. Permiss 18

Advantages of Inner Classes They can make the outer class more self-contained. The methods in the inner class have access to all the instance variables and methods of the outer class, including the private methods and variables. Inner classes are used frequently as listeners to handle events fired by the outer class or by a component of the outer class. The name of the inner class is local to the class in which it is defined, making it possible to have another class with the same name defined outside the class in which it is defined. The McGraw-Hill Companies, Inc. Permiss 19

Inner Class example class Outer { p r i v a t e c l a s s I n n e r { Object data;... private void method() {... }... } } void dosomething() { Inner i = new Inner(); i. m e t h o d ( ) ;... }... The McGraw-Hill Companies, Inc. Permiss 20

Buttons, Menus, and Abstract Buttons 21

WindowListener Receives Window events The class that is interested in processing a window event implements this interface. The listener object created from that class is then registered with a Window using the window's addwindowlistener method. 22

WindowListener Interface windowactivated( WindowEvent e ); windowclosed( WindowEvent e ); windowclosing( WindowEvent e ); windowdeactivated( WindowEvent e ); windowdeiconified( WindowEvent e ); windowiconified( WindowEvent e ); windowopened( WindowEvent e ); 23

WindowAdapter Implements WindowListener All methods are empty Extend this class to create a WindowEvent listener and override the methods for the events of interest. Convenient 24

JScrollPane Examples http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html 25