Instructions: Do note that most file transfers are not possible in applets, it just doesn't work like that. There are ways around it to do some read or write operations, but that is beyond this simple transformation recipe.
An applet is basically the GUI you already made, except it doesn't have a frame. The frame is supplied by the browser opening the [b].class[/b] file.
It is quite a simple task, assuming your code was written using panels, instead of frames (as you should have). This will allow for easy transfer from java code, to applet.
There are a few methods that are required to make an applet work:
[b]public void init() {}[/b]
this method is run when the applet is being loaded into memory or constructed. Think of this as the applet constructor.
[b]public void start() {}[/b]
this is run once all preparations are done, and the code is actually beginning to run. Place any splash screens or intro stuff here.
[b]public void stop() {}[/b]
this is run when the applet has been terminated, while memory is being freed and the code is stopping.
[b]public void destroy() {}[/b]
this method is called just before a system exit. Notifying that the applet has finished and cannot be run again without reloading it.
*Note i have added the JFrame information into the main of the java file, this allows us to test our program as we go along, without having to run the html file.
//CODE Example (applet.class):
[code]
import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class applet extends JApplet{
static final int WIDTH = 500;
static final int HEIGHT = 60;
JTextField title;
public class display extends JPanel{
public display(){
Container container;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
getContentPane().setLayout(layout);
container = getContentPane();
JLabel label1 = new JLabel("Name:");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 2.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(label1, layoutConstraints);
container.add(label1);
title = new JTextField();
layoutConstraints.gridx = 1; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 2; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 40.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(title, layoutConstraints);
container.add(title);
}
}
public void init() {
System.out.println("Applet initializing");
display d = new display();
getContentPane().add(d);
}
public void start() {
System.out.println("Applet starting");
}
public void stop() {
System.out.println("Applet stopping");
}
public void destroy() {
System.out.println("Applet destroyed");
}
public static void main(String args[]){
JFrame frame = new JFrame("Code Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applet a = new applet();
a.init(); // Do what browser normally does
frame.getContentPane().add(a); // Add applet to frame
frame.setSize(WIDTH, HEIGHT); // Set the size of the frame
frame.setVisible(true); // Show the frame
}
}
//CODE Example (runApplet.html):
[code]
<html>
<head>
<applet code="applet.class" width=600 height=30></applet>
</head>
<body>
</body>
</html>
Post a Comment