diff --git a/src/NoteLayout.java b/src/NoteLayout.java new file mode 100644 index 0000000000000000000000000000000000000000..225199124f56f5dce2b8de25dca77d568b9204e7 --- /dev/null +++ b/src/NoteLayout.java @@ -0,0 +1,806 @@ +import java.awt.*; +import java.awt.event.*; +import java.io.*; +import java.sql.Date; +import java.util.Calendar; +import java.util.Calendar.*; + +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.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.filechooser.FileFilter; +import javax.swing.undo.CannotUndoException; +import javax.swing.undo.UndoManager; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +public class NoteLayout extends JFrame implements ActionListener,DocumentListener{ + String oldValue;//oldValue是area里面原来的文本内容,用来判断area内容是否改动,是否需要保存 + boolean isNewFile=true;//是否新文件(未保存过的) + File currentFile;//当前文件名 + + JLabel Label;//剪切板 + + UndoManager undo = new UndoManager();//撤销 + UndoableEditListener undoHandler = new UndoHandler(); + //其他变量 + + JTextArea area=new JTextArea(); + + Toolkit toolkit=Toolkit.getDefaultToolkit(); + Clipboard clipBoard=toolkit.getSystemClipboard(); + + JMenuBar menuBar=new JMenuBar(); + JMenu file=new JMenu("文 件(F)"), + edit=new JMenu("编 辑(E)"), + format=new JMenu("格 式(O)"), + view=new JMenu("查 看(V)"), + help=new JMenu("帮 助(H)"); + /*分别是文件,编辑,格式,查看,帮助的菜单栏 */ + JMenuItem n=new JMenuItem("新建(N) "), + o=new JMenuItem("打开(O) "), + s=new JMenuItem("保存(S) "), + a=new JMenuItem("另存为(A) "), + x=new JMenuItem("退出(X) "); + + /*分别是新建,打开,保存,另存为,退出*/ + + JMenuItem e1=new JMenuItem("撤销(U) "), + e2=new JMenuItem("剪切(T) "), + e3=new JMenuItem("复制(C) "), + e4=new JMenuItem("粘贴(P) "), + e5=new JMenuItem("删除(L) "), + e6=new JMenuItem("全选(A) "), + e7=new JMenuItem("时间/日期(D) "); + + JCheckBoxMenuItem o1=new JCheckBoxMenuItem("自行换行(W) "); + //为了实现自动换行可以勾选 + JMenuItem o2=new JMenuItem("字体(F) "); + +// JMenu sf=new JMenu("缩放(Z)"); +// JMenuItem v1=new JMenuItem("放大(I) "), +// v2=new JMenuItem("缩小(O) "), +// v3=new JMenuItem("恢复默认缩小 "); + JCheckBoxMenuItem v4=new JCheckBoxMenuItem("状态栏(S) ");//同自动换行 + + JMenuItem h1=new JMenuItem("查看帮助(H) "), + h2=new JMenuItem("关于记事本(A) "); + + NoteLayout(){ + init(); + setVisible(true); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + } + + public void init() { + setLayout(new BorderLayout()); + setFileLayout(); + seteditLayout(); + setformatLayout(); + setviewLayout(); + sethelpLayout(); + menuBar.add(file); + menuBar.add(edit); + menuBar.add(format); + menuBar.add(view); + menuBar.add(help); + JScrollPane main=new JScrollPane(area); + area.setLineWrap(true);//自动换行 + oldValue=area.getText();//获取area的内容 + add(menuBar,BorderLayout.NORTH); + add(main,BorderLayout.CENTER); + //建立监视 + setActionEvent(); + + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + exitWindowChoose(); + } + }); + area.requestFocus(); + checkMenuItemEnabled(); //设置菜单项的功能:剪切,复制,粘帖,删除功能 + } + + //file的布局 + void setFileLayout() { + n.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); + o.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); + s.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); + x.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); + file.add(n); + file.add(o); + file.add(s); + file.add(a); + file.add(x); + } + //edit布局 + void seteditLayout() { + //快捷键 + file.setMnemonic('F'); + e1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); + e2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); + e3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); + e4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); + e5.setAccelerator(KeyStroke.getKeyStroke('D')); + e6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); + //布局 + edit.add(e1); + edit.add(e2); + edit.add(e3); + edit.add(e4); + edit.add(e5); + edit.add(e6); + edit.add(e7); + } + //format布局 + void setformatLayout(){ + format.add(o1); + format.add(o2); + } + //view布局 + void setviewLayout() { +// view.add(sf); + view.add(v4); +// sf.add(v1); +// sf.add(v2); +// sf.add(v3); + Label=new JLabel(" 血狼记事本状态栏"); + this.add(Label,BorderLayout.SOUTH); + } + //help布局 + void sethelpLayout() { + help.add(h1); + help.add(h2); + } + + //监视 + public void setActionEvent() { + //file + n.addActionListener(this); + o.addActionListener(this); + s.addActionListener(this); + a.addActionListener(this); + x.addActionListener(this); + + //edit + e1.addActionListener(this); + //编辑区注册事件监听(与撤销操作有关) + area.getDocument().addUndoableEditListener(undoHandler); + area.getDocument().addDocumentListener(this); + + e2.addActionListener(this); + e3.addActionListener(this); + e4.addActionListener(this); + e5.addActionListener(this); + e6.addActionListener(this); + e7.addActionListener(this); + + //format + o1.addActionListener(this); + o2.addActionListener(this); + + //view +// v1.addActionListener(this); +// v2.addActionListener(this); +// v3.addActionListener(this); + v4.addActionListener(this); + + //help + h1.addActionListener(this); + h2.addActionListener(this); + + //area + area.getDocument().addDocumentListener(this); + } + + + //关闭窗口时调用 + public void exitWindowChoose() { + area.requestFocus(); + String currentValue=area.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(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(area.getText(),0,area.getText().length()); + bfw.flush(); + fw.close(); + + isNewFile=false; + currentFile=saveFileName; + oldValue=area.getText(); + + this.setTitle(saveFileName.getName()+" - 记事本"); + } + catch(IOException ioException){} + } + } + else { + try{ + FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(area.getText(),0,area.getText().length()); + bfw.flush(); + fw.close(); + //isSave=true; + } + catch(IOException ioException){} + } + System.exit(0); + } + else if(exitChoose==JOptionPane.NO_OPTION){ + System.exit(0); + } + else{ + return; + } + } + }//关闭窗口时调用方法结束 + + //字体功能 + public void setFont(){ + //默认字体 + Font font = new Font("Dialog", Font.PLAIN, 12); + java.util.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); + } + } + final JDialog fontDialog=new JDialog(this,"字体设置",false); + Container con=fontDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel fontLabel=new JLabel("字体(F):"); + fontLabel.setPreferredSize(new Dimension(100,20)); + JLabel styleLabel=new JLabel("字形(Y):"); + styleLabel.setPreferredSize(new Dimension(100,20)); + JLabel sizeLabel=new JLabel("大小(S):"); + sizeLabel.setPreferredSize(new Dimension(100,20)); + final JLabel sample=new JLabel("血狼记事本"); + final JTextField textField=new JTextField(10); + textField.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=area.getFont(); + textField.setText(currentFont.getFontName()); + textField.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(90); + fontList.setFixedCellHeight(20); + fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + final String fontStyle[]={"常规","粗体","斜体","粗斜体"}; + styleList=new JList(fontStyle); + styleList.setFixedCellWidth(90); + 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(45); + sizeList.setFixedCellHeight(20); + sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + fontList.addListSelectionListener(new ListSelectionListener(){ + public void valueChanged(ListSelectionEvent event){ + textField.setText(fontName[fontList.getSelectedIndex()]); + textField.selectAll(); + Font sampleFont1=new Font(textField.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(textField.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.selectAll(); + Font sampleFont3=new Font(textField.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont3); + } + }); + okButton.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + Font okFont=new Font(textField.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + area.setFont(okFont); + fontDialog.dispose(); + } + }); + JPanel samplePanel=new JPanel(); + samplePanel.setBorder(BorderFactory.createTitledBorder("示例")); + samplePanel.add(sample); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + panel2.add(textField); + panel2.add(styleText); + panel2.add(sizeText); + panel2.add(okButton); + panel3.add(new JScrollPane(fontList)); + 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 setN() { + area.requestFocus(); + String currentValue=area.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选择文件 + 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(area.getText(),0,area.getText().length()); + bfw.flush();//刷新该流的缓冲 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=area.getText(); + this.setTitle(saveFileName.getName()+" - 血狼记事本"); + } + catch (IOException ioException) {} + } + } + if(saveChoose==JOptionPane.NO_OPTION) { + area.replaceRange("",0,area.getText().length()); + this.setTitle("无标题 - 血狼记事本"); + isNewFile=true; + undo.discardAllEdits(); + e1.setEnabled(false); + oldValue=area.getText(); + } + else if(saveChoose==JOptionPane.CANCEL_OPTION) { + return; + } + } + else{ + area.replaceRange("",0,area.getText().length()); + this.setTitle("无标题 - 记事本"); + isNewFile=true; + undo.discardAllEdits(); + e1.setEnabled(false); + oldValue=area.getText(); + } + }//新建结束 + + //打开o + public void setO() { + String currentValue=area.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(area.getText(),0,area.getText().length()); + bfw.flush();//刷新该流的缓冲 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=area.getText(); + this.setTitle(saveFileName.getName()+" - 血狼记事本"); + } + 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("")){ + JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE); + } + else{ + try { + FileReader fr=new FileReader(fileName); + BufferedReader bfr=new BufferedReader(fr); + area.setText(""); + while((str=bfr.readLine())!=null){ + area.append(str); + } + this.setTitle(fileName.getName()+" - 血狼记事本"); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=area.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); + area.setText(""); + while((str=bfr.readLine())!=null) { + area.append(str); + } + this.setTitle(fileName.getName()+" - 血狼记事本"); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=a.getText(); + } + catch (IOException ioException) {} + } + } + }//打开结束 + + //保存s + public void setS() { + if(isNewFile) { + 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(area.getText(),0,area.getText().length()); + bfw.flush();//刷新该流的缓冲 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=area.getText(); + this.setTitle(saveFileName.getName()+" - 血狼记事本"); + } + catch (IOException ioException) {} + } + } + else{ + try{ + FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(area.getText(),0,area.getText().length()); + bfw.flush(); + fw.close(); + } + catch(IOException ioException) {} + } + }//保存结束 + + //另存为 + public void setA() { + 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(area.getText(),0,area.getText().length()); + bfw.flush(); + fw.close(); + oldValue=area.getText(); + this.setTitle(saveFileName.getName()+" - 血狼记事本"); + } + catch(IOException ioException) {} + } + }//另存为结束 + + //退出x + public void setX() { + int exitChoose=JOptionPane.showConfirmDialog(this,"确定要退出吗?","退出提示",JOptionPane.OK_CANCEL_OPTION); + if(exitChoose==JOptionPane.OK_OPTION) { + System.exit(0); + } + else { + return; + } + }//退出结束 + + //撤销 + public void setE1() { + if(undo.canUndo()) { + try { + undo.undo(); + } + catch (CannotUndoException ex) { + System.out.println("Unable to undo:" + ex); + } + } + + }//撤销结束 + + //剪切 + public void setE2() { + String text=area.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + area.replaceRange("",area.getSelectionStart(),area.getSelectionEnd()); + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除功能的可用性 + }//剪切结束 + + //复制 + public void setE3() { + String text=area.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除功能的可用性 + }//复制结束 + + //粘贴 + public void setE4() { + Transferable contents=clipBoard.getContents(this); + if(contents==null) + return; + String text=""; + try { + text=(String)contents.getTransferData(DataFlavor.stringFlavor); + } + catch (Exception exception) {} + area.replaceRange(text,area.getSelectionStart(),area.getSelectionEnd()); + checkMenuItemEnabled(); + }//粘贴结束 + + //删除 + public void setE5() { + area.replaceRange("",area.getSelectionStart(),area.getSelectionEnd()); + checkMenuItemEnabled(); + }//删除结束 + + //全选 + public void setE6() { + area.selectAll(); + }//全选结束 + + //时间/日期 + public void setE7() { + Calendar calendar=Calendar.getInstance(); + //calendar.setTime(new Date()); + int year=calendar.get(Calendar.YEAR), + month=calendar.get(Calendar.MONTH), + day=calendar.get(Calendar.DAY_OF_MONTH), + hour=calendar.get(Calendar.HOUR_OF_DAY), + min=calendar.get(Calendar.MINUTE), + second=calendar.get(Calendar.SECOND); + area.append("血狼记事本提醒您,现在是北京时间:"+year+"-"+month+"-"+day+" "+hour+":"+min+":"+second); + } + + //设置菜单项的可用性:剪切,复制,粘帖,删除功能 + public void checkMenuItemEnabled() { + String selectText=area.getSelectedText(); + e2.setEnabled(true); + e3.setEnabled(true); + e4.setEnabled(true); + e5.setEnabled(true); + //粘帖功能可用性判断 + Transferable contents=clipBoard.getContents(this); + if(contents==null) { + e4.setEnabled(false); + } + else { + e4.setEnabled(true); + } + }//checkMenuItemEnabled()结束 + + //自动换行 + public void setO1() { + if(o1.getState()) + area.setLineWrap(true); + else + area.setLineWrap(false); + } + + //状态栏 + public void setV4() { + try { + int position = area.getCaretPosition();//返回当前位置 + int line = area.getLineOfOffset(position);//当前行 + int column = position - area.getLineStartOffset(line);//当前列 + line += 1; + column+=1; + String str="血狼记事本-"+" 第" + line + "行, 第" + column+"列 "; + Label.setText(str); + } + catch(Exception ex) {} + if(v4.getState()) + Label.setVisible(true); + else + Label.setVisible(false); + } + + //查看帮助 + public void setH1() { + String str="血狼笔记本实现功能:\n" + +"文件:新建,打开,保存,另存为,退出\n" + +"编辑:撤销,剪切,复制,粘贴,删除,全选,时间/日期\n" + + "格式:自动换行,字体\n" + +"查看:状态栏\n" + +"帮助:查看帮助,关于记事本"; + JOptionPane.showMessageDialog(this,str,"血狼记事本-查看帮助",JOptionPane.INFORMATION_MESSAGE); + } + + //关于记事本 + public void setH2() { + String str="****************************\n" + +"name:血狼笔记本\n" + + "author:李上俊\n" + + "Writing time:2021-6\n" + + "speciality:IMIS\n" + + "class:2\n" + +"****************************"; + JOptionPane.showMessageDialog(this,str,"血狼记事本-关于记事本",JOptionPane.INFORMATION_MESSAGE); + } + + public void actionPerformed(ActionEvent e) { + area.requestFocus(); + if(e.getSource()==n) + setN();//新建功能 + else if(e.getSource()==o2) + setFont();//字体 + else if(e.getSource()==o) + setO();//打开 + else if(e.getSource()==s) + setS();//保存 + else if(e.getSource()==a) + setA();//另保存 + else if(e.getSource()==x) + setX();//退出 + else if(e.getSource()==e1) + setE1();//撤销 + else if(e.getSource()==e2) + setE2();//剪切 + else if(e.getSource()==e3) + setE3();//复制 + else if(e.getSource()==e4) + setE4();//粘贴 + else if(e.getSource()==e5) + setE5();//删除 + else if(e.getSource()==e6) + setE6();//全选 + else if(e.getSource()==e7) + setE7();//时间/日期 + else if(e.getSource()==o1) + setO1();//自动换行 + else if(e.getSource()==v4) + setV4(); + else if(e.getSource()==h1) + setH1(); + else if(e.getSource()==h2) + setH2(); + } + //实现DocumentListener接口中的方法(与撤销操作有关) + public void removeUpdate(DocumentEvent e) { + e1.setEnabled(true); + } + public void insertUpdate(DocumentEvent e) { + e1.setEnabled(true); + } + + public void changedUpdate(DocumentEvent e) { + e1.setEnabled(true); + } + //实现接口UndoableEditListener的类UndoHandler(与撤销操作有关) + class UndoHandler implements UndoableEditListener{ + public void undoableEditHappened(UndoableEditEvent e) { + undo.addEdit(e.getEdit()); + } + } +} + diff --git a/src/NoteMain.java b/src/NoteMain.java new file mode 100644 index 0000000000000000000000000000000000000000..6baef9f0298a92a78d3a51639d710ce53eea5a98 --- /dev/null +++ b/src/NoteMain.java @@ -0,0 +1,9 @@ +public class NoteMain { + public static void main(String[] args) { + // TODO Auto-generated method stub + NoteLayout note=new NoteLayout(); + note.setTitle("血狼记事本"); + note.setBounds(100,100,800,550); + } + +}