欢迎来到天天文库
浏览记录
ID:65470814
大小:191.50 KB
页数:30页
时间:2022-01-09
《j2-8对话框[1]》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、第十章Java的窗口、菜单、对话框Java的AWT包提供三个显示窗口的组件:window,Frame和Dialog。10.1Java窗口AWTwidnow类允许用户创建包含applet的独立于浏览器窗口的“窗口”,即单独弹出的窗口。其具有自己的标题、尺寸以及菜单等。但是Window类不能被直接使用,它只提供窗口的基本行为。通常由其子类Frame和Dialog代替widnows类。而且在Frame类中可以实现菜单功能。Frame-框架Frame是一个完整的窗口类,它包括边界、标题和一个可选的菜单栏。
2、框架的大小可以调整。Frame类的几个实例方法:(P165)setBounds(inta,intb,intwidth,intheight)ContainerWindowsFrameDialogsetSize(inta,intb)setBackground(Colorc)setVisible(booleanb)validate()setTitle(Stringname)getTitle()setResizable(booleanb)getMenuBar():返回一个MenuBarsetMenuBar
3、(MenuBar)下面这个例子是Frame的简单应用的例子。通过单击Applet中的一个按钮来创建Frame框架importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassApplet2extendsApplet{publicvoidinit(){setBackground(Color.lightGray);ButtonnewFrame=newButton("CreatFrame");add(newFrame);ne
4、wFrame.addActionListener(newButtonListener());}}classButtonListenerimplementsActionListener{intcount;publicvoidactionPerformed(ActionEvente){Framef=newFrame();Buttonclose=newButton("ClosethisFrame");close.addActionListener(newCloseListener());f.add(cl
5、ose);f.addWindowListener(newWListener());f.validate();f.setTitle("Frame"+Integer.toString(count));f.show();showStatus("CreatFrame"+Integer.toString(count));count++;}}classCloseListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){((
6、Frame)((Button)e.getSource()).getParent()).dispose();}}classWListenerextendsWindowAdapter{//适配器P173publicvoidwindowClosing(WindowEvente){((Frame)e.getSource()).dispose();}}WindowAdapter类适配器可以代替一个接口来处理事件。当java提供处理事件的接口多于一个方法时,java相应地就提供一个适配器类。适配器已经实现了相
7、应的接口。因而可以用WindowAdapter的子类创建的对象做监视器。在子类中overridding所需要的接口方法即可。例如:文件名Mywindow.java,本程序说明了如何应用Frame类importjava.awt.*;importjava.awt.event.*;classmycanvasextendsCanvas//画布类{mycanvas(){getSize();}publicvoidpaint(Graphicsg){setBackground(Color.cyan);g.setC
8、olor(Color.red);g.drawString("iamdrawingonthecanvasofthewindow",50,60);}publicDimensiongetPreferredsize(){returnnewDimension(200,400);}}classmywindowListenerextendsWindowAdapter//适配器{publicvoidwindowClosing(WindowEvente){System.exit(0);}}class
此文档下载收益归作者所有