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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

User Interfaces II GUI – Awt, Swing, Web
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
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.
Corresponds with Chapter 12
18-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.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
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.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh 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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Java Programming Chapter 10 Graphical User Interfaces.
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.
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,
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.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Chapter 16: Creating Windows. 2 Basics of GUI Programming How to create a Window. TryWindow.java You always need a JFrame component before creating.
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.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Chapter 13: Advanced GUIs and Graphics
The AWT (versus Swing Components)
GUI building with the AWT
Steps to Creating a GUI Interface
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

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 graphical in content One way of writing Java programs (besides applications) Java programs that can be embedded in HTML documents (web pages) Note: Not all web browsers automatically support Java 2. You will have to install the Java Plug-in, which is done automatically if you installed Java on your machines.

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 3 JApplet This is the “swing” version of Applets JApplet is the newer version of the Applet class that is used with Java’s swing GUI components We will be using this in class

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 4 Java Applet classes Abstract Window Toolkit (AWT) Contain classes that provide the Java graphical user interface (GUI) components Java Foundation Classes Was an extension to java Contains swing components written entirely in java

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 5 Writing Java Applets import javax.swing.*; import java.awt.*; /**This applet displays “Hello, world!” on a label.**/ public class MyApplet extends JApplet { private JLabel helloLabel; public void init() { Container c = this.getContentPane(); c.setLayout( new FlowLayout() ); this.helloLabel = new JLabel( "Hello, world!" ); c.add( this.helloLabel ); }

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 6 Running Java Applets Recall that an.html needs to be created to execute applets Executing through an IDE In BlueJ:, right-click on the class and select “Run Applet”, then Click on “OK” and wait for the applet to appear In JCreator, create the.html file and execute that file Executing through the Command Prompt appletviewer file.html Executing through a browser: Go to the folder where the.html and the.class resides Double-click on that and wait for the browser to load

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 7 Writing Web Pages with Applets You will have to include this in the body of the HTML file to tell it you are embedding an applet: To embed the class file of MyApplet.java:

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 8 Container and getContentPane() Container A generic AWT container a component that can contain other AWT components getContentPane() A method found in JApplet This method returns the contentPane object (a Container) for this applet. You’ll need these to be able to add components to your applet.

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 9 public void init() Every applet must implement one or more of the init(), start(), and paint() methods. This is what a browser usually looks for when it runs applets. You will have to include this method to give your applet functionality This is where you add the components to a container and listeners to make these components work

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 10 Importing packages To be able to use JApplet and swing components you will have to import the javax.swing package and to be able to use some AWT components, you will have to import the java.awt package. Importing packages is a way to tell your program that you want to include certain java classes found in these packages

AWT/Swing components

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 12 The Java Swing Components Java’s framework for programming lightweight GUI Components Lightweight here means that it’s written in and it runs entirely in Java Components JButton, JTextField, JTextArea, JLabel, JPanel Others (self-study) You will have to add these components to an AWT container

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 13 The Java Abstract Window Toolkit (AWT) Java’s framework for programming Graphical User Interfaces (GUIs) Containers Layout Managers FlowLayout, GridLayout, BorderLayout CardLayout, GridBagLayout (self-study)

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 14 Components JButton: clickable visual object JLabel: text JTextField contains editable text methods setText() and getText() JTextArea same as JTextField but can support multiple lines JPanel may contain other visual components methods setLayout() and add()

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 15 Layout Managers FlowLayout objects are placed row by row, left to right GridLayout divides container into an m by n grid BorderLayout divides container into 5 parts Center, North, South, East, West e.g., add( component, “North” ); See examples

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 16 Panel A Panel is a component that can contain other objects We can do more complex layouts by using Panels within the Applet or even Panels within Panels

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 17 Designing a GUI for an Applet import java.awt.*; import javax.swing.*; extends JApplet “extends” means “inherit” built-in features of JApplet (more on this later) Declare variables for the different visual components to be placed on the applet In init() method, create visual components (use new) establish layout manager (use setLayout()) default for JApplets: BorderLayout add visual components (use MyContainer.add()) nested layouts possible (use Panels)

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 18 GUI Example import javax.swing.*; import java.awt.*; public class HelloWorldApplet2 extends JApplet { private JButton button; private JTextField textField; private JLabel label; public void init() { Container c = this.getContentPane(); c.setLayout( new FlowLayout() ); label = new JLabel( "Hello, World!" ); c.add( label ); textField = new JTextField( "Enter your name here", 15 ); c.add( textField ); button = new JButton( "Click Me" ); button.addActionListener( this ); c.add( button ); }... }