Challenge: Resizing applet from within Java code.挑戰: Applet的大小從Java代碼。 For example say the applet code calculates that the applet needs more space than it has been allocated, then how to go about it?例如說, Applet的代碼計算,該程序需要的空間比它已撥出,又如何去嗎?

Last time when faced with this problem去年的時候,面對這個問題 I solved it with a nifty piece of Javascript method i解決它與一漂亮的一塊JavaScript方法 . However I soon realized there were few issues with the approach.不過,我很快實現了有幾個問題與做法。

First we made the applet dependent on external piece of Javascript method which creates undesirable dependency.首先,我們作出了Applet的依賴外部一塊的JavaScript方法造成不良的依賴。

The main problem was when we wanted to embed multiple such applets in the same page.主要的問題是,當我們要嵌入多個此類程序在同一個網頁上。 Obviously now I cannot use the same resize method, which is hardcoded to call the applet by name.顯然,現在我不能使用相同的調整方法,這是硬編碼調用Applet的名字。 Also I found that document['appletname'] logic fails when the applet is within a table.此外,我發現文件[ ' appletname ' ]的邏輯,未能當Applet的是一張桌子。 Then we have to refer to the applet using the hierarchy which we do not control.然後我們已轉介到Applet的使用等級,我們不控制。 We also cannot have two applets with the same name as then only the first one executes.我們也不能有兩個程序具有相同名稱的作為則只有第一個執行。
So it was obvious we needed to pass the name of the applet to this method.所以,很明顯我們需要通過的名稱,該Applet的這種方法。 But how to get the name?但如何讓這個名字?
I intuited correctly that since applet.getParameter used to get parameters from within the applet tag, it is possible that we can fetch the name of the applet using the same method. i intuited正確地指出,自applet.getparameter用來取得參數,從內部程序標記,是有可能的,我們可以擷取的名稱,該Applet的使用相同的方法。 That turned out to be the case.這原來是如此。 So now I had this version where I passed the name of the applet to the resize method and it called the applet by its name ( applets['name'] ) and set the size as passed on by the parameters.所以現在我這個版本我通過的名稱,該Applet的,以Resize方法和它所謂的小程序,由它的名字(程序[ '名稱' ] ) ,並設置大小,通過對由參數。 This worked fine with both the browsers (這個工作的優良既符合瀏覽器( Internet Explorer Internet Explorer中 & Netscape網景 ). ) 。 However I was still not satisfied.不過,我仍然不能滿足。 The external dependency was bugging me.對外依存度是竊聽了我。 The next step was to try to execute javascript code within Java itself, using the same LiveConnect bridge.下一步是嘗試執行JavaScript代碼的Java本身,使用相同的liveconnect的橋樑。 My first attempt to set the size using我第一次嘗試設置大小使用 JSObject.getMethod jsobject.getmethod failed in IE.未能在IE中。 Apparently Internet Explorer doesn’t support the method!顯然的Internet Explorer不支持的方法! Then I simply evaluated the whole code using eval.然後我簡單地評價整個代碼使用eval 。 This worked well for both the browsers.這個運作良好,雙方的瀏覽器。 I could get rid of the pesky javascript method - resize.我可以擺脫對討厭的JavaScript方法-調整。 Now finally I was happy.現在終於我很高興。 As I soon realized however there was still a little snag.正如我盡快實現,但仍然有一點阻攔。 The fixes don’t work in Opera browser.修復程序不工作,在Opera瀏覽器。 But I am not too worried considering the market share of this browser.但我不會太擔心考慮的市場佔有率,此瀏覽器。

To summarize the key line of code: jso.eval(applet + “width = ” + width + “;”);總結的關鍵代碼行: jso.eval ( Applet的+ “寬度= ” +寬度+ ";");