简体   繁体   中英

View Java Applet without html and without browser and without Appletviewer?

With Swing applications I can use an external class to instantiate and view them.

I'd like to do the same with an Applet, outside of Eclipse, without using the appletviewer.

I want to be able to Run a class MyappletRunner and have its main method kick off the following applet for viewing.

Given the following source code:

import java.applet.*;
import java.awt.*;
public class Myapplet extends Applet{
  String str;
  public void init(){
    str = "This is my first applet";
  }
  public void paint(Graphics g){
    g.drawString(str, 50,50);
  }
}

The basic idea is to create your own Swing Frame , add the Applet to your frame, and then pass your Applet an instance of the AppletStub interface.

The best example of it appears to be here: http://www.java2s.com/Code/JavaAPI/java.applet/implementsAppletStub.htm

Appletviewer is the tool you want (if you are just testing that is): http://download.oracle.com/javase/1.3/docs/tooldocs/win32/appletviewer.html

If you want to launch it as a standalone app, the following tutorial works: http://java.sun.com/developer/technicalArticles/Programming/TurningAnApplet/

Not exactly sure why you are trying to do this... what exactly are you going to accomplish by doing this? You say you want to run a java applet... within a java application... AFAIK, it isn't possible anyway. If you just want to view the applet, use appletviewer.

write the folloing code in the begining of the xyz.java file:

/*<applet code="xyz.class" height=30 width=50></applet>*/

Save and compile the xyz.java file.

Now, Execute the file using the appletviewer as following:

c:\jdk\bin>appletviewer xyz.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM