JAVA程序设计题:-文本编辑器设计资料.doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateJAVA程序设计题:-文本编辑器设计资料JAVA程序设计题:-文本编辑器设计资料JAVA程序设计题: 文本编辑器设计 【问题描述】 实现类似Windows操作系统“记事本”的功能。它是一个简单的文本文档编辑器,可以创建、查看或编辑文本文件。【实验目的】要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件处理机制,会查看API documentation完成设计任务,熟练文件流的操作。【基本功能】设计一个文本编辑器,通过菜单组件可以创建、查看或编辑文本文件。增加对文件的存贮、剪切和黏贴等功能功能的其他扩展package com.huangyilun.work;import java.awt.*;import java.awt.event.*;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import javax.swing.*;public class Notice_B08010332 extends JFrame public static void main(String args) new Notice_B08010332();public Notice_B08010332() super("新建文本文件");setUpUIComponent();setUpEventListener();setVisible(true);this.showForm();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();/ 设置文件菜单JMenu 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("另存为");menuClose = 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("编辑");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(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 = new 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.setBorder(BorderFactory.createEtchedBorder();contentPane.add(stateBar, BorderLayout.SOUTH);popUpMenu = editMenu.getPopupMenu();private void setUpEventListener() / 按下窗口关闭钮事件处理addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) closeFile(););/ 菜单 - 打开menuOpen.addActionListener(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) saveFileAs(););/ 菜单 - 关闭文件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 void 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 MouseAdapter() 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() / 文件是否为保存状态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 isCurrentFileSaved() 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(file.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("")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 = file; 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() / 释放窗口资源,而后关闭程序dispose(); 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("已修改");popUpMenu.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;-