JAVASCRIPT动态设置applet窗口大小(转)

本文介绍了一个JavaScript脚本,用于获取浏览器窗口的宽度和高度,并通过调整Applet组件大小实时反映这些变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 下面的脚本能准确获取大部分浏览器的窗口大小。
代码如下:
ExampleApplet.htm:
<html>
        <head>
                <title>Applet Example</title>
        </head>
        <body style="height:100%;margin:0px;">
                <object type="application/x-java-applet"
                                code="ExampleApplet.class" width="100" height="100" id="ExampleApplet">
                        <comment>
                                <applet code="ExampleApplet.class" width="100" height="100" name="ExampleApplet"></applet>
                        </comment>
                </object>        
               
                <script type="text/javascript">
                <!--
                        function getSize()
                        {               
                                var windowWidth,windowHeight;

                                if(window.innerWidth){
                                        windowWidth=window.innerWidth;
                                } else if(document.documentElement && document.documentElement.clientWidth){
                                        windowWidth=document.documentElement.clientWidth;
                                } else if(document.body){
                                        windowWidth=document.body.clientWidth;
                                }

                                if(window.innerHeight){
                                        windowHeight=window.innerHeight;
                                } else if(document.documentElement && document.documentElement.clientHeight){
                                        windowHeight=document.documentElement.clientHeight;
                                } else if(document.body){
                                        windowHeight=document.body.clientHeight;
                                }
                                
                                var oApplet=document.getElementById("ExampleApplet");
                                oApplet.height=windowHeight;
                                oApplet.width=windowWidth;
                        }
                        
                        getSize();
                        window.onresize=getSize;
                //-->
                </script>
        </body>
</html>

ExampleApplet.java:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.HeadlessException;

public class ExampleApplet extends Applet {
        public ExampleApplet() throws HeadlessException{
                super();
        }
       
        public void paint(Graphics g){
                g.drawString(getWidth()+" : "+getHeight(),20,20);
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值