Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.

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.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
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 Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
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,
1 INF160 IS Development Environments AUBG, COS dept Lecture 04 Title: Computer Applications (Extract from Syllabus)
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.
GUI programming Graphical user interface-based programming.
Chapter 13. Applets and HTML HTML Applets Computer Programming with JAVA.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
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.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
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.
Applets Yong Choi School of Business CSU, Bakersfield.
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.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Java Application and Applet
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)
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.
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.
A First Look at GUI Applications
Lecture 09 Applets.
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Graphical user interface-based programming
Java Applets.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Java Programming Applets

Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple Swing applet and use a JLabel Add JTextField and JButton Components to Swing applets

Learn about event-driven programming Add output to a Swing applet Understand the Swing applet life cycle Create a more sophisticated interactive Swing applet Use the setLocation() and setEnabled() methods

To Write a Java Application: Write the application in the Java programming language, and then save it with a.java file extension Compile the application into bytecode using the javac command. The bytecode is stored in a file with a.class file extension Use the java command to interpret and execute the.class file

Writing an HTML Document to Host an Applet Applets- Programs that are called from within another application You run applets within a page on the Internet an intranet or a local computer from within another program called Applet Viewer To view an applet, it must be called from within another document written in HTML

Writing an HTML Document to Host an Applet To create an applet: Write the applet in the Java programming language, and save it with a.java file extension Compile the applet into bytecode using the javac command Write an HTML document that includes a statement to call your compiled Java class Load the HTML document into a Web browser or run the AppletViewer program

Writing an HTML Document to Host an Applet Applets are popular because users can execute them using a Web browser Web browser- A program that allows you to display HTML documents on your computer screen Internet Explorer Netscape Navigator

Writing an HTML Document to Host an Applet Code to run an applet from within an HTML document Applet tag attributes CODE = is followed by the name of the compiled applet you are calling WIDTH = is followed by the width of the applet on the screen HEIGHT = is followed by the height of the applet on the screen

Applets The WIDTH and HEIGHT attributes are measured in pixels Pixels- Picture elements, or tiny dots that make up the image on your video monitor

Understanding Simple Applets To write an applet you must also: Include import statements to ensure that necessary classes are available Learn to use some Windows components and applet methods Learn to use the keyword extends

Understanding Simple Applets Component- A class that defines any object that you want to display Container- A class that is used to define a component that can contain other components

Understanding Simple Applets Most AWT applets contain 2 import statements import java.applet.*; import java.awt.*; java.applet- Contains a class named Applet Every applet you create is based on Applet java.awt- The Abstract Windows Toolkit, or AWT

Understanding Simple Applets Most Swing applets contain 2 import statements import javax.swing.*; import java.awt.*; javax.swing- A package that contains classes that define GUI components (Swing components)

Understanding Simple Applets Swing classes- part of a more general set of GUI programming capabilities that are known as the Java Foundation Classes, or JFC JFC includes Swing component classes and selected classes from the java.awt package

AWT and Swing Applets AWT and Swing applets Begin the same way as Java applications Must also include extends Applet extends JApplet The extends keyword indicates the applet will build upon Applet and JApplet

Applets Four methods in every applet public void init() public void start() public void stop() public void destroy() Java can create these for you

Using Labels with Simple AWT Applets The java.awt package contains commonly used Windows components Labels Menus Buttons Label- Built-in class that holds text that you can display within an applet

Using Labels with Simple AWT Applets Label class contains fields that indicate font and alignment You can assign some text to a label with the setText() method Use the add() method to add a component to an applet window

Writing a Simple Swing Applet and Using a JLabel JLabel- Built-in class that holds text that you can display within an applet The counterpart to the AWT Label

Writing a Simple Swing Applet and Using a JLabel Available constructors include: JLabel() creates a JLabel instance with no image and an empty string for the title 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(String text) creates a JLabel instance with the specified text JLabel(String text, Icon icon, int horizontalAlignment) creates a JLabel instance with the specified text, image, and horizontal alignment JLabel(String text, int horizontalAlignment) creates a JLabel instance with the specified text and horizontal alignment

Writing a Simple Swing Applet and Using a JLabel AWT components are added directly to the Applet Swing components must use a content pane The content pane is an object of the Container class A container can be created using the getContentPane() method

