JAVA程序设计题文本编辑器设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JAVA程序设计题:文本编辑器设计【问题描述】实现类似Windows操作系统“记事本”的功能。它是一个简单的文本文档编辑器,可以创建、查看或编辑文本文件。【实验目的】要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件处理机制,会查看APIdocumentation完成设计任务,熟练文件流的操作。【基本功能】设计一个文本编辑器,通过菜单组件可以创建、查看或编辑文本文件。增加对文件的存贮、剪切和黏贴等功能功能的其他扩展packagecom.huangyilun.work;importjava.awt.*;importjava.awt.event.*;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileWriter;importjavax.swing.*;publicclassNotice_B08010332extendsJFrame{publicstaticvoidmain(String[]args){newNotice_B08010332();}publicNotice_B08010332(){super(新建文本文件);setUpUIComponent();setUpEventListener();setVisible(true);this.showForm();}privatevoidshowForm(){//侦听窗口关闭操作setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){dispose();System.exit(0);}});}privatevoidsetUpUIComponent(){setSize(640,480);//菜单栏JMenuBarmenuBar=newJMenuBar();//设置「文件」菜单JMenufileMenu=newJMenu(文件);menuOpen=newJMenuItem(打开);//快捷键设置menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));menuSave=newJMenuItem(保存);menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));menuSaveAs=newJMenuItem(另存为);menuClose=newJMenuItem(关闭);menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));fileMenu.add(menuOpen);fileMenu.addSeparator();//分隔线fileMenu.add(menuSave);fileMenu.add(menuSaveAs);fileMenu.addSeparator();//分隔线fileMenu.add(menuClose);//设置「编辑」菜单JMenueditMenu=newJMenu(编辑);menuCut=newJMenuItem(剪切);menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));menuCopy=newJMenuItem(复制);menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));menuPaste=newJMenuItem(粘贴);menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));editMenu.add(menuCut);editMenu.add(menuCopy);editMenu.add(menuPaste);menuBar.add(fileMenu);menuBar.add(editMenu);setJMenuBar(menuBar);//文字编辑区域textArea=newJTextArea();textArea.setFont(newFont(宋体,Font.PLAIN,16));textArea.setLineWrap(true);JScrollPanepanel=newJScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);ContainercontentPane=getContentPane();contentPane.add(panel,BorderLayout.CENTER);//状态栏stateBar=newJLabel(未修改);stateBar.setHorizontalAlignment(SwingConstants.LEFT);stateBar.setBorder(BorderFactory.createEtchedBorder());contentPane.add(stateBar,BorderLayout.SOUTH);popUpMenu=editMenu.getPopupMenu();}privatevoidsetUpEventListener(){//按下窗口关闭钮事件处理addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){closeFile();}});//菜单-打开menuOpen.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){openFile();}});//菜单-保存menuSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFile();}});//菜单-另存为menuSaveAs.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFileAs();}});//菜单-关闭文件menuClose.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){closeFile();}});//菜单-剪切menuCut.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){cut();}});//菜单-复制menuCopy.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){copy();}});//菜单-粘贴menuPaste.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){paste();}});//编辑区键盘事件textArea.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){processTextArea();}});//编辑区鼠标事件textArea.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEvente){if(e.getButton()==MouseEvent.BUTTON3)popUpMenu.show(editMenu,e.getX(),e.getY());}publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});}privatevoidopenFile(){if(isCurrentFileSaved()){//文件是否为保存状态open();//打开}else{//显示对话框intoption=JOptionPane.showConfirmDialog(null,文件已修改,是否保存?,保存文件?,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){//确认文件保存caseJOptionPane.YES_OPTION:saveFile();//保存文件break;//放弃文件保存caseJOptionPane.NO_OPTION:open();break;}}}privatebooleanisCurrentFileSaved(){if(stateBar.getText().equals(未修改)){returntrue;}else{returnfalse;}}privatevoidopen(){JFileChooserchooser=newJFileChooser();chooser.showOpenDialog(this);Filefile=chooser.getSelectedFile();currFile=file;Stringstr=;try{FileInputStreamfileinput=newFileInputStream(file.getAbsolutePath());intx=fileinput.available();byteb[]=newbyte[x];fileinput.read(b);str=newString(b);}catch(Exceptione){e.printStackTrace();}if(file!=null)this.textArea.setText(str);}privatevoidsaveFile(){Stringstr=this.textArea.getText();if(currFile!=null&&!currFile.equals()){try{FileWriterout=newFileWriter(currFile);out.write(str);out.close();}catch(Exceptione){e.printStackTrace();}}else{saveFileAs();}stateBar.setText(已保存);}privatevoidsaveFileAs(){JFileChooserchooser=newJFileChooser();chooser.showOpenDialog(this);Filefile=chooser.getSelectedFile();currFile=file;Stringstr=this.textArea.getText();if(currFile!=null&&!currFi

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功