diff --git a/src/Frame/Editor.java b/src/Frame/Editor.java new file mode 100644 index 0000000000000000000000000000000000000000..a77eb6d6a9b9b816b54f82155bbfe09d4f00d607 --- /dev/null +++ b/src/Frame/Editor.java @@ -0,0 +1,545 @@ +package Frame; + +import java.awt.BorderLayout; +import java.awt.Desktop; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringReader; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.MalformedInputException; + +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.KeyStroke; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.undo.UndoManager; + +public class Editor extends JFrame { + /** + * + */ + private static final long serialVersionUID = -712838941484955771L; + public JFrame mainFrame; + public JTextArea textArea; + public JMenuBar menuBar; + public JMenu menuFile, menuEdit, menuFormat, menuCheck, menuHelp; + public JPopupMenu popupMenu; + public JMenuItem newItem, openItem, saveItem, other_saveItem, exitItem, undoItem, pasteItem, copyItem, cutItem, + deleteItem, undoItem1, pasteItem1, copyItem1, cutItem1, deleteItem1, wordwrapItem, statusbarItem, + aboutNoteItem, helpItem; + public File file, fileCurrent; + public JScrollPane scroll; + public UndoManager undom = new UndoManager(); + public JPanel jPanel; + public JLabel jLabel0, jLabel1, jLabel2; + public int x, y, position, word; + + // 构造方法 + public Editor(int a, int b, int c, int d) { + mainFrame = new JFrame(); + jPanel = new JPanel();// 状态栏面板 + jLabel0 = new JLabel(); + jLabel1 = new JLabel(); + jLabel2 = new JLabel(); + // 创建文本区 + textArea = new JTextArea(); + x = 1; + y = 0; + word = 0; + textArea.getDocument().addDocumentListener(new DocumentListener() { + + @Override + public void removeUpdate(DocumentEvent e) { + word--; + jLabel0.setText("字数" + word); + } + + @Override + public void insertUpdate(DocumentEvent e) { + word++; + jLabel0.setText("字数" + word); + } + + @Override + public void changedUpdate(DocumentEvent e) { + word++; + jLabel0.setText("字数" + word); + } + }); + textArea.addCaretListener(new CaretListener() { + + @Override + public void caretUpdate(CaretEvent e) { + JTextArea area = (JTextArea) e.getSource(); + try { + int position = area.getCaretPosition();// 获取光标符的位置 + x = area.getLineOfOffset(position);// 将文本中的偏移量转化为行号 + y = position - area.getLineStartOffset(x); + x += 1; + jLabel1.setText("第" + x + "行"); + jLabel2.setText("第" + y + "列"); + } catch (Exception e1) { + e1.printStackTrace(); + } + + } + }); + jLabel1.setText("第" + x + "行"); + jLabel2.setText("第" + y + "列"); + jLabel0.setText("字数" + word); + jPanel.add(jLabel1); + jPanel.add(jLabel2); + jPanel.add(jLabel0); + scroll = new JScrollPane(textArea);// 设置滚动窗格 + createMenuBar();// 创建菜单条 + createMenuMouse();// 鼠标右键菜单 + // 设置窗口 + textArea.setLineWrap(true);// 自动换行 + mainFrame.add(jPanel, BorderLayout.SOUTH);// 添加状态栏 + mainFrame.add(scroll, BorderLayout.CENTER); + mainFrame.setBounds(a, b, c, d); + mainFrame.setVisible(true); + mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + // 撤销监视器 + textArea.getDocument().addUndoableEditListener(new UndoableEditListener() { + + @Override + public void undoableEditHappened(UndoableEditEvent e) { + undom.addEdit(e.getEdit()); + + } + }); + } + + // 创建菜单条 + public void createMenuBar() { + menuBar = new JMenuBar(); + // 添加各个菜单 + createMenuFile(); + menuBar.add(menuFile); + createMenuEdit(); + menuBar.add(menuEdit); + createMenuFormat(); + menuBar.add(menuFormat); + createMenuCheck(); + menuBar.add(menuCheck); + createMenuHelp(); + menuBar.add(menuHelp); + mainFrame.setJMenuBar(menuBar);// 向窗口添加菜单条 + } + + // 创建文件菜单 + public void createMenuFile() { + menuFile = new JMenu("文件"); + + // 新建功能 + newItem = new JMenuItem("新建(N)"); + newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); + newItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + Editor editor = new Editor(200, 200, 1000, 400); + } + }); + menuFile.add(newItem); + + // 打开功能 + openItem = new JMenuItem("打开(O)"); + openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); + openItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fileChooser = new JFileChooser(); + if (fileChooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { + File file = fileChooser.getSelectedFile(); + if (file != null) { + textArea.setText(" "); + try { + char[] buffer = new char[1024]; + FileReader fileReader = new FileReader(file); + for (;;) { + int count = fileReader.read(buffer);// 文件大小 + if (count != -1) { + textArea.append(new String(buffer, 0, count));// 读取文件内容 + } else { + break; + } + fileCurrent = file;// 持有打开文件的对象 + mainFrame.setTitle(file.getAbsolutePath());// 将文件的绝对路径名设为主窗口的标题 + } + fileReader.close(); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } catch (IOException e2) { + e2.printStackTrace(); + } + } else { + return; + } + } + + } + }); + menuFile.add(openItem); + + // 保存功能 + saveItem = new JMenuItem("保存(S)"); + saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); + saveItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (fileCurrent == null) { + JFileChooser fileChooser = new JFileChooser(); + if (fileChooser.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { + File file = fileChooser.getSelectedFile(); + if ((!file.exists()) || (file.exists() + && JOptionPane.showConfirmDialog(mainFrame, "override ?") == JOptionPane.YES_OPTION)) { + fileCurrent = file; + } else { + return; + } + } else { + return; + } + } + } + }); + menuFile.add(saveItem); + + // 另存为功能 + other_saveItem = new JMenuItem("另存为(A)"); + other_saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); + other_saveItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + { + if (fileCurrent == null) { + JFileChooser fileChooser = new JFileChooser(); + if (fileChooser.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { + File file = fileChooser.getSelectedFile(); + if ((!file.exists()) || (file.exists() && JOptionPane.showConfirmDialog(mainFrame, + "override ?") == JOptionPane.YES_OPTION)) { + fileCurrent = file; + } else { + return; + } + } else { + return; + } + } // end if + } + File file = fileCurrent; + if (file != null) { + char[] buffer = new char[1024]; + try { + StringReader stringReader = new StringReader(textArea.getText()); + FileWriter fileWriter = new FileWriter(file); + for (;;) { + int count = stringReader.read(buffer); + if (count != -1) { + fileWriter.append(new String(buffer, 0, count)); + } else { + break; + } + } + fileWriter.close(); + stringReader.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } else { + return; + } + } + + }); + menuFile.add(other_saveItem); + menuFile.addSeparator(); + + // 退出功能 + exitItem = new JMenuItem("退出(X)"); + exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); + exitItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + int n = JOptionPane.showConfirmDialog(null, "是否退出", "标题", JOptionPane.YES_NO_OPTION); + if (n == JOptionPane.YES_OPTION) { + System.exit(0); + } else { + } + } + }); + menuFile.add(exitItem); + } + + // 创建编辑菜单 + public void createMenuEdit() { + menuEdit = new JMenu("编辑"); + // 撤销功能 + undoItem = new JMenuItem("撤销(Z)"); + undoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); + undoItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == undoItem) { + undom.undo(); + } + } + }); + menuEdit.add(undoItem); + menuEdit.addSeparator(); + // 剪切功能 + cutItem = new JMenuItem("剪切(T)"); + cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK)); + cutItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == cutItem) + textArea.cut(); + } + }); + menuEdit.add(cutItem); + // 复制功能 + copyItem = new JMenuItem("复制(C)"); + copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); + copyItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == copyItem) + textArea.copy(); + } + }); + menuEdit.add(copyItem); + // 粘贴功能 + pasteItem = new JMenuItem("粘帖(V)"); + pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); + pasteItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == pasteItem) + textArea.paste(); + } + }); + menuEdit.add(pasteItem); + // 删除功能 + deleteItem = new JMenuItem("删除(D)"); + deleteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK)); + deleteItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == deleteItem) + textArea.setText(""); + } + }); + menuEdit.add(deleteItem); + } + + // 创建鼠标右键菜单 + public void createMenuMouse() { + popupMenu = new JPopupMenu(); + // 撤销功能 + undoItem1 = new JMenuItem("撤销"); + undoItem1.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == undoItem1) { + undom.undo(); + } + } + }); + popupMenu.add(undoItem1); + popupMenu.addSeparator(); + // 剪切功能 + cutItem1 = new JMenuItem("剪切"); + cutItem1.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == cutItem1) + textArea.cut(); + } + }); + popupMenu.add(cutItem1); + // 复制功能 + copyItem1 = new JMenuItem("复制"); + copyItem1.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == copyItem1) + textArea.copy(); + } + }); + popupMenu.add(copyItem1); + // 粘贴功能 + pasteItem1 = new JMenuItem("粘帖"); + pasteItem1.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == pasteItem1) + textArea.paste(); + } + }); + popupMenu.add(pasteItem1); + // 删除功能 + deleteItem1 = new JMenuItem("删除"); + deleteItem1.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == deleteItem1) + textArea.setText(""); + } + }); + popupMenu.add(deleteItem1); + textArea.add(popupMenu); + textArea.addMouseListener(new MouseListener() { + + @Override + public void mouseReleased(MouseEvent e) { + } + + @Override + public void mousePressed(MouseEvent e) { + if (e.getButton() == 3) { + popupMenu.show(e.getComponent(), e.getX(), e.getY()); + } + } + + @Override + public void mouseExited(MouseEvent e) { + } + + @Override + public void mouseEntered(MouseEvent e) { + } + + @Override + public void mouseClicked(MouseEvent e) { + } + }); + + } + + // 创建格式菜单 + public void createMenuFormat() { + menuFormat = new JMenu("格式"); + // 自动换行功能 + wordwrapItem = new JCheckBoxMenuItem("自动换行", true); + wordwrapItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if (wordwrapItem.isSelected()) { + textArea.setLineWrap(true); + } else { + textArea.setLineWrap(false); + } + } + }); + menuFormat.add(wordwrapItem); + } + + // 创建查看菜单 + public void createMenuCheck() { + menuCheck = new JMenu("查看"); + // 状态栏功能 + statusbarItem = new JCheckBoxMenuItem("状态栏", false); + menuCheck.add(statusbarItem); + statusbarItem.setSelected(true); + statusbarItem.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent arg0) { + if (statusbarItem.isSelected()) { + jPanel.setVisible(true); + } else { + jPanel.setVisible(false); + } + } + }); + } + + // 创建帮助菜单 + public void createMenuHelp() { + menuHelp = new JMenu("帮助"); + // 帮助功能 + helpItem = new JMenuItem("查看帮助"); + helpItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + Desktop desktop = Desktop.getDesktop(); + try { + URI uri = new URI( + "https://cn.bing.com/search?q=%E8%8E%B7%E5%8F%96%E6%9C%89%E5%85%B3+windows+10+%E4%B8%AD%E7%9A%84%E8%AE%B0%E4%BA%8B%E6%9C%AC%E7%9A%84%E5%B8%AE%E5%8A%A9&filters=guid:%224466414-zh-hans-dia%22%20lang:%22zh-hans%22&form=T00032&ocid=HelpPane-BingIA"); + desktop.browse(uri);// 打开浏览器的指定网站 + } catch (MalformedInputException e1) { + e1.printStackTrace(); + } catch (IOException e2) { + e2.printStackTrace(); + } catch (URISyntaxException e3) { + e3.printStackTrace(); + } + } + }); + + menuHelp.add(helpItem); + menuHelp.addSeparator(); + // 关于记事本 + aboutNoteItem = new JMenuItem("关于记事本"); + aboutNoteItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null, + "*********************************************\n" + " 编写者:许国栋 \n" + + " 编写时间:2021-05-24 \n" + "班级: 信管2班28号 \n" + + " 有些功能未能实现,不足之处希望大家能提出意见,谢谢! \n" + "*********************************************\n", + "记事本", JOptionPane.INFORMATION_MESSAGE); + } + }); + menuHelp.add(aboutNoteItem); + } + + // 主程序 + public static void main(String[] args) { + Editor editor = new Editor(200, 200, 1000, 400); + } + +} diff --git a/src/Right.java b/src/Right.java new file mode 100644 index 0000000000000000000000000000000000000000..59b121c8c2f5a4c893a4acc4cf7e93dd7f78b695 --- /dev/null +++ b/src/Right.java @@ -0,0 +1,233 @@ + +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +import javax.swing.JFrame; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.KeyStroke; +import javax.swing.UIManager; + +public class Right extends JFrame { + private static final long serialVersionUID = + + -5942087991012920147L; + + private JScrollPane pane = null; + + private TextAreaMenu text = null; + + public Right() { + super("右键菜单"); + + try { // 使用Windows的界面风格 + + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); + + } catch (Exception e) { + e.printStackTrace(); + + } + + text = new TextAreaMenu(); + + pane = new JScrollPane(text); + + this.getContentPane().add(pane); + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + this.setSize(300, 200); + + this.setVisible(true); + + } + + public static void main(String args[]) { + new Right(); + + } + + class TextAreaMenu extends JTextArea implements MouseListener + + { + private static final long serialVersionUID = + + -2308615404205560110L; + + private JPopupMenu pop = null; // 弹出菜单 + + private JMenuItem copy = null, paste = null, cut = null; // 三个功能菜单 + + public TextAreaMenu() { + super(); + init(); + } + + private void init() { + this.addMouseListener(this); + + pop = new JPopupMenu(); + + pop.add(copy = new JMenuItem("复制")); + + pop.add(paste = new JMenuItem("粘贴")); + + pop.add(cut = new JMenuItem("剪切")); + // 设置快捷键 + copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK)); + + paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK)); + + cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK)); + + copy.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + action(e); + + } + + }); + + paste.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + action(e); + + } + + }); + + cut.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + action(e); + } + + }); + + this.add(pop); + + } + + public void action(ActionEvent e) { + String str = e.getActionCommand(); + + if + + (str.equals(copy.getText())) { // 复制 + + this.copy(); + + } else if + + (str.equals(paste.getText())) { // 粘贴 + + this.paste(); + + } else if + + (str.equals(cut.getText())) { // 剪切 + + this.cut(); + + } + + } + + public JPopupMenu getPop() { + return pop; + + } + + public void setPop(JPopupMenu pop) { + this.pop = pop; + + } + + public boolean isClipboardString() { + boolean b = false; + + Clipboard clipboard = + + this.getToolkit().getSystemClipboard(); + + Transferable content = + + clipboard.getContents(this); + + try { + if + + (content.getTransferData(DataFlavor.stringFlavor) instanceof + + String) { + b = true; + + } + + } catch (Exception e) { + } + + return b; + + } + + public boolean isCanCopy() { + boolean b = false; + + int start = + + this.getSelectionStart(); + + int end = + + this.getSelectionEnd(); + + if (start != end) + + b = + + true; + + return b; + + } + + public void mouseClicked(MouseEvent e) { + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + } + + public void mousePressed(MouseEvent e) { + if (e.getButton() == + + MouseEvent.BUTTON3) { + copy.setEnabled(isCanCopy()); + + paste.setEnabled(isClipboardString()); + + cut.setEnabled(isCanCopy()); + + pop.show(this, e.getX(), e.getY()); + + } + + } + + public void mouseReleased(MouseEvent e) { + } + + } + +}