java点击按钮关闭当前窗口弹窗怎么关掉?

importjavax.swing.*;importjava.util.Random;importjava.awt.*;importjava.awt.event.*;importjavax.swing.JLabel.*;publicclassMultiextendsJFrameimplementsActionListener{privat...import javax.swing.*;import java.util.Random;import java.awt.*;import java.awt.event.*;import javax.swing.JLabel.*;public class Multi extends JFrame implements ActionListener{private JLabel label;private JLabel label1;private JTextField field;private JButton button;public Random random= new Random();//随机变量int a=1+random.nextInt(9);int b=1+random.nextInt(9);public static void main(String args[])//main方法{Multi frame=new Multi();frame.setSize(400,400);frame.createGUI();frame.show();}private void createGUI()//建立用户界面{setDefaultCloseOperation(EXIT_ON_CLOSE);Container window=getContentPane();window.setLayout(new FlowLayout());label=new JLabel("how much is "+a+" times "+b+"");window.add(label);field=new JTextField(20);window.add(field);field.addActionListener(this);button=new JButton(" OK ");window.add(button);button.addActionListener(this);//label1=new JLabel("");//window.add(label1);}public void actionPerformed(ActionEvent event)//静态方法play{int e=1;while(e==1){int d=1;while(d==1){int number = Integer.parseInt(field.getText());int c=a*b;if (c==number){d=0;//结束本循环}else{d=1;String message1=String.format("No.Please try again!");JOptionPane.showMessageDialog( null, message1 );}}String message2=String.format( "very good!");JOptionPane.showMessageDialog( null, message2 );}}}展开
}
一、JS的三种最常见的对话框//====================== JS最常用三种弹出对话框 ========================//弹出对话框并输出一段提示信息function ale() {//弹出一个对话框alert("提示信息!");}//弹出一个询问框,有确定和取消按钮function firm() {//利用对话框返回的值 (true 或者 false)if (confirm("你确定提交吗?")) {alert("点击了确定");}else {alert("点击了取消");}}//弹出一个输入框,输入一段文字,可以提交function prom() {var name = prompt("请输入您的名字", ""); //将输入的内容赋给变量 name ,//这里需要注意的是,prompt有两个参数,前面是提示的话,后面是当对话框出来后,在对话框里的默认值if (name)//如果返回的有内容{alert("欢迎您:" + name)}}二、点击按钮时常用的6中提示框和操作返回javaScript:window.location.reload();//返回当前页并刷新三、弹出独立窗口//关闭,父窗口弹出对话框,子窗口直接关闭this.Response.Write("");//关闭,父窗口和子窗口都不弹出对话框,直接关闭this.Response.Write("this.Response.Write("{top.opener =null;top.close();}");this.Response.Write("");//弹出窗口刷新当前页面width=200 height=200菜单。菜单栏,工具条,地址栏,状态栏全没有this.Response.Write("");//弹出窗口刷新当前页面this.Response.Write("");this.Response.Write("");//弹出提示窗口跳到webform2.aspx页(在一个IE窗口中)this.Response.Write(" ");//关闭当前子窗口,刷新父窗口this.Response.Write("");this.Response.Write("");//子窗口刷新父窗口this.Response.Write("");this.Response.Write("");//弹出提示窗口.确定后弹出子窗口(WebForm2.aspx)this.Response.Write("");//弹出提示窗口,确定后,刷新父窗口this.Response.Write("");//弹出相同的一页//Response.Write("parent.mainFrameBottom.location.href='yourwebform.aspx?temp=" +str+"';");参数解释:window.open 弹出新窗口的命令;'page.html' 弹出窗口的文件名;'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替;height=100 窗口高度;width=400 窗口宽度;top=0 窗口距离屏幕上方的象素值;left=0 窗口距离屏幕左侧的象素值;toolbar=no 是否显示工具栏,yes为显示;menubar,scrollbars 表示菜单栏和滚动栏。resizable=no 是否允许改变窗口大小,yes为允许;location=no 是否显示地址栏,yes为允许;status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;'newwin':隐藏菜单栏地址栏工具条四、弹出窗口实例演示//1、最基本的弹出窗口代码window.open ('page.html')//2、经过设置后的弹出窗口window.open('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no') //这句要写成一行//参数解释://window.open 弹出新窗口的命令;//'page.html' 弹出窗口的文件名;//'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替;//height=100 窗口高度;//width=400 窗口宽度;//top=0 窗口距离屏幕上方的象素值;//left=0 窗口距离屏幕左侧的象素值;//toolbar=no 是否显示工具栏,yes为显示;//menubar,scrollbars 表示菜单栏和滚动栏。//resizable=no 是否允许改变窗口大小,yes为允许;//location=no 是否显示地址栏,yes为允许;//status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;//3、用函数控制弹出窗口function openwin() {window.open("page.html", "newwindow", "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") //写成一行}$(document).ready(fucntion(){openwin();});//这里定义了一个函数openwin(),函数内容就是打开一个窗口。在调用它之前没有任何用途。怎么调用呢?//方法一: 浏览器读页面时弹出窗口;
//方法二: 浏览器离开页面时弹出窗口;
//方法三:用一个连接调用://打开一个窗口//注意:使用的“#”是虚连接。//方法四:用一个按钮调用:////4、同时弹出2个窗口function openwin() {window.open("page.html", "newwindow", "height=100, width=100, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=n o, status=no")//写成一行window.open("page2.html", "newwindow2", "height=100, width=100, top=1 00, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, loca tion=no, status=no")//写成一行}//为避免弹出的2个窗口覆盖,用top和left控制一下弹出的位置不要相互覆盖即可 。最后用上面说过的四种方法调用即可。//注意:2个窗口的name(newwindows和newwindow2)不要相同,或者干脆全部为空。//5、主窗口打开文件1.htm,同时弹出小窗口page.htmlfunction openwin() {window.open("page.html", "", "width=200,height=200")}//调用:open//6、弹出的窗口之定时关闭控制//下面我们再对弹出的窗口进行一些控制,效果就更好了。如果我们再将一小段 代码加入弹出的页面(注意是加入page.html的HTML中,不是主页面中),让它10秒后自动关闭是不是更酷了?//首先,将如下代码加入page.html文件的区:
function closeit() {setTimeout("self.close()", 10000) //毫秒}//页面加载完成调用关闭事件$(document).ready(fucntion(){closeit();});//7、在弹出窗口中加上一个关闭按钮////8、内包含的弹出窗口-一个页面两个窗口//上面的例子都包含两个窗口,一个是主窗口,另一个是弹出的小窗口。通过下面的例子,你可以在一个页面内完成上面的效果。function openwin() {OpenWindow = window.open("", "newwin", "height=250, width=250,toolbar=no ,scrollbars=" + scroll + ",menubar=no");//写成一行OpenWindow.document.write("例子")
OpenWindow.document.write("")
OpenWindow.document.write("")
OpenWindow.document.write("New window opened!")OpenWindow.document.write("")OpenWindow.document.write("")OpenWindow.document.close()}//打开一个窗口////9、终极应用--弹出的窗口之Cookie控制//回想一下,上面的弹出窗口虽然酷,但是有一点小毛病,比如你将上面的脚本放在一个需要频繁经过的页面里(例如首页),那么每次刷新这个页面,窗口都会弹出一次,我们使用cookie来控制一下就可以了。//首先,将如下代码加入主页面HTML的区:
function openwin() {window.open("page.html", "", "width=200,height=200")}function get_cookie(Name) {var search = Name + "="var returnvalue = "";if (document.cookie.length > 0) {offset = document.cookie.indexOf(search)if (offset != -1) {offset += search.lengthend = document.cookie.indexOf(";", offset);if (end == -1)end = document.cookie.length;returnvalue = unescape(document.cookie.substring(offset, end))}}return returnvalue;}function loadpopup() {if (get_cookie('popped') == '') {openwin()document.cookie = "popped=yes"}}//然后,用(注意不是openwin而是loadpop啊!)替换主页面中原有的这一句即可。你可以试着刷新一下这个页面或重新进入该页面,窗口再也不会弹出了。真正的Pop-Only-Once!
参考文章:http://blog.csdn.net/xiaotaoqibao/article/details/6797089}

我要回帖

更多关于 java点击按钮关闭当前窗口 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信