收藏 分销(赏)

java课程论文.doc

上传人:xrp****65 文档编号:7025716 上传时间:2024-12-25 格式:DOC 页数:11 大小:92.50KB 下载积分:10 金币
下载 相关 举报
java课程论文.doc_第1页
第1页 / 共11页
java课程论文.doc_第2页
第2页 / 共11页


点击查看更多>>
资源描述
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)本程序的局限性及改进方法 本程序的缺陷是在改变颜色和字体方面比较少的类型,因为不会调用程序的类还有学习程序有一定的局限,所以在颜色还有字体的设计上要有所改进,改进的方法是在课本还有课外中多去学习,找到调用方法的类,尽量做到好像window中的文本一样有很多颜色和字体可以改变。 本程序在还有一个比较致命的缺陷,当改变字体大小或者颜色,格式等,都不能个别修改,都是整体一起修改,所以比较不好,改进方向是朝着可以局部变化方向去改,可以是文本更完美。 (2)应用本程序的设计方法可以编写哪些类似的程序 应用本程序的设计方法可以设计许多方面,可以运用窗口和按钮菜单的的方法运用设计到简单的计算机的设计中来,还可以通过文本这个设计思想去设计word的程序 教师评语: 年 月 日 签名: 评分: 11
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服