Chapter 9: Applets Jim Burns Fall 2011. Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
COMP 14 Introduction to Programming Miguel A. Otaduy June 8, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 21, 2005.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
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.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Layout Managers Arranges and lays out the GUI components on a container.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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 Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
“Form Ever Follows Function” Louis Henri Sullivan
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
UNIT-5.
Java Applets.
GUI building with the AWT
Advanced GUIs and Graphics
Presentation transcript:

Chapter 9: Applets Jim Burns Fall 2011

Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host an applet Understand where applets fit in the class hierarchy Understand where applets fit in the class hierarchy Create a Japplet containing an init() method Create a Japplet containing an init() method Change a Jlabel’s font Change a Jlabel’s font Add JTextField and JButton components to a JApplet Add JTextField and JButton components to a JApplet

Outline

Introducing Applets Apps are stand-alone applications that occasionally get compiled and linked into machine code, frequently compiled into bytecodes Apps are stand-alone applications that occasionally get compiled and linked into machine code, frequently compiled into bytecodes Applets are Java programs that get interpreted by a Java bytecode interpreter Applets are Java programs that get interpreted by a Java bytecode interpreter Applets run under the control of a larger program, such as a web page Applets run under the control of a larger program, such as a web page

Applets Displayed as a rectangular area Displayed as a rectangular area Contains any number of components, such as buttons, text fields, pictures, etc. Contains any number of components, such as buttons, text fields, pictures, etc. Can respond to user-initiated events, such as mouse clicks or keyboard presses Can respond to user-initiated events, such as mouse clicks or keyboard presses Many of an applet’s behaviors come from a Java class named JApplet Many of an applet’s behaviors come from a Java class named JApplet

Writing an applet requires five major steps: Setting up a layout for the applet Setting up a layout for the applet Creating components and adding them to the applet Creating components and adding them to the applet Arranging for listeners to listen for events generated by users who interact with the applet’s components Arranging for listeners to listen for events generated by users who interact with the applet’s components Writing methods to respond when the events occur Writing methods to respond when the events occur Writing a document to host the applet Writing a document to host the applet

Writing an HTML Document to Host an Applet The applet is usually run within an HTML document (i.e. a web page) The applet is usually run within an HTML document (i.e. a web page) The applet can also be run from within an applet viewer The applet can also be run from within an applet viewer HTML (Hypertext Markup Language) is a simple language for creating web pages HTML (Hypertext Markup Language) is a simple language for creating web pages

When you create an applet, you do the following… Write the applet in Java and save it with a.java file extension, just as when you write a Java application Write the applet in Java and save it with a.java file extension, just as when you write a Java application Compile the applet into bytecode using the javoc command, just as when you write a Java application Compile the applet into bytecode using the javoc command, just as when you write a Java application Write an HTML document that includes a statement to call your compiled Java applet Write an HTML document that includes a statement to call your compiled Java applet Load the HTML document into a Web browser, or run the Applet Viewer program, which in turn uses the HTML document Load the HTML document into a Web browser, or run the Applet Viewer program, which in turn uses the HTML document

Writing HTML You need to learn only two pairs of HTML commands, called tags You need to learn only two pairs of HTML commands, called tags The tag that begins every HTML document is The tag that begins every HTML document is The tag that ends every HTML document is The tag that ends every HTML document is In between, you need In between, you need

The complete HTML doc <html> </html> Here, width defines the width of the applet in pixels, while height defines the height of the applet in pixels Here, width defines the width of the applet in pixels, while height defines the height of the applet in pixels 400x300 is about ¼ of the total screen size 400x300 is about ¼ of the total screen size

Running an Applet Use the web browser Use the web browser Use the Applet Viewer Use the Applet Viewer The appletviewer command is part of your SDK The appletviewer command is part of your SDK Simply type appletviewer at the command line followed by the full HTML filename. Simply type appletviewer at the command line followed by the full HTML filename. –When you press return the viewer window opens and displays the applet

