package Assis; import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import javax.swing.*; public class Note extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; JTextArea textArea = new JTextArea(); JMenuBar menuBar = new JMenuBar(); /** *文件菜单,新建,保存,另存为,退出选项** */ JMenu fileMenu = new JMenu("文件(F)"); JMenuItem newItem = new JMenuItem("新建(N)"); JMenuItem openItem = new JMenuItem("打开(O)..."); JMenuItem saveItem = new JMenuItem("保存(S)"); JMenuItem saveAsItem = new JMenuItem("另存为(A)..."); JMenuItem exitItem = new JMenuItem("退出(X)"); /* 编辑下拉菜单,以及全选,复制,剪切,粘贴等选项 */ JMenu editMenu = new JMenu("编辑(E)"); JMenuItem selectItem = new JMenuItem("全选(A)"); JMenuItem copyItem = new JMenuItem("复制(C)"); JMenuItem cutItem = new JMenuItem("剪切(T)"); JMenuItem pasteItem = new JMenuItem("粘贴(P)"); /* 格式下拉菜单,字体设置等选项 */ JMenu formatMenu = new JMenu("格式(O)"); JMenuItem changeRowItem = new JMenuItem("自动换行(W)..."); JMenuItem fontItem = new JMenuItem("字体(F)"); /* 帮助下拉菜单 */ String fileName = "未命名"; // 设置默认的文件名 Toolkit toolKit = Toolkit.getDefaultToolkit(); Clipboard clipBoard = toolKit.getSystemClipboard(); // 剪切板对象 获取系统 Clipboard // 的一个实例,该 Clipboard // 与本机平台提供的剪贴板工具相互作用。 /* 打开文件对话框和保存文件对话框 */ public FileDialog openFileDialog = new FileDialog(this, "Open File", FileDialog.LOAD); private FileDialog saveAsFileDialog = new FileDialog(this, "Sava File As", FileDialog.SAVE); public Note() { this.setTitle("无标题 - 记事本"); this.setFont(new Font("Times New Roman", Font.PLAIN, 12)); this.setBackground(Color.white);// 设置窗口背景颜色 this.setSize(700, 700); // 设置窗口大小 this.setJMenuBar(menuBar); // 添加menuBar this.add(textArea); // 添加textArea /* 在窗口中添加文件其下拉菜单 */ this.menuBar.add(fileMenu); // 添加"文件"菜单 this.fileMenu.add(newItem); // 添加"新建" this.newItem.setMnemonic('N'); // "新建"快捷键F this.fileMenu.addSeparator(); // 将新分隔符追加到菜单的末尾 this.fileMenu.add(openItem); // 添加"打开" this.openItem.setMnemonic('O'); // "打开"快捷键O this.fileMenu.addSeparator(); // 将新分隔符追加到菜单的末尾 this.fileMenu.add(saveItem); // 添加"保存" this.saveItem.setMnemonic('S'); // "保存"快捷键S this.fileMenu.add(saveAsItem); // 添加"另存为" this.saveAsItem.setMnemonic('A'); // "另存为"快捷键A this.fileMenu.addSeparator(); // 将新分隔符追加到菜单的末尾 this.fileMenu.add(exitItem); // 添加"退出" this.exitItem.setMnemonic('X'); this.newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); this.openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); this.saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); /* 在窗口中添加编辑其下拉菜单 */ this.menuBar.add(editMenu); // 添加"编辑"菜单 this.editMenu.setMnemonic('E'); // "编辑"快捷键 this.editMenu.add(copyItem); // 添加"复制" this.copyItem.setMnemonic('C'); // "复制"快捷键C this.editMenu.add(cutItem); // 添加"剪切" this.cutItem.setMnemonic('T'); // "剪切"快捷键T this.editMenu.add(pasteItem); // 添加"粘贴" this.pasteItem.setMnemonic('P'); // "粘贴"快捷键P this.copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); this.cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); this.pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); this.menuBar.add(formatMenu);// 添加"格式" this.formatMenu.add(changeRowItem); // 添加"自动换行" this.formatMenu.add(fontItem); // 添加"字体" /* 注册各个菜单项的事件监听器 */ this.newItem.addActionListener(this); this.openItem.addActionListener(this); this.saveItem.addActionListener(this); this.saveAsItem.addActionListener(this); this.exitItem.addActionListener(this); this.selectItem.addActionListener(this); this.copyItem.addActionListener(this); this.cutItem.addActionListener(this); this.pasteItem.addActionListener(this); this.fontItem.addActionListener(this); /* 指定窗口的退出方式 */ this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { /* 利用事件源判断被点击的菜单项 */ Object eventSource = e.getSource(); /* 以下为新建文件 */ if (eventSource == newItem) { textArea.setText(""); this.setTitle("无标题 - 记事本"); } /* 以下为打开文件 */ else if (eventSource == openItem) { File theOpenFile; JFileChooser noteOpenChooser = new JFileChooser(); int num = noteOpenChooser.showOpenDialog(this); if (num == JFileChooser.APPROVE_OPTION) { try { theOpenFile = noteOpenChooser.getSelectedFile(); this.setTitle(theOpenFile.getName()); FileReader noteOpenReader = new FileReader(theOpenFile); BufferedReader noteOpenBufferd = new BufferedReader( noteOpenReader); String noteString; while ((noteString = noteOpenBufferd.readLine()) != null) { textArea.setText(noteString); } noteOpenReader.close(); noteOpenBufferd.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } } /* 以下为保存文件 */ else if (eventSource == saveItem) { File newFile = null; JFileChooser noteSaveChooser = new JFileChooser(); // 文件资源管理器组件JFileChooser int num2 = noteSaveChooser.showSaveDialog(this); if (num2 == JFileChooser.APPROVE_OPTION) { newFile = noteSaveChooser.getSelectedFile(); this.setTitle(newFile.getName()); try { FileWriter fw = new FileWriter(newFile); BufferedWriter bw = new BufferedWriter(fw); bw.write(textArea.getText()); bw.close(); fw.close(); } catch (IOException e2) { e2.printStackTrace(); } } writeFile(fileName); } /* 以下为另存文件 */ else if (eventSource == saveAsItem) { this.saveAsFileDialog.setVisible(true); // 显示保存文件的对话框 fileName = saveAsFileDialog.getDirectory() + saveAsFileDialog.getFile(); if (fileName != null) writeFile(fileName); } /* 以下为保存全部 */ else if (eventSource == selectItem) { textArea.selectAll(); // 全选 } /* 以下为复制文本 */ else if (eventSource == copyItem) { String text = textArea.getSelectedText(); // 复制 StringSelection selection = new StringSelection(text); clipBoard.setContents(selection, null); } /* 剪切文本 */ else if (eventSource == cutItem) { String text = textArea.getSelectedText(); StringSelection selection = new StringSelection(text); clipBoard.setContents(selection, null); // 剪切时用空串代替被选字符。 textArea.replaceRange("", textArea.getSelectionStart(), textArea .getSelectionEnd()); } /* 粘贴文本 */ else if (eventSource == pasteItem) { Transferable contents = clipBoard.getContents(this); if (contents == null) return; String text; text = ""; try { text = (String) contents .getTransferData(DataFlavor.stringFlavor); } catch (Exception exception) { } textArea.replaceRange(text, textArea.getSelectionStart(), textArea .getSelectionEnd()); } else if (eventSource == exitItem) { System.exit(0); } } /* 读文件方法 */ public void readFile(String fileName) { try { File file = new File(fileName); FileReader readIn = new FileReader(file); int size = (int) file.length(); int charsRead = 0; char[] content = new char[size]; while (readIn.ready()) charsRead += readIn.read(content, charsRead, size - charsRead); readIn.close(); textArea.setText(new String(content, 0, charsRead)); } catch (IOException e) { System.out.println("Error opening File"); } } /* 写文件方法 */ public void writeFile(String fileName) { try { File file = new File(fileName); FileWriter writeOut = new FileWriter(file); writeOut.write(textArea.getText()); writeOut.close(); } catch (IOException e) { System.out.println("Error writing file"); } } public static void main(String[] args) { Note frame1 = new Note(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 让窗口居中显示 Dimension frame1Size = frame1.getSize(); if (frame1Size.height > screenSize.height) { frame1Size.height = screenSize.height; } if (frame1Size.width > screenSize.width) { frame1Size.width = screenSize.width; } frame1.setLocation((screenSize.width - frame1Size.width) / 2, (screenSize.height - frame1Size.height) / 2); frame1.setVisible(true); } }