《JAVA程序设计题:-文本编辑器设计资料(共15页).doc》由会员分享,可在线阅读,更多相关《JAVA程序设计题:-文本编辑器设计资料(共15页).doc(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上JAVA程序设计题: 文本编辑器设计 【问题描述】 实现类似Windows操作系统“记事本”的功能。它是一个简单的文本文档编辑器,可以创建、查看或编辑文本文件。【实验目的】要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件处理机制,会查看API documentation完成设计任务,熟练文件流的操作。【基本功能】设计一个文本编辑器,通过菜单组件可以创建、查看或编辑文本文件。增加对文件的存贮、剪切和黏贴等功能功能的其他扩展package com.huangyilun.work;import java.awt.*;import jav
2、a.awt.event.*;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import javax.swing.*;public class Notice_B extends JFrame public static void main(String args) new Notice_B();public Notice_B() super(新建文本文件);setUpUIComponent();setUpEventListener();setVisible(true);this.showF
3、orm();private void showForm()/侦听窗口关闭操作setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) dispose(); System.exit(0); );private void setUpUIComponent() setSize(640, 480);/ 菜单栏JMenuBar menuBar = new JMenuBar();/ 设置文件菜单JM
4、enu fileMenu = new JMenu(文件);menuOpen = new JMenuItem(打开);/ 快捷键设置menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);menuSave = new JMenuItem(保存);menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK);menuSaveAs = new JMenuItem(另存为);menuClos
5、e = new JMenuItem(关闭);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);/ 设置编辑菜单JMenu editMenu = new JMenu(编辑);
6、menuCut = new JMenuItem(剪切);menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK);menuCopy = new JMenuItem(复制);menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK);menuPaste = new JMenuItem(粘贴);menuPaste.setAccelerator(KeyStroke.getKeyStroke(
7、KeyEvent.VK_V,InputEvent.CTRL_MASK);editMenu.add(menuCut);editMenu.add(menuCopy);editMenu.add(menuPaste);menuBar.add(fileMenu);menuBar.add(editMenu);setJMenuBar(menuBar);/ 文字编辑区域textArea = new JTextArea();textArea.setFont(new Font(宋体, Font.PLAIN, 16);textArea.setLineWrap(true);JScrollPane panel = ne
8、w JScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);Container contentPane = getContentPane();contentPane.add(panel, BorderLayout.CENTER);/ 状态栏stateBar = new JLabel(未修改);stateBar.setHorizontalAlignment(SwingConstants.LEFT);stateBar.s
9、etBorder(BorderFactory.createEtchedBorder();contentPane.add(stateBar, BorderLayout.SOUTH);popUpMenu = editMenu.getPopupMenu();private void setUpEventListener() / 按下窗口关闭钮事件处理addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) closeFile(););/ 菜单 - 打开menuOpen.addActionListene
10、r(new ActionListener() public void actionPerformed(ActionEvent e) openFile(););/ 菜单 - 保存menuSave.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) saveFile(););/ 菜单 - 另存为menuSaveAs.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) saveF
11、ileAs(););/ 菜单 - 关闭文件menuClose.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) closeFile(););/ 菜单 - 剪切menuCut.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) cut(););/ 菜单 - 复制menuCopy.addActionListener(new ActionListener() public vo
12、id actionPerformed(ActionEvent e) copy(););/ 菜单 - 粘贴menuPaste.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) paste(););/ 编辑区键盘事件textArea.addKeyListener(new KeyAdapter() public void keyTyped(KeyEvent e) processTextArea(););/ 编辑区鼠标事件textArea.addMouseListener(new Mous
13、eAdapter() public void mouseReleased(MouseEvent e) if (e.getButton() = MouseEvent.BUTTON3)popUpMenu.show(editMenu, e.getX(), e.getY();public void mouseClicked(MouseEvent e) if (e.getButton() = MouseEvent.BUTTON1)popUpMenu.setVisible(false););private void openFile() if (isCurrentFileSaved() / 文件是否为保存
14、状态open(); / 打开 else / 显示对话框int option = JOptionPane.showConfirmDialog(null, 文件已修改,是否保存?,保存文件?, JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE, null);switch (option) / 确认文件保存case JOptionPane.YES_OPTION:saveFile(); / 保存文件break;/ 放弃文件保存case JOptionPane.NO_OPTION:open();break;private boolean isCu
15、rrentFileSaved() if (stateBar.getText().equals(未修改) return true; else return false;private void open() JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(this); File file = chooser.getSelectedFile(); currFile = file; String str = ; try FileInputStream fileinput = new FileInputStream(f
16、ile.getAbsolutePath(); int x = fileinput.available(); byte b = new bytex; fileinput.read(b); str = new String(b); catch(Exception e) e.printStackTrace(); if (file != null) this.textArea.setText(str);private void saveFile() String str = this.textArea.getText();if(currFile != null & !currFile.equals()
17、tryFileWriter out = new FileWriter(currFile);out.write(str);out.close();catch(Exception e)e.printStackTrace();elsesaveFileAs();stateBar.setText(已保存);private void saveFileAs() JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(this); File file = chooser.getSelectedFile(); currFile = fi
18、le; String str = this.textArea.getText(); if(currFile != null & !currFile.equals() try FileWriter out = new FileWriter(currFile); out.write(str); out.close(); catch(Exception e) e.printStackTrace(); stateBar.setText(已保存);private void closeFile() / 是否已保存文件if (isCurrentFileSaved() / 释放窗口资源,而后关闭程序dispo
19、se(); else int option = JOptionPane.showConfirmDialog(null, 文件已修改,是否保存?,保存文件?, JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE, null);switch (option) case JOptionPane.YES_OPTION:saveFile();break;case JOptionPane.NO_OPTION:dispose();private void cut() textArea.cut();stateBar.setText(已修改);popUpM
20、enu.setVisible(false);private void copy() textArea.copy();popUpMenu.setVisible(false);private void paste() textArea.paste();stateBar.setText(已修改);popUpMenu.setVisible(false);private void processTextArea() stateBar.setText(已修改);private JMenuItem menuOpen;private JMenuItem menuSave;private JMenuItem menuSaveAs;private JMenuItem menuClose;private JMenu editMenu;private JMenuItem menuCut;private JMenuItem menuCopy;private JMenuItem menuPaste;private JTextArea textArea;private JLabel stateBar;private JPopupMenu popUpMenu;private File currFile;专心-专注-专业