Understanding where Applets fit in the Class Hierarchy To write an applet, you must… To write an applet, you must… –Include necessary import statements  Import javax.swing.JApplet;  JApplet is a swing class from which you can instantiate an applet  Swing components are UI elements such as dialog boxes and buttons –Learn to use some new user interfaces, such as buttons, text fields and applet methods –Learn to use the keyword extends

The class hierarchy Java.lang.Object Java.lang.Object –Java.awt.Component  Java.awt.Container –Java.awt.Panel  Java.applet.Applet  Javax.swing.Japplet From this hierarchy, we learn that every container is a component, but not every component is a container From this hierarchy, we learn that every container is a component, but not every component is a container

The JLabel Hierarchy Java.lang.Object Java.lang.Object –Java.awt.Component  Java.awt.Container –Javax.swing.JComponent  Javax.swing.JLabel

Some Components Labels – JLabel() Labels – JLabel() –Writes text on the form Textboxes –JTextField Textboxes –JTextField –Places a textbox on the form (single line of text) –A textbox is a box the user can write text in –This text can be captured and stored Font – Font() Font – Font() –Allows you to adjust font (typeface), style and point size in a Jlabel Button – JButton() Button – JButton()

Available constructors for Jlabel class Jlabel() creates a Jlabel instance with no image and with an empty string for the title Jlabel() creates a Jlabel instance with no image and with an empty string for the title Jlabel(Icon image) creates a Jlabel instance with the specified image Jlabel(Icon image) creates a Jlabel instance with the specified image Jlabel(Icon image, int horizontalAlignment) creates a Jlabel instance with the specified image and horizontal alignment Jlabel(Icon image, int horizontalAlignment) creates a Jlabel instance with the specified image and horizontal alignment Jlabel(String text) creates a Jlabel instance with the specified text Jlabel(String text) creates a Jlabel instance with the specified text

import javax.swing.*; import java.awt.*; Public class Jhello extends Japplet { Container con = getContentPane(); JLabel greeting = new Jlabel(“Hello. Who are you?”); public void init() {con.add(greeting);}}

Creating a JApplet Containing an init() method In applications your main method gets called by the system, and the main method calls other methods In applications your main method gets called by the system, and the main method calls other methods In applets, four methods are included in every applet: In applets, four methods are included in every applet: –public void init() –public void start() –public void stop() –public void destroy()

No main method in a Java Applet If you fail to provide an init(), a start(), a stop(), or a destroy() If you fail to provide an init(), a start(), a stop(), or a destroy() –Java creates them for you –But Java-created methods are empty—you must put something into at least one of them

Changing a JLabel’s Font YOU have to use a class called ‘Font’ YOU have to use a class called ‘Font’ Font headlineFont = new Font(“helvetica”, font.BOLD, 36); Font headlineFont = new Font(“helvetica”, font.BOLD, 36);...greeting.setFont(headlineFont);

import javax.swing.*; import java.awt.*; Public class Jhello extends Japplet { Container con = getContentPane(); JLabel greeting = new Jlabel(“Hello. Who are you?”); Font headlineFont = new Font(“Helvetica”, Font.BOLD, 36); public void init() { greeting.setFont(headlineFont); con.add(greeting);}}

Adding JTextField and JButton components to a JApplet JTextField is a component into which a user can type a single line of text data JTextField is a component into which a user can type a single line of text data JTextField answer = new JTextField(10); JTextField answer = new JTextField(10); –Will display 10 characters To add the JTextField named answer to the Container named con within the JApplet, you write: To add the JTextField named answer to the Container named con within the JApplet, you write: con.add(answer); con.add(answer);

JTextfield Constructors public JTextField() – constructs a new JTextfield public JTextField(int columns) constructs a new, empty JTextField with the specified number of columns public JTextField(String text) constructs a new JTextField initialized by the specified text public JTextField(String text, int columns) does it all

