日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

打开指定大小的新窗口

發(fā)布時間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 打开指定大小的新窗口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用JavaScript打開一個彈出窗口,可以使用window對象的open()方法或showModalDialog()方法完成。

JavaBean類

public class ShowWindow {private String url; //打開窗口的鏈接地址private String openWindowStr=""; //用于保存打開窗口的JavaSript代碼private int width; //打開窗口的寬度private int height; //打開窗口的高度private String functionName; //打開窗口的JavaScript函數名public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getOpenWindowStr() {StringBuffer sb = new StringBuffer(openWindowStr);sb.append("<script language='javascript'>");sb.append("\r\n\t"); //添加換行縮進sb.append("function "+this.functionName+"(){"); //添加函數名sb.append("\r\n\t\t");//打開一個窗口時,返回一個window類型的對象returnObj,可以根據此對象來調整窗口的位置sb.append("var returnObj = window.open('"+this.url+"','window','width="+this.width+"px,height="+this.height+"px');"); sb.append("\r\n\t\t");sb.append("var x=(screen.width-"+width+")/2;"); //screen對象表示屏幕,此處設置相對于屏幕的x坐標sb.append("\r\n\t\t");sb.append("var y=(screen.height-"+height+")/2;");//此處設置相對于屏幕的y坐標sb.append("\r\n\t\t");sb.append("returnObj.moveTo(x,y);"); //調用moveTo()方法改變窗口位置sb.append("\r\n\t}");sb.append("\r\n");sb.append("</script>");return sb.toString();}public void setOpenWindowStr(String openWindowStr) {this.openWindowStr = openWindowStr;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public String getFunctionName() {return functionName;}public void setFunctionName(String functionName) {this.functionName = functionName;}public static void main(String [] args){ShowWindow s = new ShowWindow();s.setFunctionName("openWindow");s.setUrl("index.jsp");s.setWidth(500);s.setHeight(500);System.out.println(s.getOpenWindowStr());} }

OpenCenterWindow類

public class OpenCenterWindow {/*****************************************功能:該方法將生成一個字符串,該字符串用于輸出自定義的JavaScript函數(該函數用于打開指定大小的新窗口,并居中顯示)****************************************/public String open(String url,String w,String h,String no){String str="<script language='javascript'>function openwin"+no+"(){";str=str+"if ("+w+"=='0'){var winhdc=window.open('"+url+"');";str=str+"var width=0;var height=0;}else{";str=str+"var winhdc=window.open('"+url+"','','width="+w+",height="+h+"');";str=str+"var width=(screen.width-"+w+")/2;";str=str+"var height=(screen.height-"+h+")/2;}";str=str+"winhdc.moveTo(width,height);";str=str+"}</script>";return str;}}

index.jsp頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><!-- 導入打開窗口的JavaBean類 --><jsp:useBean id="myWindow" class="com.cn.zj.test.ShowWindow"></jsp:useBean><!-- 設置打開窗口的JavaScript函數名 --><jsp:setProperty property="functionName" name="myWindow" value="openWindow1"/><!-- 設置打開窗口的url地址 --><jsp:setProperty property="url" name="myWindow" value="window.jsp"/><!-- 設置打開窗口的寬度 --><jsp:setProperty property="width" name="myWindow" value="200"/><!-- 設置打開窗口的高度 --><jsp:setProperty property="height" name="myWindow" value="100"/><!-- 獲得打開窗口的JavaScript函數字符串 --><jsp:getProperty property="openWindowStr" name="myWindow" /><form action="window.jsp" method="post"><input type="button" value="打開窗口" onclick="openWindow1()"/></form></body> </html>

window.jsp頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>打開窗口</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">body{font-size: 15px;color:green;font-family: 隸書;background-color: pink;}</style></head><body>你好!我是被打開的新窗口!寬度200px,高度100px,居中顯示。</body> </html>

總結

以上是生活随笔為你收集整理的打开指定大小的新窗口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。