浏览器上的小程序

发布于 2024-10-14 20:07:55 字数 345 浏览 3 评论 0原文

如何使用小程序类中声明的一些函数?即

这是我的类

public class hi extends JApplet{

public void HiThere(){
    System.out.println("Hi on Java Console");
}

}

,在我的浏览器上它的声明类似于:

<applet.... name="HI" id="HI"  ......>Ooops!!!</applet>"

但是当我尝试使用该函数时出现了错误,那么如何使用在我的小程序类上声明的函数?谢谢!!!

How I can use some functions that were declared on the applet class? i.e.

this is my class

public class hi extends JApplet{

public void HiThere(){
    System.out.println("Hi on Java Console");
}

}

and on my browser it's declared something like:

<applet.... name="HI" id="HI"  ......>Ooops!!!</applet>"

but when I tried to use the function there was a mistake, so how a can use the functions declared on my applet class?? Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-10-21 20:07:55

首先,您应该对代码进行以下更改:

公共类 HI 扩展 JApplet {

public HI() {System.out.println("Java 控制台上的 Hi");}

}

当您完成此操作后,您的浏览器仍然有错误,请在此处发布确切的错误消息。另外,您可能想查找教程“Java 小程序编程入门”,因为您的代码告诉我,您似乎不知道自己在做什么。

First you should do this change to your code:

public class HI extends JApplet {

public HI() {System.out.println("Hi on Java Console");}

}

When you have done that, and your browser still has an error, then post the exact error message here. Also you might want to look for a tutorial "programming Java applets getting started", since your code tells me, that you don't seem to know what you are doing.

我是男神闪亮亮 2024-10-21 20:07:55

为了从 js 调用类方法,您必须在 html 中声明 ,并指定 .jar.class 以及您编译的文件内容,主类的包和一些其他参数,如下例所示(对象用于 IE,嵌入用于 FF):

我想您想调用 hi.HiThere()您的问题中定义的方法。

<object
width="100" height="100" id="hi">
<param name = "code" value = "path.to.your.main.class.hi">
<param name = "archive" value = "jar location">
<param name = "mayscript" value = "true">
<param name = "scriptable" value = "true">
...
    <comment>
       <embed 
          code = "path.to.your.main.class.hi" 
          archive = "your jar location"
          scriptable = "true"
          width = "100" height = "100"
          name = "hi"
          ...
       </embed>
</comment>
</object>

然后,您可以在 js 中使用 name 中的 id 属性通过 document 访问对象> 在示例案例 document.hi 中。使用以下代码,您可以调用 HiThere(); 方法:

try{
   // ie, ff
   document.hi.HiThere();
}catch(Exception){
   // chrome, safari, opera
   document.hi[1].HiThere();
}

为了使用最新的 java 版本执行,请记住满足新的 java 安全要求(清单属性 http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html,jar签名... )。

In order to call your class methods from js you must declare the <object> in html specifying a .jar or .class with your compiled content, the package of the main class and some other parameters like in the follow example (object is for IE, and embed is for FF):

I suppose that you want to invoke the hi.HiThere() method as defined in your question.

<object
width="100" height="100" id="hi">
<param name = "code" value = "path.to.your.main.class.hi">
<param name = "archive" value = "jar location">
<param name = "mayscript" value = "true">
<param name = "scriptable" value = "true">
...
    <comment>
       <embed 
          code = "path.to.your.main.class.hi" 
          archive = "your jar location"
          scriptable = "true"
          width = "100" height = "100"
          name = "hi"
          ...
       </embed>
</comment>
</object>

Then from the js you can access you object through the document using the id attribute in <object> or name in <embed> in the sample case document.hi. With the follow code you can invoke the HiThere(); method:

try{
   // ie, ff
   document.hi.HiThere();
}catch(Exception){
   // chrome, safari, opera
   document.hi[1].HiThere();
}

In order to execute with last java versions remember to meet the new java security requirements (manifest attributes http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html, jar signature... ).

世界等同你 2024-10-21 20:07:55

您可以在 JavaScript 中调用 applet 方法:

为此,应从 JavaScript 启动 applet,例如,

<script src="https://www.java.com/js/deployJava.js"></script>
    <script>
        var attributes = { id: 'mainApplet', code:  
'org.jazzteam.Example', archive: 'example.jar', width: 812, height:  
635};                 
        var parameters = {};         
        deployJava.runApplet(attributes, parameters, '1.7');                 
        function actionInApplet(url) {                         
            mainApplet.appletMethod(url);                 
        }
</script>

在本例中,使用 id= 'mainApplet' 从 JavaScript 启动 applet。在小程序中有我们要调用的appletMethod()方法。为此,您需要调用与具有 id name 的 JavaScript 对象相关的方法。您还可以将参数传递给被调用的方法,如本例所示。

还需要知道从 JavaScript 调用的 applet 方法应该具有特权。为此,您需要将方法代码包装在以下“包装器”中:

AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
        //method code
        return new Object();
    }
});

此信息取自文章:
Java applet 开发过程中的常见问题

You can call the applet methods in JavaScript:

To do this the applet should be launched from JavaScript, e.g.

<script src="https://www.java.com/js/deployJava.js"></script>
    <script>
        var attributes = { id: 'mainApplet', code:  
'org.jazzteam.Example', archive: 'example.jar', width: 812, height:  
635};                 
        var parameters = {};         
        deployJava.runApplet(attributes, parameters, '1.7');                 
        function actionInApplet(url) {                         
            mainApplet.appletMethod(url);                 
        }
</script>

In this case the applet is launched from JavaScript with id= 'mainApplet'. In the applet there is appletMethod() method, which we want to call. To do this you need to call the method related to JavaScript object with id name. You can also pass parameters to the called methods, as demonstrated in this example.

Also it is necessary to know that the applet method called from JavaScript should be privileged. To do this you need to wrap the method code in the following "wrapper":

AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
        //method code
        return new Object();
    }
});

This information is taken from the article:
Frequently Asked Questions during Java applet development

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文