Java applet在浏览器中不起作用

时间:2012-11-21 16:18:34

标签: java applet

我制作一个java applet的人:

    import java.net.InetAddress;
public class PegaIP {
    public static void main(String[] args) {
        try {
            getAppletContext().showDocument
                    (new URL("javascript:UpdateIP(\"" + InetAddress.getLocalHost().getHostAddress() + "\")"));
        } catch (Exception e) { }
    }
}

我收到了一些错误:

Plug-in 10.9.2.05 do Java  
Usando versão JRE 1.7.0_09-b05 Java HotSpot(TM) Client VM  
Diretório home do usuário = C:\Users\Edilsom  

c:   limpar janela da console  
f:   concluir objetos da fila de finalização  
g:   coleta de lixo  
h:   exibir esta mensagem de ajuda  
l:   descartar lista de carregadores de classes  
m:   imprimir uso de memória  
o:   log do trigger  
q:   ocultar console  
r:   recarregar configuração da política  
s:   descartar propriedades do sistema e de implantação  
t:   descartar lista de threads  
v:   descartar pilha de threads  
x:   limpar cache do carregador de classes  
0-5: definir nível de rastreamento como <n>  

Match: beginTraversal  
Match: digest selected JREDesc: JREDesc[version 1.6+, heap=-1--1, args=null, ref=http://java.sun.com/products/autodl/j2se, sel=false, null, null], JREInfo: JREInfo for index 0:  
platform is: 1.7  
product is: 1.7.0_09  
location is: http://java.sun.com/products/autodl/j2se  
path is: C:\Program Files (x86)\Java\jre7\bin\javaw.exe  
args is: null  
native platform is: Windows, x86 [ x86, 32bit ]  
JavaFX runtime is: JavaFX 2.2.3 found at C:\Program Files (x86)\Java\jre7\  
enabled is: true  
registered is: true  
system is: true  

Match: ignoring maxHeap: -1  
Match: ignoring InitHeap: -1  
Match: digesting vmargs: null  
Match: digested vmargs: [JVMParameters: isSecure: true, args: ]  
Match: JVM args after accumulation: [JVMParameters: isSecure: true, args: ]  
Match: digest LaunchDesc: null  
Match: digest properties: []  
Match: JVM args: [JVMParameters: isSecure: true, args: ]  
Match: endTraversal ..  
Match: JVM args final:   
Match: Running JREInfo Version    match: 1.7.0.09 == 1.7.0.09  
Match: Running JVM args match: have:<>  satisfy want:<>  
CacheEntry[http://localhost/3gloja/pegaip.class]: updateAvailable=true,lastModified=Tue Nov 20 22:46:40 GMT-03:00 2012,length=780  

我做错了什么?

2 个答案:

答案 0 :(得分:2)

Java applet不与main一起运行,需要init。还必须扩展JApplet。

导入javax.swing.JApplet;

公共类PegaIP扩展了Applet

public void init()替换public static void main(String [] args)

可在此处找到资源:http://docs.oracle.com/javase/tutorial/deployment/applet/getStarted.html

答案 1 :(得分:1)

人们这是代码适合我:

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import javax.swing.JApplet;
public class PegaIP extends JApplet {
    @Override
    public void init() {
        try {
            getAppletContext().showDocument (new URL("javascript:UpdateIP(\"" + InetAddress.getLocalHost().getHostAddress() + "\")"));
        } catch (UnknownHostException | MalformedURLException e) {
            System.err.println("Ocorreu erro na criação da GUI");
        }
    }
}

谢谢大家帮助我......