如何在 Mac OS 10.6.6 上运行 Java Applet [英] How to run a Java Applet on Mac OS 10.6.6

查看:27
本文介绍了如何在 Mac OS 10.6.6 上运行 Java Applet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上我的第一堂编程课,现在我已经快到学期末了.我有一个在线作品集,可以在其中分享我的大学成就.在这个学期的这个时候,我想将我创建的一些简单的小程序上传到我的在线作品集中.我的投资组合托管在 Weebly.com 上.我尝试将文件上传到主机站点并使用简单的标签在 html 中运行小程序.我很确定我正在使用正确的目录访问文件.但在我们得出任何结论之前,我决定我应该在本地运行小程序以确保我做的一切都是正确的.我在运行 OS 10.6.6 的 macbook pro 上.在 Java 首选项中,我的 Java SE 版本是 Java SE 6 64 位和 Java SE 6 32 位.我的插件版本是 1.6.0(在/System/Library/Java/JavaVirtualMachines 中找到).这些是我机器上仅有的版本.我的研究告诉我,我可能有版本分歧.一些论坛建议返回插件版本 1.5(尽管我不知道如何).尽管苹果已将 Safari 更新为 64 位版本,但我现在很确定.我也将 Eclipse 设置为 1.6.对我来说,一切似乎都在同一页面上.

I am in my first programming class and I am now reaching the end of the semester. I have an online portfolio where I share my college accomplishments. At this point in the semester, I would like to upload some of the simple Applets I have created to my online portfolio. My portfolio is hosted on Weebly.com. I tried uploading the files to the host site and using the simple tags to run the applet within the html. I'm quite sure I'm accessing the files using the correct directories. But before we jump to any conclusions there, I decided I ought to run the applets locally to ensure I'm doing everything correctly. I am on a macbook pro running OS 10.6.6. In Java Preferences, my Java SE versions are Java SE 6 64-bit and Java SE 6 32-bit. My plug-in version is 1.6.0 (found in /System/Library/Java/JavaVirtualMachines). These are the only versions I have on my machine. My research tells me that I may be having version disagreements. Some forums have suggested going back to plug-in version 1.5 (although, I have no idea how). I'm pretty sure now though that apple has updated Safari to a 64-bit version. I have Eclipse set to 1.6 also. Everything seems to be on the same page to me.

是的,我一遍又一遍地阅读了这里的所有相关问题.他们中的大多数现在都有些过时了.

And yes, I've read all the related questions on here over and over. Most of them are a little outdated now.

这是我的小程序代码:

/** 
 * Class NightScene - Draws a night scene (just for fun). 
 *  
 * @author Alex Stout 
 * @version February 8, 2011 
 */ 
package lab05_1; 
import java.awt.*; 
import javax.swing.*; 

public class NightScene extends JApplet 
{ 
        /** 
         * Paint method for applet. 
         *  
         * @param  g   the Graphics object for this applet 
         */ 
        public void paint(Graphics g) 
        { 
            g.setColor(Color.BLUE.darker().darker().darker());
            g.fillRect(0,0, this.getWidth(), this.getHeight());

            this.drawMoon(g);

            this.drawStars(g);

            this.drawHorizon(g);

        }
        public void drawStars(Graphics h)
        {   
            for (int i = 0 ; i <= this.getWidth()*5; i++)
            {
                int x = (int)(Math.random()*this.getWidth());
                int y = (int)(Math.random()*this.getHeight());

                h.setColor(Color.WHITE);
                h.fillOval (x, y, (int) (Math.random()*3)+1, (int) (Math.random()*3)+1);
            }

        }
        public void drawMoon(Graphics j)
        {
            int x = (int)(Math.random()*(this.getWidth()-200)+50);
            int y = (int)(Math.random()*(this.getHeight()-200)+50);

            j.setColor(Color.YELLOW.brighter().brighter());
            j.fillOval (x, y, this.getWidth()/10, this.getWidth()/10);
            j.setColor (Color.BLUE.darker().darker().darker());
            j.fillOval (x-(this.getWidth()/100), y-(this.getWidth()/100), this.getWidth()/10, this.getWidth()/10);
        }   

        public void drawHorizon(Graphics k)
        {
            int xi = 0;
            int xj = this.getWidth();
            int yj = this.getHeight();
            int rh = this.getHeight()-this.getHeight()/8;

            for (int i=0; i < xj; i++)
            {
                k.setColor(Color.BLACK);
                k.drawLine(xi, yj, xi, rh);

                k.setColor(Color.BLUE);
                if(Math.random()<0.50)
                {
                    k.drawLine(xi++, rh++, xi, rh++);
                }
                else
                {
                    k.drawLine(xi++, rh--, xi, rh--);
                }

            }           
        }
}    

这是我的 html 代码:

Here's my html code:

<html>
<Applet code = NightScene.class codebase = "." width = "400" height = "400">
</Applet>
</html>

Java 控制台输出如下:

Here's the Java Console output:

Java Plug-in 1.6.0_24
Using JRE version 1.6.0_24-b07-334-10M3326 Java HotSpot(TM) 64-Bit Server VM
User home directory = /Users/myUserName

有些人建议使用 codebase = "."所以我尝试了无济于事.无论有没有它,它都不起作用.我尝试放入完整的目录路径,但没有成功.我尝试了引号,但类名周围没有引号.我最后尝试使用和不使用 .class.我尝试创建一个 lab05_1 子目录,因为这是代码中的包名.没运气.class 文件和 html 文件都在桌面上的同一个文件夹中.类文件是在 Eclipse 中创建的原始文件的副本,但它具有相同的名称,因此我认为这不会导致位于不同目录中的任何问题.

Some people have suggested using codebase = "." so I tried that to no avail. It doesn't work with or without it. I tried putting in the full directory path, no success. I tried quotes and no quotes around the class name. I tried with and without .class on the end. I tried making a lab05_1 subdirectory because that's the package name in the code. No luck. Both the class file and the html file are in the same folder on the desktop. The class file is a copy of the original one that was created in Eclipse, but it has the same name, so I wouldn't think this should cause any problems being in different directories.

我不知道还能做什么.请帮忙!这让我恼火了一个星期.我花了几个小时在这么简单的事情上.

I don't know what else to do. Please Help! This has been irking me for a week. I've spent hours upon hours on something so simple.

推荐答案

我手边没有 Mac 来检查这个,但是如果你改变你的 HTML 文件,它应该可以工作 --- 你缺少包名NightScene.class.

I don't have a Mac handy to check this, but if you change your HTML file, it should work --- you are missing the package name for the NightScene.class.

<html>
  <Applet code="lab05_1.NightScene.class" width="400" height="400"/>
</html>

APPLET 标记的 Oracle 参考是 这里.

The Oracle reference for the APPLET tag is here.

从那个页面:

CODE = appletFile

CODE = appletFile

这需要属性给出文件名包含小程序的编译小程序子类.这个文件是相对的到小程序的基本 URL.它不可能是绝对的.CODE 或对象必须存在.价值appletFile 可以是以下形式classname.class 或形式packagename.classname.class.

This REQUIRED attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute. One of CODE or OBJECT must be present. The value appletFile can be of the form classname.class or of the form packagename.classname.class.

<小时>

以防万一不清楚,目录应该这样布置:


Just in case it's not clear, the directory should be laid out like so:

    +-top-level/
      |
      +-lab05_1/
      | |
      | +-NightScene.class
      |
      +-test.html

这篇关于如何在 Mac OS 10.6.6 上运行 Java Applet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