版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
GUI(GraphicalUserInterface)图形化界面编程总结Java.Awt:AbstractWindowToolKit(抽象窗口工具包)Javax.Swing:图形化界面基本设置:设置窗体大小:setSize(长,宽);设置窗体位置:setLocation(距离左,距离上);setBounds(长,宽,距离左,距离上);设置布局:setLayout(newFlowLayout());使窗体可见:setVisible(true);事件监听机制:事件监听机制的特点:1,事件源。2,事件。3,监听器。4,事件处理。事件源:就是awt包或者swing包中的那些图形界面组件。事件:每一个事件源都有自己特有的对应事件和共性事件。监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。以上三者,在java中都已经定义好了。直接获取其对象来用就可以了。我们要做的事情是,就是对产生的动作进行处理Eg:编写程序,练习图形化界面编程!importjava.awt.*;importjava.awt.event.*;importjava.io.*;classMyWindowDemo{ privateFramef; privateTextFieldtf; privateButtonbut; privateTextAreata; privateDialogd; privateLabellab; privateButtonokBut; MyWindowDemo() { init(); } publicvoidinit() { f=newFrame("mywindow"); f.setBounds(300,100,600,500); f.setLayout(newFlowLayout()); tf=newTextField(60); but=newButton("转到"); ta=newTextArea(25,70); d=newDialog(f,"提示信息-self",true); d.setBounds(400,200,240,150); d.setLayout(newFlowLayout()); lab=newLabel(); okBut=newButton("确定"); d.add(lab); d.add(okBut); f.add(tf); f.add(but); f.add(ta); myEvent(); f.setVisible(true); } privatevoidmyEvent() { okBut.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { d.setVisible(false); } }); d.addWindowListener(newWindowAdapter() { publicvoidwindowClosing(WindowEvente) { d.setVisible(false); } }); tf.addKeyListener(newKeyAdapter() { publicvoidkeyPressed(KeyEvente) { if(e.getKeyCode()==KeyEvent.VK_ENTER) showDir(); } }); but.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { showDir(); } }); f.addWindowListener(newWindowAdapter() { publicvoidwindowClosing(WindowEvente) { System.exit(0); } }); } privatevoidshowDir() { StringdirPath=tf.getText(); Filedir=newFile(dirPath); if(dir.exists()&&dir.isDirectory()) { ta.setText(""); String[]names=dir.list(); for(Stringname:names) { ta.append(name+"\r\n"); } } else { Stringinfo="您输入的信息:"+dirPath+"是错误的。请重输"; lab.setText(info); d.setVisible(true); } } publicstaticvoidmain(String[]args) { newMyWindowDemo(); }}菜单:MenuBar菜单整体;Menu包含于MenuBar中;MenuItem包含于Menu中!以上3个关系添加用add();将MenuBar放进Frame中用setMenuBar();FileDialog.LOAD:打开的modeFileDialog.Save:保存的mode练习:一个简易的记事本/**写一个关于记事本的小程序@author田建@versionv1.1*/importjava.awt.*;importjava.awt.event.*;importjava.io.*;classMyMenuDemo{ privateFramef; privateTextAreata; privateMenuBarmb; privateMenume; privateMenuItemmiNew,miSave,miLoad; privateFilefile; privateFileDialogopenDia,saveDia; MyMenuDemo() { init(); } publicvoidinit() { f=newFrame("田建--记事本"); f.setBounds(300,100,650,600); ta=newTextArea(); //ta.setBounds(302,105,400,300); //f.setLayout(newFlowLayout());//为什么设置成了流式布局之后再设置文本区域没有效果 mb=newMenuBar(); me=newMenu("文件(F)"); miNew=newMenuItem("新建"); miSave=newMenuItem("保存(S)"); miLoad=newMenuItem("打开(O)"); mb.add(me); me.add(miNew); me.add(miLoad); me.add(miSave); f.setMenuBar(mb); f.add(ta); openDia=newFileDialog(f,"打开文件",FileDialog.LOAD); saveDia=newFileDialog(f,"保存文件",FileDialog.SAVE); myEvent(); f.setVisible(true); } publicvoidmyEvent() { f.addWindowListener(newWindowAdapter() { publicvoidwindowClosing(WindowEvente) { System.exit(0); } }); miLoad.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { openDia.setVisible(true); StringdirPath=openDia.getDirectory(); StringfileName=openDia.getFile(); if(dirPath==null||fileName==null) return; ta.setText(""); file=newFile(dirPath,fileName); try { BufferedReaderbufr=newBufferedReader(newFileReader(file)); Stringline=null; while((line=bufr.readLine())!=null) { ta.append(line+"\r\n"); } bufr.close(); } catch(IOExceptionex) { thrownewRuntimeException("读取失败"); } } }); miSave.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { if(file==null) { saveDia.setVisible(true); StringdirPath=saveDia.getDirectory(); StringfileName=saveDia.getFile(); if(dirPath==null||fileName==null) return; file=newFile(dirPath,fileName); } try { BufferedWriterbufw=newBufferedWriter(newFileWriter(file)); Stringtext=ta.getText(); bufw.write(text); bufw.close(); } catch(IOExceptionex) { thrownewRuntimeException(""); } } });
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖南省浏阳市2027届九年级化学第一学期期末学业质量监测模拟试题含解析
- 2027届甘肃泾川县九上化学期末调研试题含解析
- 2027届北京朝阳人大附朝阳分校九上物理期末联考试题含解析
- 2027届湖南省汨罗市沙溪中学九上化学期末联考试题含解析
- 2027届江苏省句容市华阳片区九上物理期末质量跟踪监视模拟试题含解析
- 2026中国运动毛巾市场竞争格局与品牌溢价能力分析报告
- 天津市河北区2027届九上物理期末调研模拟试题含解析
- 2027届河北省沧州孟村县联考物理九上期末综合测试模拟试题含解析
- 2026中国叶黄素酯下游应用市场拓展与商业机会分析报告
- 2026汽车轻量化技术深度研究及材料创新应用价值评估
- 分管领导与分管科室单位负责人进行廉政谈心谈话
- 续修宗谱财务制度
- 应急部13号令培训课件
- 老年康复辅助器具租赁服务实施办法
- 2025年及未来5年市场数据中国副车架市场供需现状及投资战略数据分析研究报告
- 汽轮机安全监测系统tsi课件
- 老年慢性咳嗽中西医结合诊疗方案
- 2025~2026学年广西南宁市第四十七中学九年级上学期开学考试物理试卷
- 2026考研:政治必背知识点(可下载)
- 上半年院感工作总结
- 地基基础检测现场安全作业规程及安全预案
评论
0/150
提交评论