diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000000000000000000000000000000000..de9b862f9e21c96b6dec038c8181586e5c7b967f --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//src/java2020spring/Notebook.java=UTF-8 diff --git a/src/java2020spring/Notebook.java b/src/java2020spring/Notebook.java new file mode 100644 index 0000000000000000000000000000000000000000..aa84d24f03e0ec231d8ef12c2db3386737ee37ea --- /dev/null +++ b/src/java2020spring/Notebook.java @@ -0,0 +1,1346 @@ +package java2020spring; + +import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.io.Serializable ; + +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.event.MenuEvent; +import javax.swing.event.MenuListener; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.undo.CannotUndoException; +import javax.swing.undo.UndoManager; +import java.io.*; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Enumeration; + +import javax.swing.*; +import java.awt.event.*; +import java.beans.EventHandler; + +public class Notebook extends JFrame implements DocumentListener,ActionListener { + private static Icon statusBar_status; + public static final Color doushalv = new Color(199,237,204);//新建颜色为护眼豆沙绿 + /*菜单*/ + private JMenu menu_file; //文件按钮 + private JMenu menu_edit; //编辑按钮 + private JMenu menu_format; //格式按钮 + private JMenu menu_help; //帮助按钮 + private JMenu menu_view; //查看按钮 + private JMenu menu_settime; //定时器按钮 + + /*右键弹出菜单*/ + private JPopupMenu popupMenu; + private JMenuItem popupmenu_undo; + private JMenuItem popupmenu_cut; + private JMenuItem popupmenu_copy; + private JMenuItem popupmenu_paste; + private JMenuItem popupmenu_delete; + private JMenuItem popupmenu_selectall; + + /*文件菜单*/ + private JMenuItem menuItem_new; + private JMenuItem menuItem_open; + private JMenuItem menuItem_save; + private JMenuItem menuItem_saveas; + private JMenuItem menuItem_exit; + + /*编辑菜单*/ + private JMenuItem menuItem_undo; + private JMenuItem menuItem_time; + private JMenuItem menuItem_find; + private JMenuItem menuItem_replace; + private JMenuItem menuItem_paste; + private JMenuItem menuItem_selectall; + private JMenuItem menuItem_cut; + private JMenuItem menuItem_copy; + private JMenuItem menuItem_delete; + + /*格式菜单*/ + private JCheckBoxMenuItem menuItem_linewrap; + private JMenuItem menuItem_font; + + /*查看的菜单*/ + private JCheckBoxMenuItem menuItem_status; + + /*帮助的菜单*/ + private JMenuItem menuItem_HelpTopics; + private JMenuItem menuItem_AboutNoteBook; + + /*定时器*/ + private JMenuItem settime; + + /*文本编辑区域*/ + private JTextArea textArea; + + /*状态栏*/ + private JLabel statusLabel; + /*菜单*/ + private JMenuBar menuBar; + private JLabel labelTime; + Toolkit toolkit=Toolkit.getDefaultToolkit(); + Clipboard clipBoard=toolkit.getSystemClipboard(); + + /*创建撤销操作管理器*/ + protected UndoManager undo=new UndoManager(); + protected UndoableEditListener undoHandler=new UndoHandler();//其他变量 + private String oldValue;//存放编辑区原来的内容,用于比较文本是否有改动 + boolean isNewFile=true;//是否新文件(未保存过的) + File currentFile; + private JLabel label; + + //当前文件名 + + public Notebook(){ + super("护眼记事本"); + Font font=new Font("Courier Prime", Font.PLAIN, 12); //改变系统默认字体 + Enumeration keys = UIManager.getDefaults().keys(); + while (keys.hasMoreElements()) { + Object key = keys.nextElement(); + Object value = UIManager.get(key); + if (value instanceof javax.swing.plaf.FontUIResource) { + UIManager.put(key,font); + } + } + + + JMenuBar menuBar=new JMenuBar(); //创建菜单条 + + /*创建文件菜单及菜单项并注册事件监听*/ + menu_file=new JMenu("文件"); + + menuItem_new=new JMenuItem("新建"); + menuItem_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); + menuItem_new.addActionListener(this); + + menuItem_open=new JMenuItem("打开"); + menuItem_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); + menuItem_open.addActionListener(this); + + menuItem_save=new JMenuItem("保存"); + menuItem_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); + menuItem_save.addActionListener(this); + + menuItem_saveas=new JMenuItem("另存为"); + menuItem_saveas.addActionListener(this); + + menuItem_exit=new JMenuItem("退出"); + menuItem_exit.addActionListener(this); + + /*创建编辑菜单及菜单项并注册事件监听*/ + menu_edit=new JMenu("编辑"); + menu_edit.setMnemonic('E');//设置快捷键ALT+E + + /*当选择编辑菜单时,设置剪切、复制、粘贴、删除等功能的可用性*/ + + menu_edit.addMenuListener(new MenuListener() + { public void menuCanceled(MenuEvent e)//取消菜单时调用 + { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + } + public void menuDeselected(MenuEvent e)//取消选择某个菜单时调用 + { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + } + public void menuSelected(MenuEvent e)//选择某个菜单时调用 + { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + } + }); + + menuItem_undo=new JMenuItem("撤销"); + menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); + menuItem_undo.addActionListener(this); + menuItem_undo.setEnabled(false); + + menuItem_cut=new JMenuItem("剪切"); + menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); + menuItem_cut.addActionListener(this); + + menuItem_copy=new JMenuItem("复制"); + menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); + menuItem_copy.addActionListener(this); + + menuItem_paste=new JMenuItem("粘贴"); + menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); + menuItem_paste.addActionListener(this); + + menuItem_delete=new JMenuItem("删除"); + menuItem_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0)); + menuItem_delete.addActionListener(this); + + menuItem_find=new JMenuItem("查找"); + menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); + menuItem_find.addActionListener(this); + + menuItem_replace = new JMenuItem("替换"); + menuItem_replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); + menuItem_replace.addActionListener(this); + + menuItem_selectall = new JMenuItem("全选"); + menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); + menuItem_selectall.addActionListener(this); + + menuItem_time = new JMenuItem("时间/日期"); + menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0)); + menuItem_time.addActionListener(this); + + /*创建格式菜单及菜单项并注册事件监听*/ + menu_format=new JMenu("格式"); + menu_format.setMnemonic('O');//设置快捷键ALT+O + + menuItem_linewrap=new JCheckBoxMenuItem("自动换行"); + menuItem_linewrap.setMnemonic('W');//设置快捷键ALT+W + menuItem_linewrap.setState(true); + menuItem_linewrap.addActionListener(this); + + menuItem_font=new JMenuItem("字体"); + menuItem_font.addActionListener(this); + + /*创建查看菜单及菜单项并注册事件监听*/ + menu_view=new JMenu("查看"); + menu_view.setMnemonic('V');//设置快捷键ALT+V + + menuItem_status=new JCheckBoxMenuItem("状态栏"); + menuItem_status.setMnemonic('S');//设置快捷键ALT+S + menuItem_status.setState(true); + menuItem_status.addActionListener(this); + + /*创建帮助菜单及菜单项并注册事件监听*/ + menu_help = new JMenu("帮助"); + menu_help.setMnemonic('H');//设置快捷键ALT+H + + menuItem_HelpTopics = new JMenuItem("帮助主题"); + menuItem_HelpTopics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); + menuItem_HelpTopics.addActionListener(this); + + menuItem_AboutNoteBook = new JMenuItem("关于记事本"); + menuItem_AboutNoteBook.addActionListener(this); + + /*定时器*/ + menu_settime=new JMenu ("定时器"); + settime=new JMenuItem("定时器"); + settime.addActionListener(this); + + /*向菜单条添加"文件"菜单及菜单项*/ + menuBar.add(menu_file); + menu_file.add(menuItem_new); + menu_file.add(menuItem_open); + menu_file.add(menuItem_save); + menu_file.add(menuItem_saveas); + menu_file.add(menuItem_exit); + + /*向菜单条添加"编辑"菜单及菜单项 */ + menuBar.add(menu_edit); + menu_edit.add(menuItem_undo); + menu_edit.add(menuItem_cut); + menu_edit.add(menuItem_copy); + menu_edit.add(menuItem_paste); + menu_edit.add(menuItem_delete); + menu_edit.add(menuItem_find); + menu_edit.add(menuItem_replace); + menu_edit.add(menuItem_selectall); + menu_edit.add(menuItem_time); + + /*向菜单条添加"格式"菜单及菜单项*/ + menuBar.add(menu_format); + menu_format.add(menuItem_linewrap); + menu_format.add(menuItem_font); + + /*向菜单条添加"查看"菜单及菜单项 */ + menuBar.add(menu_view); + menu_view.add(menuItem_status); + + /*向菜单条添加"帮助"菜单及菜单项*/ + menuBar.add(menu_help); + menu_help.add(menuItem_HelpTopics); + menu_help.addSeparator(); + menu_help.add(menuItem_AboutNoteBook); + + /*向菜单条添加“定时器”菜单*/ + menuBar.add(menu_settime); + menu_settime.add(settime); + + /*向窗口添加菜单条*/ + this.setJMenuBar(menuBar); + + /*创建文本编辑区并添加滚动条*/ + textArea=new JTextArea(20,50); + JScrollPane scroller=new JScrollPane(textArea); + scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + this.add(scroller,BorderLayout.CENTER);//向窗口添加文本编辑区 + textArea.setBackground(doushalv); + textArea.setWrapStyleWord(true);//设置单词在一行不足容纳时换行 + textArea.setLineWrap(true);//设置文本编辑区自动换行默认为true,即会"自动换行" + oldValue=textArea.getText();//获取原文本编辑区的内容 + + /*编辑区注册事件监听(与撤销操作有关)*/ + textArea.getDocument().addUndoableEditListener(undoHandler); + textArea.getDocument().addDocumentListener(this); + + + /*创建右键弹出菜单*/ + popupMenu=new JPopupMenu(); + popupmenu_undo=new JMenuItem("撤销"); + popupmenu_cut=new JMenuItem("剪切"); + popupmenu_copy=new JMenuItem("复制"); + popupmenu_paste=new JMenuItem("粘帖"); + popupmenu_delete=new JMenuItem("删除"); + popupmenu_selectall=new JMenuItem("全选"); + popupmenu_undo.setEnabled(false); + + /*向右键菜单添加菜单项和分隔符*/ + popupMenu.add(popupmenu_undo); + popupMenu.addSeparator(); + popupMenu.add(popupmenu_cut); + popupMenu.add(popupmenu_copy); + popupMenu.add(popupmenu_paste); + popupMenu.add(popupmenu_delete); + popupMenu.addSeparator(); + popupMenu.add(popupmenu_selectall); + + /*文本编辑区注册右键菜单事件*/ + popupmenu_undo.addActionListener(this); + popupmenu_cut.addActionListener(this); + popupmenu_copy.addActionListener(this); + popupmenu_paste.addActionListener(this); + popupmenu_delete.addActionListener(this); + popupmenu_selectall.addActionListener(this); + + /*文本编辑区注册右键菜单事件*/ + textArea.addMouseListener(new MouseAdapter() + { public void mousePressed(MouseEvent e) + { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + } + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 + textArea.requestFocus();//编辑区获取焦点 + } + public void mouseReleased(MouseEvent e) + { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + } + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 + textArea.requestFocus();//编辑区获取焦点 + } + }); + + /*创建和添加状态栏*/ + JToolBar statusLabel=new JToolBar(); + statusLabel.setFloatable(false); + SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd 今年的第DD天"); + labelTime = new JLabel("日期: "+sdf.format(new Date())); + statusLabel.addSeparator(new Dimension(180,5)); + statusLabel.add(labelTime); + statusLabel.setVisible(true); + + this.add(statusLabel,BorderLayout.SOUTH);//向窗口添加状态栏标签 + this.setLocation(100,100);//设置窗口在屏幕上的位置 + this.setSize(650,550);//设置窗口在屏幕上的大小 + this.setVisible(true);//设置窗口在屏幕上的可见性 + + /* + * textArea.getDocument().addDocumentListener(new DocumentListener() {// 监听文本区改变 + * public void insertUpdate(DocumentEvent e) { isItemsAvalible(); // + * 一旦文本有改变就设置各按钮的可用性 changeTextLengthStatus(); // 实时显示文本字数 } public void + * removeUpdate(DocumentEvent e) { isItemsAvalible(); // 一旦文本有改变就设置各按钮的可用性 + * changeTextLengthStatus(); // 实时显示文本字数 } public void + * changedUpdate(DocumentEvent e) { changeTextLengthStatus(); // 实时显示文本字数 + * isItemsAvalible(); // 一旦文本有改变就设置各按钮的可用性 } });} public void + * changeTextLengthStatus(){ // 文本监听 String content = textArea.getText().trim(); + * label.setText("当前字数:"+content.length()); } private void isItemsAvalible(){ // + * 监视文本区并设置各功能项是否可用 String content = textArea.getText(); if(content.equals("")){ + * menuItem_cut.setEnabled(false); menuItem_delete.setEnabled(false); + * menuItem_copy.setEnabled(false); menuItem_save.setEnabled(false); + * menuItem_saveas.setEnabled(false); popupmenu_copy.setEnabled(false); + * popupmenu_cut.setEnabled(false); }else{ menuItem_save.setEnabled(true); + * menuItem_cut.setEnabled(true); menuItem_delete.setEnabled(true); + * menuItem_copy.setEnabled(true); popupmenu_copy.setEnabled(true); + * popupmenu_cut.setEnabled(true); + * + * } + */ + + + + /*添加窗口监听器*/ + addWindowListener(new WindowAdapter() + { public void windowClosing(WindowEvent e) + { exitWindowChoose(); + } + }); + checkMenuItemEnabled(); + textArea.requestFocus();} + + + + + + + /*设置菜单项的可用性:剪切,复制,粘帖,删除功能*/ + public void checkMenuItemEnabled() + { String selectText=textArea.getSelectedText(); + if(selectText==null) + { menuItem_cut.setEnabled(false); + popupmenu_cut.setEnabled(false); + menuItem_copy.setEnabled(false); + popupmenu_copy.setEnabled(false); + menuItem_delete.setEnabled(false); + popupmenu_delete.setEnabled(false); + } + else + { menuItem_cut.setEnabled(true); + popupmenu_cut.setEnabled(true); + menuItem_copy.setEnabled(true); + popupmenu_copy.setEnabled(true); + menuItem_delete.setEnabled(true); + popupmenu_delete.setEnabled(true); + } + /*粘帖功能可用性判断*/ + Transferable contents=clipBoard.getContents(this); + if(contents==null) + { menuItem_paste.setEnabled(false); + popupmenu_paste.setEnabled(false); + } + else + { menuItem_paste.setEnabled(true); + popupmenu_paste.setEnabled(true); + } + } + + /*关闭窗口时调用*/ + public void exitWindowChoose() + { textArea.requestFocus(); + String currentValue=textArea.getText(); + if(currentValue.equals(oldValue)==true) + { System.exit(0); + } + else + { int exitChoose=JOptionPane.showConfirmDialog(this,"你的文件还没保存哦:)","退出!!",JOptionPane.YES_NO_CANCEL_OPTION); + if(exitChoose==JOptionPane.YES_OPTION) + { boolean isSave=false; + if(isNewFile) + { + String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setApproveButtonText("!!!确定!!!"); + fileChooser.setDialogTitle("另存为"); + + int result=fileChooser.showSaveDialog(this); + + File saveFileName=fileChooser.getSelectedFile(); + + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText(" 您没有保存文件"); + return; + } + + if(saveFileName==null||saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"不合法的文件名!","不合法的文件名!",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + + this.setTitle(saveFileName.getName()+" de记事本"); + statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile()); + isSave=true; + } + catch(IOException ioException){ + } + } + } + else + { + try + { FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + isSave=true; + } + catch(IOException ioException){ + } + } + System.exit(0); + if(isSave)System.exit(0); + else return; + } + else if(exitChoose==JOptionPane.NO_OPTION) + { System.exit(0); + } + else + { return; + } + } + } + + /*查找方法*/ + public void find() + { final JDialog findDialog=new JDialog(this,"查找",false);//false时允许其他窗口同时处于激活状态(即无模式) + Container con=findDialog.getContentPane();//返回此对话框的contentPane对象 + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel findContentLabel=new JLabel("查找内容:"); + final JTextField findText=new JTextField(15); + JButton findNextButton=new JButton("查找"); + final JCheckBox matchCheckBox=new JCheckBox("区分大小写"); + ButtonGroup bGroup=new ButtonGroup(); + final JRadioButton upButton=new JRadioButton("往上"); + final JRadioButton downButton=new JRadioButton("往下"); + downButton.setSelected(true); + bGroup.add(upButton); + bGroup.add(downButton); + JButton cancel=new JButton("取消");//取消按钮事件 + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { findDialog.dispose(); + } + }); + + /*"查找下一个"按钮监听*/ + findNextButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { //"区分大小写"的JCheckBox是否被选中 + int k=0,m=0; + final String str1,str2,str3,str4,strA,strB; + str1=textArea.getText(); + str2=findText.getText(); + str3=str1.toUpperCase(); + str4=str2.toUpperCase(); + if(matchCheckBox.isSelected())//区分大小写 + { strA=str1; + strB=str2; + } + else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找 + { strA=str3; + strB=str4; + } + if(upButton.isSelected()) + { + if(textArea.getSelectedText()==null) + k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); + else + k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { JOptionPane.showMessageDialog(null,"找不到您查找的内容!:(","查找",JOptionPane.INFORMATION_MESSAGE); + } + } + else if(downButton.isSelected()) + { if(textArea.getSelectedText()==null) + k=strA.indexOf(strB,textArea.getCaretPosition()+1); + else + k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { JOptionPane.showMessageDialog(null,"找不到您查找的内容!:(","查找",JOptionPane.INFORMATION_MESSAGE); + } + } + } + }); + + /*创建"查找"对话框的界面*/ + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + JPanel directionPanel=new JPanel(); + directionPanel.setBorder(BorderFactory.createTitledBorder("位置")); //设置directionPanel组件的边框; + directionPanel.add(upButton); + directionPanel.add(downButton); + panel1.setLayout(new GridLayout(2,1)); + panel1.add(findNextButton); + panel1.add(cancel); + panel2.add(findContentLabel); + panel2.add(findText); + panel2.add(panel1); + panel3.add(matchCheckBox); + panel3.add(directionPanel); + con.add(panel2); + con.add(panel3); + findDialog.setSize(410,180); + findDialog.setResizable(false);//不可调整大小 + findDialog.setLocation(230,280); + findDialog.setVisible(true); + } + + /*替换方法*/ + public void replace() + { final JDialog replaceDialog=new JDialog(this,"替换",false);//false时允许其他窗口同时处于激活状态(即无模式) + Container con=replaceDialog.getContentPane();//返回此对话框的contentPane对象 + con.setLayout(new FlowLayout(FlowLayout.CENTER)); + JLabel findContentLabel=new JLabel("查找内容:"); + final JTextField findText=new JTextField(15); + JButton findNextButton=new JButton("查找"); + JLabel replaceLabel=new JLabel("替换为:"); + final JTextField replaceText=new JTextField(15); + JButton replaceButton=new JButton("替换"); + JButton replaceAllButton=new JButton("全部替换"); + JButton cancel=new JButton("取消"); + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { replaceDialog.dispose(); + } + }); + final JCheckBox matchCheckBox=new JCheckBox("区分大小写"); + ButtonGroup bGroup=new ButtonGroup(); + final JRadioButton upButton=new JRadioButton("往上"); + final JRadioButton downButton=new JRadioButton("往下"); + downButton.setSelected(true); + bGroup.add(upButton); + bGroup.add(downButton); + + + + /*"替换"按钮监听*/ + replaceButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { if(replaceText.getText().length()==0 && textArea.getSelectedText()!=null) + textArea.replaceSelection(""); + if(replaceText.getText().length()>0 && textArea.getSelectedText()!=null) + textArea.replaceSelection(replaceText.getText()); + } + });//"替换"按钮监听结束 + + /*"全部替换"按钮监听*/ + replaceAllButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { textArea.setCaretPosition(0); //将光标放到编辑区开头 + int k=0,m=0,replaceCount=0; + if(findText.getText().length()==0) + { JOptionPane.showMessageDialog(replaceDialog,"请填写查找内容!","提示",JOptionPane.WARNING_MESSAGE); + findText.requestFocus(true); + return; + } + while(k>-1)//当文本中有内容被选中时(k>-1被选中)进行替换,否则不进行while循环 + { + final String str1,str2,str3,str4,strA,strB; + str1=textArea.getText(); + str2=findText.getText(); + str3=str1.toUpperCase(); + str4=str2.toUpperCase(); + if(matchCheckBox.isSelected())//区分大小写 + { strA=str1; + strB=str2; + } + else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找 + { strA=str3; + strB=str4; + } + if(upButton.isSelected()) + { + if(textArea.getSelectedText()==null) + k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); + else + k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { if(replaceCount==0) + { JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的内容!", "记事本",JOptionPane.INFORMATION_MESSAGE); + } + else + { JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个匹配内容!","替换成功(≧∇≦)",JOptionPane.INFORMATION_MESSAGE); + } + } + } + else if(downButton.isSelected()) + { if(textArea.getSelectedText()==null) + k=strA.indexOf(strB,textArea.getCaretPosition()+1); + else + k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { if(replaceCount==0) + { JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的内容!:(", "记事本",JOptionPane.INFORMATION_MESSAGE); + } + else + { JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个匹配内容!","替换成功(≧∇≦)",JOptionPane.INFORMATION_MESSAGE); + } + } + } + if(replaceText.getText().length()==0 && textArea.getSelectedText()!= null) + { textArea.replaceSelection(""); + replaceCount++; + } + + if(replaceText.getText().length()>0 && textArea.getSelectedText()!= null) + { textArea.replaceSelection(replaceText.getText()); + replaceCount++; + } + } + } + }); + + /*创建"替换"对话框的界面*/ + JPanel directionPanel=new JPanel(); + directionPanel.setBorder(BorderFactory.createTitledBorder("位置"));//设置directionPanel组件的边框; + directionPanel.add(upButton); + directionPanel.add(downButton); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + JPanel panel4=new JPanel(); + panel4.setLayout(new GridLayout(2,1)); + panel1.add(findContentLabel); + panel1.add(findText); + panel1.add(findNextButton); + panel4.add(replaceButton); + panel4.add(replaceAllButton); + panel2.add(replaceLabel); + panel2.add(replaceText); + panel2.add(panel4); + panel3.add(matchCheckBox); + panel3.add(directionPanel); + panel3.add(cancel); + con.add(panel1); + con.add(panel2); + con.add(panel3); + replaceDialog.setSize(420,220); + replaceDialog.setResizable(false);//不可调整大小 + replaceDialog.setLocation(230,280); + replaceDialog.setVisible(true); + } + + /*"字体"方法*/ + + public void font() + { final JDialog fontDialog=new JDialog(this,"字体设置",false); + Container con=fontDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel fontLabel=new JLabel("字体:"); + fontLabel.setPreferredSize(new Dimension(100,20));//构造一个Dimension,并将其初始化为指定宽度和高度 + JLabel styleLabel=new JLabel("字形:"); + styleLabel.setPreferredSize(new Dimension(100,20)); + JLabel sizeLabel=new JLabel("大小:"); + sizeLabel.setPreferredSize(new Dimension(100,20)); + final JLabel sample=new JLabel("是你想要的字体吗?(*^@^*)"); + sample.setHorizontalAlignment(SwingConstants.CENTER); + final JTextField fontText=new JTextField(9); + fontText.setPreferredSize(new Dimension(200,20)); + final JTextField styleText=new JTextField(8); + styleText.setPreferredSize(new Dimension(200,20)); + final int style[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC}; + final JTextField sizeText=new JTextField(5); + sizeText.setPreferredSize(new Dimension(200,20)); + JButton okButton=new JButton("确定"); + JButton cancel=new JButton("取消"); + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { fontDialog.dispose(); + } + }); + Font currentFont=textArea.getFont(); + fontText.setText(currentFont.getFontName()); + fontText.selectAll(); + if(currentFont.getStyle()==Font.PLAIN) + styleText.setText("常规"); + else if(currentFont.getStyle()==Font.BOLD) + styleText.setText("粗体"); + else if(currentFont.getStyle()==Font.ITALIC) + styleText.setText("斜体"); + else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) + styleText.setText("粗斜体"); + styleText.selectAll(); + String str=String.valueOf(currentFont.getSize()); + sizeText.setText(str); + sizeText.selectAll(); + final JList fontList,styleList,sizeList; + GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); + final String fontName[]=ge.getAvailableFontFamilyNames(); + fontList=new JList(fontName); + fontList.setFixedCellWidth(86); + fontList.setFixedCellHeight(20); + fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + final String fontStyle[]={"常规","粗体","斜体","粗斜体"}; + styleList=new JList(fontStyle); + styleList.setFixedCellWidth(86); + styleList.setFixedCellHeight(20); + styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + if(currentFont.getStyle()==Font.PLAIN) + styleList.setSelectedIndex(0); + else if(currentFont.getStyle()==Font.BOLD) + styleList.setSelectedIndex(1); + else if(currentFont.getStyle()==Font.ITALIC) + styleList.setSelectedIndex(2); + else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) + styleList.setSelectedIndex(3); + final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"}; + sizeList=new JList(fontSize); + sizeList.setFixedCellWidth(43); + sizeList.setFixedCellHeight(20); + sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + fontList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { fontText.setText(fontName[fontList.getSelectedIndex()]); + fontText.selectAll(); + Font sampleFont1=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont1); + } + }); + + styleList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { int s=style[styleList.getSelectedIndex()]; + styleText.setText(fontStyle[s]); + styleText.selectAll(); + Font sampleFont2=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont2); + } + }); + + sizeList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { sizeText.setText(fontSize[sizeList.getSelectedIndex()]); + sizeText.requestFocus(); + sizeText.selectAll(); + Font sampleFont3=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont3); + } + }); + okButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { Font okFont=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + textArea.setFont(okFont); + fontDialog.dispose(); + } + }); + JPanel samplePanel=new JPanel(); + samplePanel.setBorder(BorderFactory.createTitledBorder("示例")); + samplePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); + samplePanel.add(sample); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + panel2.add(fontText); + panel2.add(styleText); + panel2.add(sizeText); + panel2.add(okButton); + panel3.add(new JScrollPane(fontList));//JList不支持直接滚动,所以要让JList作为JScrollPane的视口视图 + panel3.add(new JScrollPane(styleList)); + panel3.add(new JScrollPane(sizeList)); + panel3.add(cancel); + con.add(panel1); + con.add(panel2); + con.add(panel3); + con.add(samplePanel); + fontDialog.setSize(350,340); + fontDialog.setLocation(200,200); + fontDialog.setResizable(false); + fontDialog.setVisible(true); + } + + + /*定时器方法*/ + public void Clock() + { + final JDialog replaceDialog=new JDialog(this,"定时器",false); + Container con=replaceDialog.getContentPane(); //返回此对话框的contentPane对象 + con.setLayout(new FlowLayout(FlowLayout.CENTER)); + JOptionPane.showMessageDialog(null, "启动定时器"); + System.out.println("启动定时器!"); + int setTime = setTime(); + if (-1 != setTime) { + System.out.println("开始计时!"); + long tmil = 60 * 1000 * setTime; + while (true) { + try { + Thread.sleep(tmil); + int confirmDialog = JOptionPane.showConfirmDialog(null, "叮叮叮~", "", JOptionPane.YES_NO_OPTION); + if (1 == confirmDialog) { + break; + } + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } + } + } + JOptionPane.showMessageDialog(null, "结束定时器"); + System.out.println("结束定时器"); + } + public int setTime() { + String time = JOptionPane.showInputDialog("输入间隔时间(分钟0-1440):"); + if (time == null) { + return -1; + } + int i = 5; + try {i = Integer.parseInt(time); + if (i < 1) { + throw new Exception("时间输入范围不正确!"); + } + if (i > 1440) { + i = 1440; + throw new Exception("时间输入范围不正确!"); + } + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(null, "时间输入错误!"); + i = setTime(); + } catch (Exception e) { + JOptionPane.showMessageDialog(null, e.getMessage()); + i = setTime(); + } + return i; + } + + + + public void actionPerformed(ActionEvent e) + { //新建 + if(e.getSource()==menuItem_new) + { textArea.requestFocus(); + String currentValue=textArea.getText(); + boolean isTextChange=(currentValue.equals(oldValue))?false:true; + if(isTextChange) + { int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); + if(saveChoose==JOptionPane.YES_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("另存为"); + int result=fileChooser.showSaveDialog(this); + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"不合法的文件名:(","不合法的文件名:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de 记事本"); + statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else if(saveChoose==JOptionPane.NO_OPTION) + { textArea.replaceRange("",0,textArea.getText().length()); + statusLabel.setText(" 新建文件"); + this.setTitle("无标题 de记事本"); + isNewFile=true; + undo.discardAllEdits(); //撤消所有的"撤消"操作 + menuItem_undo.setEnabled(false); + oldValue=textArea.getText(); + } + else if(saveChoose==JOptionPane.CANCEL_OPTION) + { return; + } + } + else + { textArea.replaceRange("",0,textArea.getText().length()); + statusLabel.setText(" 新建文件"); + this.setTitle("无标题 - 记事本"); + isNewFile=true; + undo.discardAllEdits();//撤消所有的"撤消"操作 + menuItem_undo.setEnabled(false); + oldValue=textArea.getText(); + } + } + + /*打开*/ + else if(e.getSource()==menuItem_open) + { textArea.requestFocus(); + String currentValue=textArea.getText(); + boolean isTextChange=(currentValue.equals(oldValue))?false:true; + if(isTextChange) + { int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存:(","提示",JOptionPane.YES_NO_CANCEL_OPTION); + if(saveChoose==JOptionPane.YES_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("另存为"); + int result=fileChooser.showSaveDialog(this); + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { + statusLabel.setText("您没有选择任何文件"); + JOptionPane.showMessageDialog(this,"不合法的文件名:(","不合法的文件名:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush();//刷新该流的缓冲 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de记事本"); + statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else if(saveChoose==JOptionPane.NO_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("打开文件"); + int result=fileChooser.showOpenDialog(this); + File fileName=fileChooser.getSelectedFile(); + if(fileName==null || fileName.getName().equals("")){ + statusLabel.setText("您没有选择任何文件"); + JOptionPane.showMessageDialog(this,"不合法的文件名:(","不合法的文件名:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileReader fr=new FileReader(fileName); + BufferedReader bfr=new BufferedReader(fr); + textArea.setText(""); + while((str=bfr.readLine())!=null) + { textArea.append(str); + } + this.setTitle(fileName.getName()+" de 记事本"); + statusLabel.setText(" 当前打开文件:"+fileName.getAbsoluteFile()); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=textArea.getText(); + } + catch (IOException ioException) + { + } + } + } + else + { return; + } + } + else + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("打开文件"); + int result=fileChooser.showOpenDialog(this); + File fileName=fileChooser.getSelectedFile(); + if(fileName==null || fileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"不合法的文件名:(","不合法的文件名:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileReader fr=new FileReader(fileName); + BufferedReader bfr=new BufferedReader(fr); + textArea.setText(""); + while((str=bfr.readLine())!=null) + { textArea.append(str); + } + this.setTitle(fileName.getName()+" de 记事本"); + statusLabel.setText(" 当前打开文件:"+fileName.getAbsoluteFile()); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=textArea.getText(); + } + catch (IOException ioException) + { + } + } + } + } + + /*保存*/ + else if(e.getSource()==menuItem_save) + { textArea.requestFocus(); + if(isNewFile) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + fileChooser.setDialogTitle("保存"); + int result=fileChooser.showSaveDialog(this); + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText("您没有选择任何文件"); + return; + } + + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"不合法的文件名:(","不合法的文件名:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush();//刷新该流的缓冲 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" - 记事本"); + statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else + { try + { FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + } + catch(IOException ioException) + { + } + } + } + + /*另存为*/ + else if(e.getSource()==menuItem_saveas) + { textArea.requestFocus(); + String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + fileChooser.setDialogTitle("另存为"); + int result=fileChooser.showSaveDialog(this); + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText(" 您没有选择任何文件"); + return; + } + + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null||saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"不合法的文件名:(","不合法的文件名:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de记事本"); + statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile()); + + } + catch(IOException ioException) + { + } + } + } + + /*退出*/ + else if(e.getSource()==menuItem_exit) + { int exitChoose=JOptionPane.showConfirmDialog(this,"你真的确定要退出吗?保存了吗","退出提示",JOptionPane.OK_CANCEL_OPTION); + if(exitChoose==JOptionPane.OK_OPTION) + { System.exit(0); + } + else + { return; + } + } + /*撤销*/ + else if(e.getSource()==menuItem_undo || e.getSource()==popupmenu_undo) + { textArea.requestFocus(); + if(undo.canUndo()) + { try + { undo.undo(); + } + catch (CannotUndoException ex) + { System.out.println("Unable to undo:" + ex); + + } + } + if(!undo.canUndo()) + { menuItem_undo.setEnabled(false); + } + } + + /*剪切*/ + else if(e.getSource()==menuItem_cut || e.getSource()==popupmenu_cut) + { textArea.requestFocus(); + String text=textArea.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除功能的可用性 + } + + /*复制*/ + else if(e.getSource()==menuItem_copy || e.getSource()==popupmenu_copy) + { textArea.requestFocus(); + String text=textArea.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除功能的可用性 + } + + /*粘帖*/ + else if(e.getSource()==menuItem_paste || e.getSource()==popupmenu_paste) + { textArea.requestFocus(); + Transferable contents=clipBoard.getContents(this); + if(contents==null)return; + String text=""; + try + { text=(String)contents.getTransferData(DataFlavor.stringFlavor); + } + catch (Exception exception) + { + } + textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled(); + } + + /*删除*/ + else if(e.getSource()==menuItem_delete || e.getSource()==popupmenu_delete) + { textArea.requestFocus(); + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled(); //设置剪切、复制、粘贴、删除等功能的可用性 + } + + /*查找*/ + else if(e.getSource()==menuItem_find) + { textArea.requestFocus(); + find(); + } + + /*替换*/ + else if(e.getSource()==menuItem_replace) + { textArea.requestFocus(); + replace(); + }//替换结束 + + /*时间日期*/ + else if(e.getSource()==menuItem_time) + { textArea.requestFocus(); +// SimpleDateFormat currentDateTime=new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss "); +// textArea.insert(currentDateTime.format(new Date()),textArea.getCaretPosition()); + Calendar rightNow=Calendar.getInstance(); + java.util.Date date=rightNow.getTime(); + textArea.insert(date.toString(),textArea.getCaretPosition()); + } + + /*全选*/ + else if(e.getSource()==menuItem_selectall || e.getSource()==popupmenu_selectall) + { textArea.selectAll(); + } + + /*自动换行*/ + else if(e.getSource()==menuItem_linewrap) + { if(menuItem_linewrap.getState()) + textArea.setLineWrap(true); + else + textArea.setLineWrap(false); + + } + + /*字体设置*/ + else if(e.getSource()==menuItem_font) + { textArea.requestFocus(); + font(); + } + + /*状态栏设置*/ + else if(e.getSource()==menuItem_status) + { if(menuItem_status.getState()) + statusLabel.setVisible(true); + else + statusLabel.setVisible(false); + } + + + + /*帮助*/ + else if(e.getSource()==menuItem_HelpTopics) + { textArea.requestFocus(); + JOptionPane.showMessageDialog(this,"优秀不是一种行为,而是一种习惯!","查看帮助",JOptionPane.INFORMATION_MESSAGE); + } + + /*关于*/ + else if(e.getSource()==menuItem_AboutNoteBook) + { textArea.requestFocus(); + JOptionPane.showMessageDialog(this, + "记事本中的定时器功能使用说明\n" + + "1.“确认”————开始计时器\n(确认间隔时间后,到间隔时间就会弹出窗口提醒,若按“是”则继续休息该间隔时间)\n2.“取消”————结束计时器\n" + +"计时器开启后,则不能对记事本进行编辑\n所以,请好好休息一下眼睛吧!", + "关于",JOptionPane.INFORMATION_MESSAGE); + } + + /*定时器*/ + else if(e.getSource()==settime) + { + Clock(); + } + } + + /*实现DocumentListener接口中的方法*/ + public void removeUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + public void insertUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + public void changedUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + + + /*实现接口UndoableEditListener的类UndoHandler*/ + class UndoHandler implements UndoableEditListener + { public void undoableEditHappened(UndoableEditEvent uee) + { undo.addEdit(uee.getEdit()); + } + } + + + + + } + + + + + + + diff --git a/src/java2020spring/Test.java b/src/java2020spring/Test.java deleted file mode 100644 index fc09c195f566d0e707f5e8a2f05fd168069c73a7..0000000000000000000000000000000000000000 --- a/src/java2020spring/Test.java +++ /dev/null @@ -1,10 +0,0 @@ -package java2020spring; - -public class Test { - - public static void main(String[] args) { - System.out.println("Hello world!"); - - } - -} diff --git a/src/java2020spring/window.java b/src/java2020spring/window.java new file mode 100644 index 0000000000000000000000000000000000000000..3c8e96e8ac83f892bc69a2b2b41d38a26b32a639 --- /dev/null +++ b/src/java2020spring/window.java @@ -0,0 +1,9 @@ +package java2020spring; + +import javax.swing.JFrame; +public class window { + public static void main(String[] args) { + Notebook notebook=new Notebook(); + notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//ʹ System exit ˳Ӧó + } +}