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.

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.
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
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
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.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
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.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
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.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
University of Limerick1 Software Architecture Java Layout Managers.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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.
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.
Basics of GUI Programming Chapter 11 and Chapter 22.
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)
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.
Advanced Topics on Graphical User Interfaces CardLayout JTabbedPane JFrame.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces -- GUIs
Java Swing.
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Chapter 13: Advanced GUIs and Graphics
The AWT (versus Swing Components)
Steps to Creating a GUI Interface
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

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 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.

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI 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

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI 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

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI 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 = getContentPane(); c.setLayout( new FlowLayout() ); this.helloLabel = new JLabel( "Hello, world!" ); c.add( helloLabel ); }

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI 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

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI 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:

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 8 Container and getContentPane() Container A generic AWT container a component that can contain other AWT components Most important methods: setLayout() and add() 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.

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI 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 the init() method to give your applet its intended appearance and functionality Think of init() as a constructor for your applet This is where you add the components to a container The applet is in effect an aggregate class containing the labels, buttons, text fields, and other visual components

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI 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. At the top of your java class: import javax.swing.*; import java.awt.*;

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 11 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

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 12 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)

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 13 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()

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 14 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

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 15 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

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 16 Designing a GUI for an Applet import java.awt.*; import javax.swing.*; extends JApplet inherits built-in features of JApplet 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)

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 17 Applet Example import javax.swing.*; import java.awt.*; public class HelloWorldApplet extends JApplet { private JButton button; private JTextField textField; private JLabel label; public void init() { Container c = 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" ); c.add( button ); }... }

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 18 HTML file for the applet <applet code=HelloWorldApplet.class width=300 height=200>

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 19 JFrame Use JFrame instead of JApplet if you want a GUI application (instead of a web-based applet) Extend JFrame instead of JApplet Instead of the init() method, place initialization code in the class’ constructor Need a main method (perhaps in a separate class) that instantiates and displays the frame

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 20 Frame example import javax.swing.*; import java.awt.*; public class HelloWorldFrame extends JFrame { private JButton button; private JTextField textField; private JLabel label; public HelloWorldFrame() { 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" ); c.add( button ); }... }

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 21 Driver class for the frame import javax.swing.*; public class FrameRunner { public static void main( String args[] ) { JFrame f = new HelloWorldFrame(); f.setSize( 200, 300 ); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); f.setVisible( true ); }

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 22 About frames When using frames, you could call setLayout() and add() directly on the frame object No need to declare a Container object or call getContentPane() Other methods applicable on a frame object (these can be called in the frame’s constructor, or from the driver) setSize(): sets the dimensions of the frame setTitle(): sets the title bar of the frame