当浏览器大小改变时,如何调整Java小程序的大小?

8
我有一个java小程序,使用方法嵌入在html中。我想在浏览器窗口大小改变时调整小程序的大小。我在互联网上找到了一些解决方案,但它们都是基于已弃用的标签的。

此外,在FireBug中尝试对我的元素进行setSize()调用时,它会调整小程序内容,但不会调整小程序视口。也就是说,给Java分配的显示区域没有变化。

当前代码大致如下:

<object
    id='MyApplet1'
    width='300' height='200'
    classid='clsid:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
    codebase='http://java.sun.com/update/1.6.0/jinstall-6-windows-i586.cab#Version=1,6,0,0'>
    <param name='type' value='application/x-java-applet;version=1.6'>
    <param name='scriptable' value='false'>
    <param name='codebase' value='foo'>
    <param name='code' value='bar'>
    <param name='archive' value='baz'>
    <param name='arg1' value='A'>
    <param name='arg2' value='B'>
    <comment>
        <embed
            id='MyApplet2'
            width='300' height='200'
            pluginspage='http://java.sun.com/products/plugin/index.html#download'
            type='application/x-java-applet;version=1.6'
            scriptable='false'
            codebase='foo'
            code='bar'
            archive='baz'
            arg1='A'
            arg2='B'>
            <noembed>
            </noembed>
        </embed>
    </comment>
</object>
<script type="text/javascript"> 
function resize() {
  min_width = 300;
  min_height = 200;
  frame_width = 0;
  frame_height = 0;
  if(parseInt(navigator.appVersion) > 3) {
    if(navigator.appName=='Netscape') {
      frame_width = window.innerWidth;
      frame_height = window.innerHeight;
    }
    if (navigator.appName.indexOf('Microsoft') != -1) {
      frame_width = document.body.offsetWidth;
      frame_height = document.body.offsetHeight;
    }
  }
  frame_width *= 0.78;
  frame_height *= 0.78;
  applet_width = frame_width > min_width ? frame_width : min_width;
  applet_height = frame_height > min_height ? frame_height : min_height;
  document.getElementById('MyApplet1').setSize(applet_width, applet_height);
  document.getElementById('MyApplet2').setSize(applet_width, applet_height);
}
window.onResize = resize;
window.onLoad = resize;
</script> 

你的调整大小函数被调用了吗?(例如,到目前为止出现了什么错误?) - scunliffe
2个回答

4

请尝试替换以下代码:

document.getElementById('MyApplet1').setSize(applet_width, applet_height);
document.getElementById('MyApplet2').setSize(applet_width, applet_height);

使用:

document.getElementById('MyApplet1').style.height = applet_height + "px";
document.getElementById('MyApplet1').style.width = applet_width + "px";

document.getElementById('MyApplet2').style.height = applet_height + "px";
document.getElementById('MyApplet2').style.width = applet_width + "px";

我支持这种方法。我已经基本上用相同的方法来调整ActiveX控件的大小。它应该也适用于Applet。 - CodingWithSpike
这个有效!我也在做同样的事情,使用setSize()。它被调用了,但是小程序没有重新调整大小。谢谢! - Steven Mai

1
为什么不使用百分比的宽度和高度呢?我通常在我的小程序中使用以下代码:
<applet codebase="http://localhost:89" archive="npaxet?version=0.0.1.28" code="main.NPaxet.class" width=100% height=90%>\
<PARAM NAME="thumbnailUrl" VALUE="http://localhost:89/thumbnail?seriesid=%s">
<PARAM NAME="dicomUrl" VALUE="http://localhost:89/dicom?imageid=%d">
<PARAM NAME="seriesCount" VALUE="11">
...
</applet>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接