1、班 级:09-1学 号:540912010120指导教师:黄永丽、宋保卫、金松河 2011年7月2日一、引言11项目的名称 记事本程序12项目背景和目标 背景:掌握图形用户界面设计方法,掌握常用swing组件的使用和事件处理程序设计;掌握文件的I/O操作或程序对数据库的访问。 目标:利用eclipse开发环境,使用图形用户界面实现,能实现编辑、保存、另存为、查找替换等功能。二、需求分析21系统概述使用图形用户界面实现,能实现编辑、保存、另存为、查找替换等功能。22系统运行环境 Jdk-eclipse23功能需求描述 能实现编辑、保存、另存为、查找替换等功能。三、总体设计31开发与设计的总体思想
2、 a分析题目 b根据自己的想法搭建程序界面 c为各个菜单项添加事件处理程序32模块设计 a主界面模块图 1b.菜单栏界面模块图 2图3图4图5四、详细设计1主界面模块public class Editor extends JFrame implements ActionListener private JMenu file, edit, view, about; private JMenuBar jmb; private JTextArea jta; private JScrollPane jsp; private JPopupMenu pop; private boolean wasSave
3、d = true; private File currentFile = null; private Object text; public static void main(String args) new Editor().init(); public Editor() this.setName(文本编辑器1.0); pop = new JPopupMenu(); file = new JMenu(文件 edit = new JMenu(编辑 view = new JMenu(查看 about = new JMenu(关于 jmb = new JMenuBar(); jta = new J
4、TextArea(30, 30); jsp = new JScrollPane(jta); jta.addMouseListener(new MouseAdapter() Override public void mouseClicked(MouseEvent arg0) if (arg0.getButton() = MouseEvent.BUTTON1) pop.setVisible(false); public void mouseReleased(MouseEvent e) if (e.getButton() = MouseEvent.BUTTON3) pop.show(edit, e.
5、getX(), e.getY(); pop.setVisible(true); ); jta.getDocument().addDocumentListener(new DocumentListener() public void changedUpdate(DocumentEvent arg0) wasSaved = false; public void insertUpdate(DocumentEvent arg0) public void removeUpdate(DocumentEvent arg0) JMenuItem item; file.add(item = new JMenuI
6、tem(新建); item.addActionListener(this);打开. file.addSeparator();保存另存为.退出 edit.add(item = new JMenuItem(复制粘贴剪切 edit.addSeparator();替换.查找.全选 view.add(item = new JMenuItem(字体.颜色. about.add(item = new JMenuItem(关于.2.菜单栏界面模块A文件菜单模块保存界面截图图6图7保存界面代码if (command.equals() if (!wasSaved) if (currentFile = null)
7、JFileChooser jfc = new JFileChooser(); jfc.showSaveDialog(this); File f = jfc.getSelectedFile(); if (f != null) savetoFile(f); currentFile = f; else savetoFile(currentFile); return; public void savetoFile(File f) FileWriter fw = null; try fw = new FileWriter(f); fw.write(jta.getText(); fw.flush(); w
8、asSaved = true; catch (IOException e) e.printStackTrace(); finally if (fw != null) try fw.close(); catch (IOException e) 打开已保存文件界面截图图8图9打开已保存文件代码 if (askSave() = 0) return; if (askSave() = 1) jta.setText( JFileChooser jfc = new JFileChooser(); jfc.showSaveDialog(this); File f = jfc.getSelectedFile()
9、; if (f ! currentFile = f; openFile(f);public void openFile(File f) FileReader fr = null; BufferedReader br = null; fr = new FileReader(f); br = new BufferedReader(fr); String str = null; while (str = br.readLine() ! jta.append(str + n catch (Exception e) try Thread.sleep(200); catch (InterruptedExc
10、eption e1) wasSaved = false; if (br ! br.close(); catch (Exception e) if (fr ! fr.close();文件另存为界面截图图10图11 文件另存为代码 if (command.equals( savetoFile(f);退出界面截图图12图13退出界面代码 else System.exit(0);public int askSave() if (!wasSaved) / 如果没有保存 int choice = JOptionPane.showConfirmDialog(this, 是否保存文件?, 保存文件?, JOp
11、tionPane.YES_NO_CANCEL_OPTION); switch (choice) case JOptionPane.OK_OPTION: / 要求保存文件 if (currentFile = null) / 如果是新建文件 else / 如果是打开的旧文件 return 1; case JOptionPane.NO_OPTION:/ 不保存文件 case JOptionPane.CANCEL_OPTION: return 0;/ 取消 return -1;B.编辑菜单模块复制模块截图图14复制模块代码 jta.copy(); pop.setVisible(false);粘贴模块截
12、图图15图16粘贴模块代码 jta.paste();剪切界面截图图17图18剪切界面代码 jta.cut();全选界面截图图19图20全选界面代码 jta.selectAll();C.查看菜单模块颜色菜单模块截图图21图22图23图24颜色菜单模块代码 Color c = JColorChooser.showDialog(this, 请选择文字颜色, Color.BLACK); jta.setForeground(c);五、实现和源程序import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import j
13、ava.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWrit
14、er;import java.io.IOException;import javax.swing.AbstractButton;import javax.swing.JColorChooser;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.text.JTextComponent; edit.add(item = n
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2