1、JAVA程序设计题 文本编辑器设计资料JAVA程序设计题: 文本编辑器设计 【问题描述】 实现类似Windows操作系统“记事本”的功能。它是一个简单的文本文档编辑器,可以创建、查看或编辑文本文件。【实验目的】要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件处理机制,会查看API documentation完成设计任务,熟练文件流的操作。【基本功能】设计一个文本编辑器,通过菜单组件可以创建、查看或编辑文本文件。增加对文件的存贮、剪切和黏贴等功能功能的其他扩展package com.huangyilun.work;import java.awt.*;impo
2、rt 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(); setUpEventListe
3、ner(); 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); / 菜单栏 JMe
4、nuBar 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.C
5、TRL_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(); / 分隔线 fil
6、eMenu.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
7、 = 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.setFon
8、t(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); / 状态栏 st
9、ateBar = new JLabel(未修改); stateBar.setHorizontalAlignment(SwingConstants.LEFT); stateBar.setBorder(BorderFactory.createEtchedBorder(); contentPane.add(stateBar, BorderLayout.SOUTH); popUpMenu = editMenu.getPopupMenu(); private void setUpEventListener() / 按下窗口关闭钮事件处理 addWindowListener(new WindowAdapt
10、er() 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(); ); / 菜单 -
11、 另存为 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 v
12、oid 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.addKeyList
13、ener(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 (
14、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 (optio
15、n) / 确认文件保存 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.showOp
16、enDialog(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 (fi
17、le != null) this.textArea.setText(str); private void saveFile() 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(); else saveFileAs(); stateBar.setText(已保存);
18、 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); ou
19、t.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); sw
20、itch (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(); stateBa
21、r.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;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1