【发布时间】:2011-04-04 01:46:15
【问题描述】:
我在 Java 小程序中遇到了某种竞争条件,这是 Google Chrome (win xp) 特有的。
我正在将<applet> 标签写入弹出窗口。
问题的特点是:
- 小程序在 MS IE 和 Firefox 中始终显示良好。
- 在 Chrome 中,小程序仅显示大约 50% 的时间。
- 当它不显示时,小程序实际上似乎在 Java 控制台中完美地静默运行!没有错误!
- 始终调用
JApplet的init()和start()方法。 -
isShowing()和isDisplayable()始终返回true,即使小程序未显示。 - 当它不显示时,
paint()方法不会被调用。 - When the applet doesn't show, a transparent rectangular area (of the window background colour) where the applet should have been can be noticed when the text in the window is selected.由此我认为小程序区域正确放置在窗口中。
我的代码:
MyApplet.java
package mypackage;
public class MyApplet extends JApplet {
public void init(){
super.init();
getContentPane().add(new JLabel("Hello"), BorderLayout.CENTER);
System.out.println("init ok");
}
public void start() {
super.start();
System.out.println("s parent:"+this.getParent());
System.out.println("s disp:"+this.isDisplayable());
System.out.println("s showing:"+this.isShowing());
}
public void paint(Graphics g){
System.out.println("painting");
super.paint(g);
}
}
test.html
<HTML>
<SCRIPT language='JavaScript'>
var jarpath='./myjar.jar';
var classname='mypackage.MyApplet';
function opentest(){
applet=window.open('', '', "height=420,width=620");
applet.document.write(
'<html><body>'+
'<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"'+
'width=600 height=400>'+
'<param name=code value="'+classname+'">'+
'<param name=archive value="'+jarpath+'">'+
'<param name=type value="application/x-java-applet;version1.3">'+
'<COMMENT>'+
'<EMBED code="'+classname+'" archive="'+jarpath+'" ' +
'type="application/x-java-applet;version=1.3" ' +
'width=600 height=400 ' +
'>'+
'<NOEMBED>' +
'unavailable'+
'</NOEMBED>'+
'</EMBED>' +
'</COMMENT>' +
'</OBJECT>'+
'</body></html>' );
}
</SCRIPT>
<BODY>
Click here to open <A onclick="opentest()">Test</A>
</BODY>
</HTML>
控制台输出
50% 的时间我点击“测试”,新窗口包含“你好”标签。如前所述,另外 50% 的时间窗口是空白的。 当小程序加载和不加载时,控制台输出似乎相同。似乎无可指责!
security: property package.access value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.
security: property package.access new value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.javaws
security: property package.access value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.javaws
security: property package.access new value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.javaws,com.sun.deploy
security: property package.access value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.javaws,com.sun.deploy
security: property package.access new value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.javaws,com.sun.deploy,com.sun.jnlp
security: property package.definition value null
security: property package.definition new value com.sun.javaws
security: property package.definition value com.sun.javaws
security: property package.definition new value com.sun.javaws,com.sun.deploy
security: property package.definition value com.sun.javaws,com.sun.deploy
security: property package.definition new value com.sun.javaws,com.sun.deploy,com.sun.jnlp
security: property package.access value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.javaws,com.sun.deploy,com.sun.jnlp
security: property package.access new value sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.javaws,com.sun.deploy,com.sun.jnlp,org.mozilla.jss
security: property package.definition value com.sun.javaws,com.sun.deploy,com.sun.jnlp
security: property package.definition new value com.sun.javaws,com.sun.deploy,com.sun.jnlp,org.mozilla.jss
basic: Added progress listener: sun.plugin.util.GrayBoxPainter$GrayBoxProgressListener@137c60d
basic: Plugin2ClassLoader.addURL parent called for file:/E:/Java/eclipse/MyApplet/myjar.jar
network: Cache entry not found [url: file:/E:/Java/eclipse/MyApplet/myjar.jar, version: null]
network: Cache entry not found [url: file:/E:/Java/eclipse/MyApplet/myjar.jar, version: null]
basic: Applet loaded.
basic: Applet resized and added to parent container
basic: PERF: AppletExecutionRunnable - applet.init() BEGIN ; jvmLaunch dt 145396 us, pluginInit dt 331540 us, TotalTime: 476936 us
init ok
basic: Applet initialized
basic: Removed progress listener: sun.plugin.util.GrayBoxPainter$GrayBoxProgressListener@137c60d
basic: Applet made visible
basic: Starting applet
basic: completed perf rollup
s parent:sun.plugin2.main.client.PluginEmbeddedFrame[frame0,0,0,600x400,invalid,layout=java.awt.BorderLayout,title=,resizable,normal]
s disp:true
s showing:true
basic: Applet started
basic: Told clients applet is started
更新
感谢 Ates,尝试以下超时可使 Applet 100% 正确显示。
function opentest(){
applet=window.open('', '', "height=420,width=620");
setTimeout(function () {
applet.document.write( ... ); } ,
1000 );
}
- 正如 Ates 所提到的,我们现在看到我们的弹出窗口不喜欢直接
document.writes! - 这是正确的行为吗?我应该在写入文档之前等待一些信号吗?
- 我应该以不同的方式解决这个问题吗?我可以添加某种监听器来知道何时可以安全地拨打
document.write()?
【问题讨论】:
标签: java javascript google-chrome