Other methods for use with JTextFields setText() method allows you to change the text in a JTextField that has already been created, as in – –answer.setText(“Thank you”); – –Answer.setText(“”); -- clears out whatever getText() method allows you to retrieve the String of text in a JTextField, as in – –String whatDidTheySay = answer.getText(); – –answer.requestfocus(); causes the cursor to appear in the JTextField—only one component can have focus at any given time

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JHello9 extends JApplet implements ActionListener { JLabel greeting = new JLabel("Hello. Who are you?"); Font headlineFont = new Font("Helvetica", Font.BOLD, 36); JTextField answer = new JTextField(10); JButton pressMe = new JButton("Press me"); JLabel personalGreeting = new JLabel(""); Container con = getContentPane(); public void init() { greeting.setFont(headlineFont); personalGreeting.setFont(headlineFont); con.add(greeting); con.add(answer); con.add(pressMe); con.setLayout(new FlowLayout()); pressMe.addActionListener(this); answer.addActionListener(this); } public void actionPerformed(ActionEvent e) { remove(pressMe); remove(answer); String name = answer.getText(); personalGreeting.setText("Hello, " + name); con.add(personalGreeting); validate(); personalGreeting.setLocation(10,150); }

Adding JButtons JButton creates a button that the user can click on to make a selection—five constructors JButton creates a button that the user can click on to make a selection—five constructors –public JButton() –public JButton(Icon icon) –public JButton(String text) –public JButton(String text, Icon icon) –public JButton(Action a)

To create a JButton with the label “Press when ready”, you write: JButton readyJButton = new JButton(“Press when ready”); To add the JButton TO A CONTAINER named con in an applet, you write: Con.add(readyButton); To change a JButton’s label with the setLabel() method, you use readyJButton.setLabel(“Don’t press me again!”);

Adding Multiple Components to a JApplet The following code places one component on top of the other, completely hiding it The following code places one component on top of the other, completely hiding it In this case, the JTextField gets placed on top of the JLabel, completely obscuring it In this case, the JTextField gets placed on top of the JLabel, completely obscuring it

import javax.swing.*; import java.awt.*; public class JHello3 extends JApplet { JLabel greeting = new JLabel("Hello. Who are you?"); Font headlineFont = new Font("Helvetica", Font.BOLD, 36); JTextField answer = new JTextField(10); public void init() { Container con = getContentPane(); greeting.setFont(headlineFont); con.add(greeting); con.add(answer); }

The following code … Does just the opposite, placing the JLabel on top of the JTextField

import javax.swing.*; import java.awt.*; public class JHello4 extends JApplet { Container con = getContentPane(); JLabel greeting = new JLabel("Hello. Who are you?"); Font headlineFont = new Font("Helvetica", Font.BOLD, 36); JTextField answer = new JTextField(10); public void init() { greeting.setFont(headlineFont); con.add(answer); con.add(greeting); }

To fix this, you must use a layout manager Layout manager—a class that controls component positioning This is in contrast to the use of the BorderLayout format The BorderLayout class divides a container into five regions: north, east, south, west and center

The following code… Uses the flowLayout() to control the ordering of the components

import javax.swing.*; import java.awt.*; public class JHello5 extends JApplet { Container con = getContentPane(); JLabel greeting = new JLabel("Hello. Who are you?"); Font headlineFont = new Font("Helvetica", Font.BOLD, 36); JTextField answer = new JTextField(10); JButton pressMe = new JButton("Press me"); public void init() { greeting.setFont(headlineFont); con.add(greeting); con.add(answer); con.add(pressMe); con.setLayout(new FlowLayout()); }

import javax.swing.*; import java.awt.*; public class JHello5 extends JApplet { Container con = getContentPane(); JLabel greeting = new JLabel("Hello. Who are you?"); Font headlineFont = new Font("Helvetica", Font.BOLD, 36); JTextField answer = new JTextField(10); JButton pressMe = new JButton("Press me"); public void init() { greeting.setFont(headlineFont); con.add(greeting); con.add(answer); con.add(pressMe); con.setLayout(new FlowLayout()); }

Learning about event-driven programming An event occurs when someone using your applet takes action on a component, such as clicking the mouse on a JButton object An event occurs when someone using your applet takes action on a component, such as clicking the mouse on a JButton object In an event-driven program, the user has dozens of events to choose from in any given order In an event-driven program, the user has dozens of events to choose from in any given order –Consider MS Word… A component that a user can act on is a source of an event A component that a user can act on is a source of an event

Sources… A button that a user can click on A text field that a user can enter text into A menu bar is another collection of sources

Listeners An object that is interested in an event is a listener If you want an object, such as your applet, to be a listener for an event, you must register the object as a listener for the source Listeners must have event-handling methods that respond to the events

To respond to user events Prepare your JApplet to accept event messages Tell your JApplet to expect events to happen Tell your JApplet how to respond to events

Preparing your JApplet to Accept Event Messages You must add this import statement You must add this import statement Import java.awt.event.*; Then you must change your class header as follows: Then you must change your class header as follows: Public class JHello6 extends JApplet implements ActionListener See the code below See the code below

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JHello6 extends JApplet implements ActionListener { Container con = getContentPane(); JLabel greeting = new JLabel("Hello. Who are you?"); Font headlineFont = new Font("Helvetica", Font.BOLD, 36); JTextField answer = new JTextField(10); JButton pressMe = new JButton("Press me"); public void init() { greeting.setFont(headlineFont); con.add(greeting); con.add(answer); con.add(pressMe); con.setLayout(new FlowLayout()); pressMe.addActionListener(this); } public void actionPerformed(ActionEvent e) { String name = answer.getText(); System.out.println("You pressed the button, " + name); }

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JHello6 extends JApplet implements ActionListener { Container con = getContentPane(); JLabel greeting = new JLabel("Hello. Who are you?"); Font headlineFont = new Font("Helvetica", Font.BOLD, 36); JTextField answer = new JTextField(10); JButton pressMe = new JButton("Press me");

public void init() { greeting.setFont(headlineFont); con.add(greeting); con.add(answer); con.add(pressMe); con.setLayout(new FlowLayout()); pressMe.addActionListener(this); answer.addActionListener(this);/* to allow user to hit return, as well as click button */ } public void actionPerformed(ActionEvent e) { String name = answer.getText(); System.out.println("You pressed the button, " + name); }

Telling your JApplet How to Respond to Events The ActionListener interface contains the actionPerformed(ActionEvent e) method The ActionListener interface contains the actionPerformed(ActionEvent e) method

Telling Your JApplet to Expect Events to Happen You tell your applet to expect ActionEvents with the addActionListener() method You tell your applet to expect ActionEvents with the addActionListener() method

Adding and Removing JApplet Components remove(pressMe); Removes the pressMe button Removes the pressMe buttonremove(answer); Removes the answer JTextField Removes the answer JTextField personalGreeting.setText(“hello, “ + name); Con.add(personalGreeting); Adds a component Adds a component

Validate() This method is added to an actionPerformed method after one or more JComponents have been added to the screen. The method ensures that each Component draws itself properly on the screen This method is added to an actionPerformed method after one or more JComponents have been added to the screen. The method ensures that each Component draws itself properly on the screen

Understanding the JApplet Life Cycle Init() start() stop() destroy()

Using Additional Applet Methods There are 200 additional methods for manipulating components within Japplets There are 200 additional methods for manipulating components within Japplets You can learn about these at You can learn about these at

Using Additional Applet Methods

Using the setLocation() Method The setLocation() Method The setLocation() Method Allows you to place a component at a specific location within the Applet Viewer window Allows you to place a component at a specific location within the Applet Viewer window Does not allow you to specify the location of the applet itself Does not allow you to specify the location of the applet itself

Using the setEnabled() Method Use this method to make a component unavailable and then make it available again in turn Use this method to make a component unavailable and then make it available again in turnpressMe.setEnabled(false);answer.setEnabled(false); The above statements will make the textbox and buttons on the form dim so that they cannot be enabled