如何在 Mac OS 10.6.6 上运行 Java Applet
我正在上第一堂编程课,现在学期即将结束。我有一个在线作品集,我在其中分享我的大学成就。在本学期的这个时候,我想将我创建的一些简单的小程序上传到我的在线作品集中。我的作品集托管在 Weebly.com 上。我尝试将文件上传到主机站点并使用简单的标签在 html 中运行小程序。我很确定我正在使用正确的目录访问文件。但在我们得出任何结论之前,我决定应该在本地运行这些小程序,以确保我所做的一切都是正确的。我的 MacBook Pro 运行操作系统 10.6.6。在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。对我来说一切似乎都在同一页上。
是的,我已经一遍又一遍地阅读了这里的所有相关问题。其中大多数现在都有点过时了。
这是我的小程序代码:
/**
* 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 代码:
<html>
<Applet code = NightScene.class codebase = "." width = "400" height = "400">
</Applet>
</html>
这是 Java 控制台输出:
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 中创建的原始文件的副本,但它具有相同的名称,因此我认为这不会导致位于不同目录中的任何问题。
我不知道还能做什么。请帮忙!这已经让我烦恼了一个星期了。我在如此简单的事情上花了几个小时。
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.
Here's my applet code:
/**
* 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--);
}
}
}
}
Here's my html code:
<html>
<Applet code = NightScene.class codebase = "." width = "400" height = "400">
</Applet>
</html>
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有方便的 Mac 来检查这一点,但如果您更改 HTML 文件,它应该可以工作 --- 您缺少 NightScene.class 的包名称。
APPLET 标记的 Oracle 参考位于 此处。
从该页面:
编辑:以防万一不清楚,目录应如下所示:
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.
The Oracle reference for the APPLET tag is here.
From that page:
EDIT: Just in case it's not clear, the directory should be laid out like so:
也许现在已经很老了,我只是在试图回答一个不同的问题时发现了这个页面,但是对于它的价值,查看您的原始 HTML 和 ORacle 链接,我发现您在类文件名周围没有引号。怀疑这可能真的是问题的根源。事实上,这也许也是《海绵》所要表达的意思。我只是没有看到任何提及引号的内容,这正是我突然想到的......
Maybe old now, I just found this page in trying to answer a different question, but for what it's worth, looking at your original HTML and the ORacle link, I see you did not have quotation marks around the class file name. Suspect that may really have been the root of problem. In effect this is perhaps what 'Spong' is getting at too. I just didn't see any mention of the quotation marks which is what jumped out at me...