PowerPoint File, 1,38 MB
Download
Report
Transcript PowerPoint File, 1,38 MB
Applications & Applets
Standalone applications
& Java applets
Peter Mozelius DSV/UCSC
1
Running Java
Standalone applications
Like traditional programs
BUT platform independent
javac MyClass.java
java MyClass
http://java.sun.com/developer/onlineTraini
ng/Programming/BasicJava1/compile.html
2
Running Java on the Internet
Applications could be packed as jararchives and executed by JNLP
Run on a server
3
Running Java on the Internet
Two other alternatives
Servlets
Applets
Running on the server side
Running in the web browser
And Java Applets is the main theme for
this course !!
4
Inheritance for Applets
An Object extended to
A Container extended to
A Panel that is extended to
An Applet which I can extend to
to MyApplet
5
Application Applet
How to make an applet out of an application ?
THE APPLICATION:
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class MyApplication extends JFrame
implements ActionListener {
6
The Application
private JButton northButton;
private JLabel southLabel;
/**
* The constructor that creates the GUI
*/
public MyApplication(){
//set the title and the size
super("Lektion24");
setSize(300, 150);
7
The Application
//create some Swing components
northButton = new JButton("PUSH ME for
a greeting");
southLabel = new JLabel("Here goes the
greeting!", JLabel.CENTER);
//connect the button with a listener
northButton.addActionListener(this);
8
The Application
//lay out the components
add(northButton, BorderLayout.NORTH);
add(southLabel, BorderLayout.SOUTH);
//make the window visible and closable
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//constructor
9
The Application
/**
* The implementation of the method from the
* interface ActionListener. The code that’s
* going to be executed when the user clicks
* on the button
*/
public void actionPerformed(ActionEvent e) {
southLabel.setText("Hello Colombo!");
}//actionPerformed
public static void main(String[] args) {
new MyApplication();
}
10
Workshop Pause
11
The Applet run by AppletViewer
12
The Applet Code part 1
Even simpler than the application
import
import
import
import
java.awt.BorderLayout;
java.awt.event.*;
javax.swing.*;
java.applet.*;
public class MyApplet extends JApplet
implements ActionListener {
13
The Applet Code part 2
private JButton northButton;
private JLabel southLabel;
/**
* A method that initiates the applet and
* creates the GUI
*/
public void init(){
//set the size of the window
setSize(300, 150);
14
The Applet Code part 3
northButton = new JButton("PUSH ME
for a greeting");
southLabel = new JLabel("Here goes the
greeting!", JLabel.CENTER);
//connect the button with a listener
northButton.addActionListener(this);
add(northButton, BorderLayout.NORTH);
add(southLabel, BorderLayout.SOUTH);
}//constructor
15
The Applet Code part 4
/**
* The implementation of the method from
* ActionListener. The code that is
* going to be executed when the user
* clicks on the button
*/
public void actionPerformed(ActionEvent e) {
southLabel.setText("Hello Colombo!");
}//actionPerformed
}//MyApplet
16
The (oversimplified) HTML file
<html>
<applet code="MyApplet.class"
width="300" height="200">
Problems with the applet
</applet>
</html>
17
Workshop Pause
18
Another Simple Applet
A simple but
illustrative
Applet
Run with the
AppletViewer
19
The Simple Applet Code 1
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class SimpleApplet
extends Applet{
private String text;
20
The Simple Applet Code 2
public void init() {
text = "I'm a simple applet";
setBackground(Color.cyan);
}
public void start() {
System.out.println("starting...");
}
21
The Simple Applet Code 3
public void stop() {
System.out.println("stopping...");
}
public void destroy() {
System.out.println("preparing to
unload...");
}
22
The Simple Applet Code 4
public void paint(Graphics g){
g.setColor(Color.blue);
g.drawRect(0, 0, getSize().width
-1, getSize().height -1);
g.setColor(Color.red);
g.drawString(text, 15, 25);
}//paint
}//SimpleApplet
23
The Simple Applet Code 5
Further info about this applet can be
found on:
http://java.sun.com/developer/onlineTr
aining/Programming/BasicJava1/applet.
html#struct
In Swing applets you replace paint()
with the newer paintComponent()
24
The Appletviewer
How to run applets outside a web browser
25
The Appletviewer
The final result in a web browser
A web browser works with a cache
Appletviewer during the development of
the applet
That all for now, thank you!
26