




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JAVA语言程序设计 谢永超邹国胜Java课程设计1设计报告(1)本程序的功能描述本程序似于Windows记事本(Notepad)的Java程序。可以打开、新建、保存一个文本文件;对选中的文本进行各种编辑操作(设置字体、字号、字型、对齐方式、背景、前景色、复制、粘贴、剪切、查找、替换等);在文本中能够插入对象。(2)程序设计思想本程序运用简单的文本书写方式开始延伸,结合了JFame窗口的设计,设计多级菜单组建来建立此程序,程序里面包括三级菜单组建,分别为JMenuBar菜单条、JMenu菜单栏、还有JMenuItem菜单项三项菜单,在每项菜单项下在设立相应的功能条,在对应每项功能做程序设计,最终完成此程序的一个基本设计,为了方便观看,可以用一个简单的图表来表达我这次设计的思路:文本编辑器格式编辑黏贴打开 菜单保存新建退出另存为文件剪切黏贴查找复制字体字号插对像懂得想想象替换(3)核心程序清单(应有必要说明)/创建菜单条JMenuBar bar = new JMenuBar();setJMenuBar( bar ); / 设置文件菜单及其菜单项JMenu fileMenu = new JMenu( 文件 );/ 设置新建菜单项JMenuItem newItem = new JMenuItem( 新建 ); fileMenu.add( newItem );newItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event )displayText.setText(); );/ 设置打开菜单项 JMenuItem openItem = new JMenuItem( 打开 ); fileMenu.add( openItem ); openItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) fd.setTitle(打开); /设置标题 fd.show();if (fd.getFile() != null) File file = new File(fd.getFile();用从fd中取得的文件建立新文件,即打开的文件displayText.setText( );try FileReader f = new FileReader(file);BufferedReader b = new BufferedReader(f);/按行读打开的文件,然后传入文本域String s;try while (s = b.readLine() != null) displayText.append(s + n);/将给定文本追加到文本域的当前文本(即把读的内容加入文本域)f.close();b.close(); catch (IOException ex) catch (FileNotFoundException ex) else return; / 设置保存菜单项 JMenuItem saveItem = new JMenuItem( 保存 ); fileMenu.add( saveItem ); saveItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event )/ 设置另存为菜单项 JMenuItem saveAsItem = new JMenuItem( 另存为 ); fileMenu.add( saveAsItem ); saveAsItem.addActionListener( new ActionListener()这两个保存和另存为的菜单项的新建的菜单项的方法相似/ 设置退出菜单项 JMenuItem exitItem = new JMenuItem( 退出 ); fileMenu.add( exitItem ); exitItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) System.exit( 0 ); ); JMenu editMenu = new JMenu( 编辑 );/剪切菜单选项 JMenuItem cutItem = new JMenuItem( 剪切 );editMenu.add( cutItem );cutItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) selectText = displayText.getSelectedText();/取得选定的文本int start = displayText.getSelectionStart();/选定的文本的开始位置int end = displayText.getSelectionEnd();/选定的文本的结束位置displayText.replaceRange(, start, end);/*用指定替换文本替换指定开始位置与结束位置之间的文本。 这里指定替换文本为空,即为剪切了文本*/ );复制和粘贴的功能与剪切功能相似,只不过在调用的时候有所不同。/替换的实现JMenuItem swapItem = new JMenuItem( 替换 );editMenu.add( swapItem );swapItem.addActionListener(new ActionListener()public void actionPerformed( ActionEvent event )JPanel swapPanel=new JPanel();JLabel lookupLabel=new JLabel(要替换的内容);JTextField inputText=new JTextField(10);JLabel swapLabel=new JLabel(替换为:);JTextField changetoText=new JTextField(10);swapPanel.add(lookupLabel);swapPanel.add(inputText);swapPanel.add(swapLabel);swapPanel.add(changetoText);JOptionPane.showMessageDialog(null,swapPanel);String text=displayText.getText();/获得整个文本内容String changeText=text.replaceFirst(inputText.getText(),changetoText.getText();/获得替换后的内容 displayText.setText(changeText); );/删除子菜单 JMenuItem RemoveItem=new JMenuItem(删除); editMenu.add(RemoveItem); RemoveItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int start=displayText.getSelectionStart(); int end=displayText.getSelectionEnd(); displayText.replaceRange(null,start,end); );/ 颜色菜单项 String colors = 黑色, 蓝色,红色,绿色 ; JMenu colorMenu = new JMenu( 颜色 ); colorItems = new JRadioButtonMenuItem colors.length ; colorGroup = new ButtonGroup(); ItemHandler itemHandler = new ItemHandler(); / 创建颜色按钮监听器 for ( int count = 0; count colors.length; count+ ) colorItems count = new JRadioButtonMenuItem( colors count ); colorMenu.add( colorItems count ); colorGroup.add( colorItems count ); colorItems count .addActionListener( itemHandler ); 创建字体的监听差不多的构思/ 设置面板显示文本 displayText = new JTextArea();displayText.setForeground( colorvalues 0 );displayText.setFont( new Font( Serif, Font.PLAIN, 24 ) );/设置默认字体scroll=new JScrollPane( displayText,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );container.add( scroll, BorderLayout.CENTER );displayText.addKeyListener( /通过对displayText注册键盘事件来获得当前文本的 行数new KeyListener() public void keyPressed( KeyEvent event ) rowNumber = displayText.getLineCount();/获得文本区的函数 setTitle(总共 + rowNumber + 行);/设置标题 public void keyReleased( KeyEvent event )/空 public void keyTyped( KeyEvent event )/空 ); setSize( 700, 500 ); setVisible( true ); /主函数 public static void main( String args ) Notepad application = new Notepad(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); /字体和颜色监听器 private class ItemHandler implements ActionListener public void actionPerformed( ActionEvent event ) / 颜色 for ( int count = 0; count colorItems.length; count+ ) if ( colorItems count .isSelected() ) displayText.setForeground( colorvalues count ); break; / 字体 for ( int count = 0; count fonts.length; count+ ) if ( event.getSource() = fonts count ) displayText.setFont( new Font( fonts count .getText(), style, 72 ) ); break; repaint(); private class StyleHandler implements ItemListener / 处理字体风格选项 public void itemStateChanged( ItemEvent e ) style = 0; if ( styleItems 0 .isSelected() ) style += Font.BOLD; if ( styleItems 1 .isSelected() ) style += Font.ITALIC; displayText.setFont( new Font( displayText.getFont().getName(), style, 72 ) ); repaint(); 2结果分析(1)本程序的局限性及改进方法本程序的缺陷是在改变颜色和字体方面比较少的类型,因为不会调用程序的类还有学习程序有一定的
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 强-台风作用下城市广域化风场数值模拟研究
- 2023三年级英语下册 Module 4 Things we enjoy Unit 11 Mother's Day第3课时说课稿 牛津沪教版(三起)
- 第4课 作圆和弧说课稿-2025-2026学年初中信息技术(信息科技)八年级下册人教版
- 活动与创造 吹吹打打 电影歌曲中的民歌风 影视与名著说课稿-2025-2026学年初中音乐沪教版八年级下册-沪教版
- 高中音乐教学多彩歌唱艺术教案
- 初中英语单词竞赛备考详细攻略
- 7 闭合电路的欧姆定律说课稿-2025-2026学年高中物理苏教版选修3-1-苏教版2014
- 江苏省海安县实验中学高一体育 篮球说课稿2 苏教版
- 监理工程管理总结及持续改进记录
- 建筑工程安全防护标准汇编
- 变电所安全管理规章制度
- 2025江苏苏州市相城城市建设投资(集团)有限公司人员招聘考前自测高频考点模拟试题及答案详解(夺冠)
- 游乐园安全培训课件
- 2025年新生儿科常见疾病诊断试题答案及解析
- Unit2WorkingthelandIntegratedskills课件-高中英语译林版(2020)选修第一册
- 2025年广西壮族自治区省直机关公开遴选公务员笔试题及答案解析(A类)
- 2025年探伤工(二级)实操技能考试题库(附答案)
- 2025至2030中国脑深部电刺激(DBS)设备市场应用规模与重点企业发展调研报告
- 屋顶彩钢加固施工方案
- 高校财会监督与预算绩效管理协同效能优化研究
- 输液室理论知识培训课件
评论
0/150
提交评论