收藏 分享(赏)

java课程论文.doc

上传人:hyngb9260 文档编号:6748053 上传时间:2019-04-22 格式:DOC 页数:11 大小:92.50KB
下载 相关 举报
java课程论文.doc_第1页
第1页 / 共11页
java课程论文.doc_第2页
第2页 / 共11页
java课程论文.doc_第3页
第3页 / 共11页
java课程论文.doc_第4页
第4页 / 共11页
java课程论文.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、Java课程设计1设计报告(1)本程序的功能描述本程序似于 Windows记事本(Notepad)的 Java程序。可以打开、新建、保存一个文本文件;对选中的文本进行各种编辑操作(设置字体、字号、字型、对齐方式、背景、前景色、复制、粘贴、剪切、查找、替换等) ;在文本中能够插入对象。(2)程序设计思想本程序运用简单的文本书写方式开始延伸,结合了 JFame窗口的设计,设计多级菜单组建来建立此程序,程序里面包括三级菜单组建,分别为 JMenuBar菜单条、JMenu 菜单栏、还有 JMenuItem菜单项三项菜单,在每项菜单项下在设立相应的功能条,在对应每项功能做程序设计,最终完成此程序的一个基

2、本设计,为了方便观看,可以用一个简单的图表来表达我这次设计的思路:JAVA 语言程序设计 谢永超邹国胜2文本编辑器格式编辑 黏贴打开菜单保存新建退出另存为文件剪切黏贴查找复制字体字号插对像懂得想想象替换(3)核心程序清单(应有必要说明)/创建菜单条JMenuBar bar = new JMenuBar();setJMenuBar( bar ); / 设置文件菜单及其菜单项JMenu fileMenu = new JMenu( “文件“ );/ 设置新建菜单项JMenuItem newItem = new JMenuItem( “新建“ ); fileMenu.add( newItem );ne

3、wItem.addActionListener(new ActionListener() public void actionPerformed( ActionEvent event )JAVA 语言程序设计 谢永超邹国胜3displayText.setText(“); );/ 设置打开菜单项JMenuItem openItem = new JMenuItem( “打开“ ); fileMenu.add( openItem );openItem.addActionListener(new ActionListener() public void actionPerformed( ActionE

4、vent 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) dis

5、playText.append(s + “n“);/将给定文本追加到文本域的当前文本(即把读的内容加入文本域)f.close();b.close(); catch (IOException ex) catch (FileNotFoundException ex) JAVA 语言程序设计 谢永超邹国胜4else return; / 设置保存菜单项JMenuItem saveItem = new JMenuItem( “保存“ ); fileMenu.add( saveItem );saveItem.addActionListener(new ActionListener() public voi

6、d actionPerformed( ActionEvent event )/ 设置另存为菜单项JMenuItem saveAsItem = new JMenuItem( “另存为“ ); fileMenu.add( saveAsItem );saveAsItem.addActionListener(new ActionListener()这两个保存和另存为的菜单项的新建的菜单项的方法相似/ 设置退出菜单项JMenuItem exitItem = new JMenuItem( “退出“ ); fileMenu.add( exitItem );exitItem.addActionListener

7、(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 )selectT

8、ext = displayText.getSelectedText();/取得选定的文本int start = displayText.getSelectionStart();/选定的文本的开始位JAVA 语言程序设计 谢永超邹国胜5置int end = displayText.getSelectionEnd();/选定的文本的结束位置displayText.replaceRange(“, start, end);/*用指定替换文本替换指定开始位置与结束位置之间的文本。 这里指定替换文本为空,即为剪切了文本*/);复制和粘贴的功能与剪切功能相似,只不过在调用的时候有所不同。/替换的实现JMen

9、uItem 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(“替换

10、为:“);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(),ch

11、angetoText.getText();/获得替换后的内容displayText.setText(changeText); );/删除子菜单JMenuItem RemoveItem=new JMenuItem(“删除“);JAVA 语言程序设计 谢永超邹国胜6editMenu.add(RemoveItem);RemoveItem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) int start=displayText.getSelectionStart();int end=di

12、splayText.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 ( in

13、t 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

14、( colorvalues 0 );displayText.setFont( new Font( “Serif“, Font.PLAIN, 24 ) );/设置默认字体scroll=new JScrollPane( displayText,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );JAVA 语言程序设计 谢永超邹国胜7container.add( scroll, BorderLayout.CENTER );displayText.addKeyListener( /通过对

15、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 );/主函数p

16、ublic 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

17、+ )if ( colorItems count .isSelected() ) JAVA 语言程序设计 谢永超邹国胜8displayText.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 cl

18、ass 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();JAV

19、A 语言程序设计 谢永超邹国胜92结果分析(1)本程序的局限性及改进方法本程序的缺陷是在改变颜色和字体方面比较少的类型,因为不会调用程序的类还有学习程序有一定的局限,所以在颜色还有字体的设计上要有所改进,改进的方法是在课本还有课外中多去学习,找到调用方法的类,尽量做到好像window 中的文本一样有很多颜色和字体可以改变。本程序在还有一个比较致命的缺陷,当改变字体大小或者颜色,格式等,都不能个别修改,都是整体一起修改,所以比较不好,改进方向是朝着可以局部变化方向去改,可以是文本更完美。(2)应用本程序的设计方法可以编写哪些类似的程序应用本程序的设计方法可以设计许多方面,可以运用窗口和按钮菜单的的方法JAVA 语言程序设计 谢永超邹国胜10运用设计到简单的计算机的设计中来,还可以通过文本这个设计思想去设计word 的程序教师评语:年 月 日签名:评分:JAVA 语言程序设计 谢永超邹国胜11

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报