2011-03-29 54 views
1

我在我的第一個編程課,現在我已經達到了學期的末尾。我有一個在線投資組合,我可以分享我的大學成就。在這個學期的這個時候,我想將我創建的一些簡單小程序上傳到我的在線作品集中。我的投資組合託管在Weebly.com上。我嘗試將文件上傳到主機站點,並使用簡單的標籤在html中運行applet。我很確定我正在使用正確的目錄訪問這些文件。但在我們跳到任何結論之前,我決定我應該在本地運行小程序,以確保我正確地做到了一切。我在運行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。一切似乎都與我在同一頁面上。如何在Mac OS上運行Java Applet 10.6.6

是的,我一遍又一遍地讀了所有相關的問題。他們中的大多數現在有點過時了。

這裏是我的applet代碼:

/** 
* 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 

有些人已經開始使用基本代碼=」建議。 「所以我嘗試了這個無濟於事。它無法使用或不使用它。我試圖把完整的目錄路徑,沒有成功。我嘗試了引號,並且沒有引用類名。最後我嘗試了使用和不使用.class。我試着製作一個lab05_1子目錄,因爲這是代碼中的包名稱。沒有運氣。類文件和html文件都在桌面上的相同文件夾中。類文件是在Eclipse中創建的原始文件的副本,但它具有相同的名稱,所以我不認爲這應該會導致在不同目錄中出現任何問題。

我不知道還能做些什麼。請幫忙!這一週讓我感到厭煩。我花了幾個小時來處理這麼簡單的事情。

+0

我已經有最幸運的運行使用Firefox的Java小程序,什麼是值得的。 – CarlosZ 2011-03-29 04:45:58

+0

我忘了提及它在Eclipse中運行時在applet查看器中運行。只是當我嘗試在瀏覽器(Safari或Firefox)中運行它們時,它們不起作用。 – 2011-03-29 04:49:47

+0

@CarlosZ感謝CarlosZ,我一直在嘗試firefox,無濟於事。 :/ – 2011-03-29 04:50:39

回答

1

我沒有Mac的方便檢查,但如果你改變你的HTML文件,它應該工作---你錯過了NightScene.class的包名稱。

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

APPLET標記的Oracle參考是here

在這個頁面:

CODE = appletFile

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.


編輯:萬一它不清晰,目錄應該像這樣佈局:

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

真棒謝謝!我得到它與該代碼工作!它也適用於正斜槓。好吧,現在我已經開始工作了。我只需要讓它在我的主機上工作。問題在於他們要求您將所有文件上傳到一個一級目錄。所以我不能把類文件放到一個子目錄中。我是否應該改變我的類文件,以使包名與周圍的目錄匹配?那裏有任何想法?再次感謝Spong,你搖滾! – 2011-03-29 17:59:21

+0

我認爲如果你不能創建'lab05_1'子目錄,那麼你將需要從你的java源文件中刪除包聲明並重新編譯。您還需要從html中刪除軟件包名稱;) – msandiford 2011-03-30 01:47:10

1

也許老了,我只是發現這個網頁試圖回答一個不同的問題,但是看看你的原始HTML和ORACLE鏈接的價值,我發現你周圍沒有引號類文件名。懷疑這可能是問題的根源。實際上,這也許是'Spong'正在獲得的。我只是沒有看到任何提及的引號,這是跳出我的...

相關問題