1个回答
展开全部
import java.awt.*; import java.awt.event.*; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.*; import javax.swing.event.*; @SuppressWarnings("serial") public class Notepad extends JFrame { JTextArea text = new JTextArea(); //字段摘要 JMenuBar menubar = new JMenuBar(); JMenu files = new JMenu("文件(F)"); JMenu edit = new JMenu("编辑(E)"); JMenu formats = new JMenu("格式(O)"); JMenu view = new JMenu("查看(V)"); JMenu helps = new JMenu("帮助(H)"); JMenuItem newFile = new JMenuItem("新建(N)"); JMenuItem open = new JMenuItem("打开(O)..."); JMenuItem save = new JMenuItem("保存(S)"); JMenuItem saveAs = new JMenuItem("另存为(A)..."); JMenuItem exit = new JMenuItem("退出(X)"); JMenuItem undo = new JMenuItem("撤销(U)"); JMenuItem cut = new JMenuItem("剪切(T)"); JMenuItem copy = new JMenuItem("复制(C)"); JMenuItem paste = new JMenuItem("粘贴(P)"); JMenuItem delete = new JMenuItem("删除(L)"); JMenuItem find = new JMenuItem("查找(F)..."); JMenuItem findNext = new JMenuItem("查找下一个(N)"); JMenuItem replace = new JMenuItem("替换(R)..."); JMenuItem selectAll = new JMenuItem("全选(A)"); JMenuItem timeDate = new JMenuItem("时间/日期(D)"); JCheckBoxMenuItem lineWrap = new JCheckBoxMenuItem("自动换行(M)"); JMenuItem fonts = new JMenuItem("字体(F)..."); JCheckBoxMenuItem state = new JCheckBoxMenuItem("状态栏(S)"); JMenuItem help = new JMenuItem("帮助主题(H)"); JMenuItem about = new JMenuItem("关于记事本(A)"); String name; //param name 保存标题名 String openedPath = null; //param openedPath 系统保存的打开路径 boolean isChangered = false; //param isChangered text是否被改动过,true表示改过,false表示未改动过 boolean isOpened = false; //param isOpened true表示打开的text,false表示新建的text String findStr = null; //param findStr 保存要查找的字符串 public Notepad(String name) { //构造函数 super(name); this.name = name; int x,y; Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); x = size.width; y = size.height; this.setSize(600, 500); this.setLocation((x-600)/2, (y-500)/2); windowAdapter winAda = new windowAdapter(); this.addWindowListener(winAda); menubar.add(files); //以下添加控键 menubar.add(edit); menubar.add(formats); menubar.add(view); menubar.add(helps); files.add(newFile); files.add(open); files.add(save); files.add(saveAs); files.addSeparator(); files.add(exit); edit.add(undo); edit.addSeparator(); edit.add(cut); edit.add(copy); edit.add(paste); edit.add(delete); edit.addSeparator(); edit.add(find); edit.add(findNext); edit.add(replace); edit.addSeparator(); edit.add(selectAll); edit.add(timeDate); formats.add(lineWrap); formats.add(fonts); view.add(state); helps.add(help); helps.addSeparator(); helps.add(about); this.setJMenuBar(menubar); this.add(new JScrollPane(text)); DocumentListener docLis = new documentListener(); text.getDocument().addDocumentListener(docLis); files.setMnemonic('F'); //以下为添加助记符 edit.setMnemonic('E'); formats.setMnemonic('O'); view.setMnemonic('V'); helps.setMnemonic('H'); newFile.setMnemonic('N'); open.setMnemonic('O'); save.setMnemonic('S'); saveAs.setMnemonic('A'); exit.setMnemonic('X'); undo.setMnemonic('U'); cut.setMnemonic('T'); copy.setMnemonic('C'); paste.setMnemonic('P'); delete.setMnemonic('L'); find.setMnemonic('F'); findNext.setMnemonic('N'); replace.setMnemonic('R'); selectAll.setMnemonic('A'); timeDate.setMnemonic('D'); lineWrap.setMnemonic('M'); fonts.setMnemonic('F'); state.setMnemonic('S'); help.setMnemonic('H'); about.setMnemonic('A'); //以下添加快捷键 newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK)); undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK)); selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); findNext.setAccelerator(KeyStroke.getKeyStroke("F3")); timeDate.setAccelerator(KeyStroke.getKeyStroke("F5")); actionListener listener = new actionListener(); //增加监听 newFile.addActionListener(listener); open.addActionListener(listener); save.addActionListener(listener); saveAs.addActionListener(listener); exit.addActionListener(listener); undo.addActionListener(listener); cut.addActionListener(listener); copy.addActionListener(listener); paste.addActionListener(listener); delete.addActionListener(listener); find.addActionListener(listener); findNext.addActionListener(listener); replace.addActionListener(listener); selectAll.addActionListener(listener); timeDate.addActionListener(listener); lineWrap.addActionListener(listener); fonts.addActionListener(listener); state.addActionListener(listener); help.addActionListener(listener); about.addActionListener(listener); fonts.setEnabled(false); state.setEnabled(false); } void save(boolean isChangered, boolean isOpened) { String savePath = new String(); if (isChangered && isOpened) { // 文件保存 if (JOptionPane.showConfirmDialog(Notepad.this, "需要保存文件吗?","保存提示", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { try { savePath = openedPath; FileOutputStream fos = new FileOutputStream(savePath); BufferedOutputStream bufferedOutput = new BufferedOutputStream(fos); bufferedOutput.write(text.getText().getBytes()); bufferedOutput.close(); fos.close(); this.isChangered = false; this.isOpened = true; } catch (Exception ex) { ex.printStackTrace(); } } } else if (isChangered && !isOpened) { //文件另存为 savePath = null; if (JOptionPane.showConfirmDialog(Notepad.this, "需要保存文件吗?","另存为提示", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { FileDialog saveFile = new FileDialog(Notepad.this,"保存文件...", FileDialog.SAVE); saveFile.setVisible(true); if (saveFile.getDirectory() != null&& saveFile.getFile() != null) { savePath = saveFile.getDirectory() + saveFile.getFile(); } if (savePath != null) { try { FileOutputStream fos = new FileOutputStream(savePath); BufferedOutputStream bufferedOutput = new BufferedOutputStream(fos); bufferedOutput.write(text.getText().getBytes()); bufferedOutput.close(); fos.close(); openedPath = savePath; this.isChangered = false; this.isOpened = true; } catch (Exception ex) { ex.printStackTrace(); } } } } } class windowAdapter extends WindowAdapter { //windows监听类 public void windowClosing(WindowEvent e) { save(isChangered,isOpened); System.exit(0); } } class documentListener implements DocumentListener { //文档监听接口,判断文档是否被改过 public void changedUpdate(DocumentEvent e) { isChangered = true; } public void insertUpdate(DocumentEvent e) { isChangered = true; } public void removeUpdate(DocumentEvent e) { isChangered = true; } }
采纳哦
采纳哦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询