Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 08, 2024, 10:26:18 AM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » JAVA
 Java: turn your .class into an applet
Pages: [1]   Go Down
  Print  
Author Topic: Java: turn your .class into an applet  (Read 3145 times)
Khushi
Global Moderator
Adv. Member
*****



Karma: 4
Offline Offline

Posts: 329

Hello!!


View Profile
Java: turn your .class into an applet
« Posted: January 03, 2007, 12:03:36 AM »


How to modify your existing java GUIs into applets to share with the web viewing world.

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 .class 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:
public void init() {}
this method is run when the applet is being loaded into memory or constructed. Think of this as the applet constructor.

public void start() {}
this is run once all preparations are done, and the code is actually beginning to run. Place any splash screens or intro stuff here.

public void stop() {}
this is run when the applet has been terminated, while memory is being freed and the code is stopping.

public void destroy() {}
this method is called just before a system exit. Notifying that the applet has finished and cannot be run again without reloading it.

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


*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 (runApplet.html):

Code:
<html> 
<head>
<applet code="applet.class" width=600 height=30></applet>
</head>
<body>
</body>
</html>

A simple applet tage designating the source and display size is all that is necessary within the head of your html file.

NOTE your applet has more than 1 .class file, but the main file is the only one that needs to be called. The html and class files, should be located in the same directory to ensure all .class files can be found.

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.132 seconds with 24 queries.