Changing a JLabel’s Font Font object- Holds typeface and size information setFont() method requires a Font object argument To construct a Font object you need 3 arguments Typeface Style Point size

Changing a JLabel’s Font To construct a Font object you need 3 arguments Typeface String representing a font Common fonts are Arial, Courier, and New Times Roman Is only a request Style- applies an attribute to displayed text Font.PLAIN Font.BOLD Font.ITALIC Point size Integer that represents 1/72 of an inch Printed text is usually 10- or 12 points

Adding JTextField Components to Swing Applets JTextField- Component into which a user can type a single line of text data JText field can be constructed from public JTextField() constructs a new JTextField public JTextField(int numColumns) constructs a new empty JTextField with a specified number of columns public JTextField(String text) constructs a new JTextField initialized with the specific text public JTextField(String text, int columns) constructs a new JTextField with the specified text and columns

Other JTextField Methods setText() method- Allows you to change the text in a JTextField that has already been created getText() method- Allows you to retrieve the string of text in a JTextField

Other JTextField Methods Keyboard focus- When the user clicks within the JTextField, the JTextField has focus, which means the next entries from the keyboard will be at that location requestFocus() method- To have the insertion point appear automatically within the TextField without requiring the user to click in it first

Editable- The capacity for a field to accept keystrokes setEditable() method- Used to change the editable status of a JTextField Other JTextField Methods

Adding JButton Components to Swing Applets JButton- Creates a button JButton can be constructed from public JButton() constructs a button with no set text public JButton(Icon icon) creates a button with an icon of type Icon or ImageIcon public JButton(String text) creates a button with the specific text public JButton(String text, int columns) constructs a new JTextField with the specified text and columns

Adding JButton Components to Swing Applets setLabel() method To change a JButton’s label readyJButton.setLabel(“Don’t press me again!”)

Adding Multiple Components to a JApplet To add multiple components in a container use a layout manager To control component positioning Default behavior is to use a border layout Border layouts Flow layouts

Adding Multiple Components to a JApplet Border layouts Created by the BorderLayout class Divide a container into 5 sections North, South, East, West, and center Created with the BorderLayout() or BorderLayout(int, int) methods

Adding Multiple Components to a JApplet Flow Layouts Places components in a row, and when a row is filled, it automatically spills components onto the next row Default positioning of the row of components is centered in the container

Event Driven Programming

Learning about Event-Driven Programming Event- Occurs when someone using your applet takes action on a component Procedural- Programmers dictate the order in which events occur Event-driven programs- The user can initiate any number of events in any order Source- Component on which an event is generated Listener- Object that is interested in an event

Preparing Your Swing Applet to Accept Event Messages Prepare your applet to accept mouse events by: importing the java.awt.event package adding the phrase implements ActionListener to the class header ActionListener is an interface Interface- A set of specifications for methods that you can use with event objects

Telling Your Swing Applet to Expect Events to Happen addActionListener() method To tell the applet to expect ActionEvents aButton.addActionListener(this);

Telling Your Swing Applet How to Respond to Any Events That Happen actionPerformed(ActionEvent e) method When a JApplet has registered as a listener with a JButton, and a user clicks the JButton the actionPerformed method executes

Adding Output to a Swing Applet You can add components to an applet using the add() method You can also remove components from an applet using the remove() method Remove(answer);

Understanding the Swing Applet Life Cycle Override- When you write a method that has the same method header as an automatically provided method

Understanding the Swing Applet Life Cycle start() method- Executes after the init() method Executes every time the applet becomes active after it has been inactive stop() method- When a user leaves a web page You do not usually write your own stop() methods

Understanding the Swing Applet Life Cycle destroy() method- When the user closes the browser or AppletViewer You do not usually write your own destroy() methods

Using the setLocation() and setEnabled() Methods setLocation() method- Allows you to place a component at a specific location within the AppletViewer window X-axis- Horizontal position in a window X-coordinate- Value increases as you travel from left to right across the window Y-axis- Vertical position in the window Y-coordinate- Value increases as you travel from top to bottom in the window

The setEnabled() Method setEnabled() method- To make a component unavailable and, in turn, to make it available again True if you want to enable a component False if you want to disable a component If (yLoc==280) pressButton.setEnabled(false);