您好,欢迎访问三七文档
第九章窗口、菜单和对话框本章的任务:介绍图形用户界面的高级组件。这些组件包括窗口、菜单和对话框等,它们可以构造出标准GUI应用程序。最后,简要介绍JavaSwing的使用方法。本章主要内容1.窗口2.菜单3.对话框4.Swing简介9.1窗口Java的窗口由Frame类生成,遗憾的是它不具有关闭功能,必须要在程序中实现窗口事件监听接口并编写关闭窗口的代码才行。本节介绍创建可关闭窗口的几种方法以及窗口的使用。9.1.1创建可关闭窗口例9.1方式1:创建一个可关闭的空白窗口,如图所示。importjava.awt.*;importjava.awt.event.*;classW1extendsFrameimplementsWindowListener{W1(){super(Window1);setSize(350,200);setVisible(true);addWindowListener(this);}publicvoidwindowClosing(WindowEvente){System.exit(0);}publicvoidwindowOpened(WindowEvente){}publicvoidwindowClosed(WindowEvente){}publicvoidwindowIconified(WindowEvente){}publicvoidwindowDeiconified(WindowEvente){}publicvoidwindowActivated(WindowEvente){}publicvoidwindowDeactivated(WindowEvente){}publicstaticvoidmain(Stringargs[]){newW1();}}9.1.2窗口事件适配器例9.2方式2:创建一个可关闭的空白窗口。importjava.awt.*;importjava.awt.event.*;publicclassW2extendsFrame{W2(){super(Window2);setSize(350,200);setVisible(true);addWindowListener(newWin());}publicstaticvoidmain(String[]args){newW2();}}classWinextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){System.exit(0);}}窗口事件适配器是为方便程序员而专门设计的类。定义:publicabstractclassWindowAdapterextendsObjectimplementsWindowListener{publicWindowAdapter()//构造方法publicvoidwindowOpened(WindowEvente)//窗口被打开后调用publicvoidwindowClosing(WindowEvente)//窗口被关闭时调用publicvoidwindowClosed(WindowEvente)//窗口被关闭后调用publicvoidwindowIconified(WindowEvente)//窗口被缩小为图标时调用publicvoidwindowDeiconified(WindowEvente)//窗口被复原时调用publicvoidwindowActivated(WindowEvente)//窗口激活时调用publicvoidwindowDeactivated(WindowEvente)//窗口失活时调用}9.1.3匿名类例9.3方式3:创建一个可关闭的空白窗口。importjava.awt.*;importjava.awt.event.*;publicclassW3{publicstaticvoidmain(String[]args){Framef=newFrame(Window3);f.setSize(350,200);f.setVisible(true);f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}}9.1.4在窗口中加入组件例9.4在窗口中添加按钮,点击“显示”按钮可以显示文字,点击“退出”按钮可关闭窗口,同时窗口右上角的关闭按钮也起作用。如图所示。importjava.awt.*;importjava.awt.event.*;publicclassW4extendsFrameimplementsActionListener{Buttonbtn1,btn2;TextFieldf;W4(){super(Window4);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(350,200);btn1=newButton(显示);btn2=newButton(退出);f=newTextField(20);setLayout(newFlowLayout());add(f);add(btn1);add(btn2);btn1.addActionListener(this);btn2.addActionListener(this);show();}publicstaticvoidmain(Stringargs[]){newW4();}publicvoidactionPerformed(ActionEvente){f.setText(你按下了“+e.getActionCommand()+”按钮);if(e.getSource()==btn2){for(inti=0;i100000000;i++);System.exit(0);}}}9.1.5多重窗口例9.5设计一个多重窗口的程序,如图所示。importjava.awt.*;importjava.awt.event.*;publicclassW5extendsFrameimplementsActionListener{Buttonbtn;W5(){super(Window5);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(350,200);btn=newButton(打开新窗口);setLayout(newFlowLayout());add(btn);btn.addActionListener(this);setLocation(200,100);show();}publicstaticvoidmain(Stringargs[]){newW5();}publicvoidactionPerformed(ActionEvente){newW5();setLocation(getX()+20,getY()+20);toFront();}}9.2菜单菜单是图形用户界面的重要组成部分。由菜单条(MenuBar)、菜单(Menu)、菜单项(MenuItem)和复选菜单项(CheckboxMenuItem)等对象组成。9.2.1为窗口加入菜单例9.6在窗口中添加菜单,如图所示。importjava.awt.*;importjava.awt.event.*;publicclassW6extendsFrameimplementsActionListener{Panelp=newPanel();Buttonbtn=newButton(退出);MenuBarmb=newMenuBar();//以下生成菜单组件对象Menum1=newMenu(文件);MenuItemopen=newMenuItem(打开);MenuItemclose=newMenuItem(关闭);MenuItemexit=newMenuItem(退出);Menum2=newMenu(编辑);MenuItemcopy=newMenuItem(复制);MenuItemcut=newMenuItem(剪切);MenuItempaste=newMenuItem(粘贴);Menum3=newMenu(帮助);MenuItemcontent=newMenuItem(目录);MenuItemindex=newMenuItem(索引);MenuItemabout=newMenuItem(关于);W6(){super(Window6);setSize(350,200);add(South,p);p.add(btn);btn.addActionListener(this);m1.add(open);//将菜单项加入到菜单中m1.add(close);m1.addSeparator();//将分隔条加入到菜单中m1.add(exit);exit.addActionListener(this);//注册菜单项的事件监听器mb.add(m1);//将菜单加入到菜单条中m2.add(copy);m2.add(cut);m2.add(paste);mb.add(m2);m3.add(content);m3.add(index);m3.addSeparator();m3.add(about);mb.add(m3);setMenuBar(mb);//显示菜单条show();}publicstaticvoidmain(Stringargs[]){newW6();}publicvoidactionPerformed(ActionEvente){if(e.getActionCommand()==退出)System.exit(0);}}9.2.2菜单综合应用例9.7菜单综合应用,如图所示。importjava.awt.*;importjava.awt.event.*;publicclassW7extendsFrameimplementsActionListener,ItemListener{TextFieldmsg=newTextField();MenuBarmb=newMenuBar();Menum1=newMenu(菜单);Menum2=newMenu(二级菜单);MenuItemitem=newMenuItem(普通菜单项,newMenuShortcut('p'));CheckboxMenuItemcheckbox=newCheckboxMenuItem(复选菜单项);MenuItemexit=newMenuItem(退出);;W7(){setTitle(菜单综合应用);setSize(350,200);add(msg);mb.add(m1);m1.add(m2);//将二级菜单m2加入到m1中checkbox.setState(true);//设定复选菜单项为选中m1.add(item);m1.add(checkbox);m1.addSeparator();m1.add(exit);m2.add(菜单项A);//为二级菜单m2加入菜单项m2.add(菜单项B);item.addActionListener(this);//注册事件监听器checkbox.addItemListener(this);exit.addActionListener(this);setMenuBar(mb);//显示菜单show();//显示窗口}publicvoidactionPerformed(ActionEvente){if(e.getSource()==exit)System.exit(0);elsemsg.setText(e.getActionCommand()+被打开);}publicvoiditem
本文标题:窗口、菜单和对话框
链接地址:https://www.777doc.com/doc-5523417 .html