Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Applets

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

3 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

4 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

5 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 ); }

6 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

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

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

10 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

11 AWT/Swing components

12 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

13 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)

14 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()

15 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

16 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

17 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)

18 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 ); }... }


Download ppt "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."

Similar presentations


Ads by Google