JAVA文本编辑器.docx
JAVA文本编辑器文本编辑器,实现功能: 1.颜色,字体的改变关联 2.复制,粘贴,剪切 功能 3.全选功能 4.时间戳功能 5.自动换行功能 6.帮助功能 7.文件读取,打开,保存,另存为功能等 功能,首先 我们看 主要程序! import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.undo.StateEdit; import java.util.*; import filter.FileExtensionFilter; public class EditorJFrame extends ActionListener,MouseListener,KeyListener,CaretListener private JComboBox combox_name,combox_size; private JCheckBox checkb_bold,checkb_italic; private JRadioButton radiob_color; private JTextArea text; private JButton findenter; private JTextField findtxt; private JPopupMenu popupmenu; private JLabel stateBar; private Color color; private JCheckBoxMenuItem jcbmi; private JRadioButtonMenuItem rbmi_color; private JMenuItem file; private JMenuItem editor; private JMenuItem style; private JMenuItem other; private File filename; JFrame implements private JFileChooser fchooser; private boolean iexc=true,jd=true,xy=true,xx = true; private String strline; private String substrline="" private JDialog jdialog; private int endf=0,startf=0; private JFrame jframe; private JTextField txt; public EditorJFrame super("文本编辑器"); Dimension dim=getToolkit.getScreenSize; this.setBounds(dim.width/4,dim.height/4, dim.width/2,dim.height/2); this.setDefaultCloseOperation(EXIT_ON_CLOSE); text=new JTextArea("Welcome 欢迎"); text.addMouseListener(this); text.addCaretListener(this); text.addKeyListener(this); text.setForeground(new Color(255,0,0); this.getContentPane.add(new JScrollPane(text); findenter=new JButton("查找下一个"); findenter.addActionListener(this); findtxt=new JTextField(7); findenter.addActionListener(this); findenter.setEnabled(false); findtxt.addCaretListener(this); txt=new JTextField("ABC"); txt.setEditable(false); JToolBar toolbar=new JToolBar; this.getContentPane.add(toolbar,"North"); GraphicsEnvironment ge =GraphicsEnvironment.getLocalGraphicsEnvironment; String fontsName=ge.getAvailableFontFamilyNames; combox_name= new JComboBox(fontsName); combox_name.addActionListener(this); toolbar.add(combox_name); String sizestr="20","30","40","50","60","70" combox_size=new JComboBox(sizestr); combox_size.setEditable(true); combox_size.addActionListener(this); toolbar.add(combox_size); this.filename=null; this.fchooser=new JFileChooser(new File(".",""); this.fchooser.setFileFilter(new FileExtensionFilter("文本文件(*.txt)","txt"); strline=text.getText; checkb_bold =new JCheckBox("粗体"); toolbar.add(checkb_bold); checkb_bold.addActionListener(this); checkb_italic=new JCheckBox("斜体"); toolbar.add(checkb_italic); checkb_italic.addActionListener(this); String colorstr="红","绿","蓝","其他" ButtonGroup bgroup_color=new ButtonGroup; radiob_color=new JRadioButtoncolorstr.length; for(int i=0;i<radiob_color.length;i+) radiob_colori=new JRadioButton(colorstri); radiob_colori.addActionListener(this); bgroup_color.add(radiob_colori); toolbar.add(radiob_colori); radiob_color0.setSelected(true); this.addmyMenu; this.setVisible(true); this.addWindowListener(new WindowAdapter public void windowClosing(WindowEvent e) closeFile; ); private void addmyMenu JMenuBar menubar = new JMenuBar; this.setJMenuBar(menubar); String menustr="文件","编辑","格式","帮助" JMenu menu=new JMenumenustr.length; /此处为弹 存 出对话保 for(int i=0;i<menustr.length;i+) menui=new JMenu(menustri); menubar.add(menui); String filestr="新建(N) ","打开(O) ","保存(S) ","另存为(A). ","退出(X) " file=new JMenuItemfilestr.length; for(int i=0;i<filestr.length;i+) if(i=4) menu0.addSeparator; filei=new JMenuItem(filestri); filei.addActionListener(this); menu0.add(filei); file0.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK); file1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK); file2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK); file3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK); file4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK); String editorstr="撤销(U) ","剪切(T) ","复制(C) ","粘贴(P) ","删除(L) ","查找(F) ","查找下一个(N) ","全选(A) ","时间/日期" editor=new JMenuItemeditorstr.length; for(int i=0;i<editorstr.length;i+) if(i=1|i=6|i=9) menu1.addSeparator; editori=new JMenuItem(editorstri); editori.setEnabled(false); editori.addActionListener(this); menu1.add(editori); editor3.setEnabled(true); editor0.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK); editor1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK); editor2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); editor3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK); editor4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK); editor5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK); editor6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,InputEvent.CTRL_MASK); editor7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK); editor8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,InputEvent.CTRL_MASK); JMenu menu_style= new JMenu("字形"); String str="粗体","斜体" jcbmi=new JCheckBoxMenuItemstr.length; jcbmi0=new JCheckBoxMenuItem("粗体"); menu_style.add(jcbmi0); jcbmi0.addActionListener(this); jcbmi1=new JCheckBoxMenuItem("斜体"); menu_style.add(jcbmi1); jcbmi1.addActionListener(this); menu1.add(menu_style); JMenu menu_color =new JMenu("颜色"); menu1.add(menu_color); ButtonGroup buttongroup=new ButtonGroup; String colorstr="红","绿","蓝","其他" rbmi_color=new JRadioButtonMenuItemcolorstr.length; for(int i=0;i<rbmi_color.length;i+) rbmi_colori=new JRadioButtonMenuItem(colorstri); buttongroup.add(rbmi_colori); rbmi_colori.addActionListener(this); menu_color.add(rbmi_colori); String stylestr="自动换行(W) " style=new JMenuItemstylestr.length; for(int i=0;i<stylestr.length;i+) stylei=new JMenuItem(stylestri); stylei.addActionListener(this); menu2.add(stylei); style0.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK); String otherstr="帮助(H)","关于记事本(A)" other=new JMenuItemotherstr.length; for(int i=0;i<otherstr.length;i+) otheri=new JMenuItem(otherstri); otheri.addActionListener(this); menu3.add(otheri); menu3.addSeparator; other0.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK); other1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK); popupmenu= new JPopupMenu; String menuitemstr="剪切","复制","粘贴" JMenuItem popmenuitem=new JMenuItemmenuitemstr.length; for(int i=0;i<popmenuitem.length;i+) popmenuitemi=new JMenuItem(menuitemstri); popupmenu.add(popmenuitemi); popmenuitemi.addActionListener(this); JScrollPane panel = new JScrollPane(text, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); Container contentPane = getContentPane; contentPane.add(panel, BorderLayout.CENTER); / 状态栏 stateBar = new JLabel("未修改"); stateBar.setHorizontalAlignment(SwingConstants.LEFT); stateBar.setBorder( BorderFactory.createEtchedBorder); contentPane.add(stateBar, BorderLayout.SOUTH); popmenuitem0.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK); popmenuitem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); popmenuitem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK); text.add(popupmenu); public void Search jdialog=new JDialog(this,"查找"); jdialog.add(new JLabel("输入要查找字符:"); jdialog.setLayout(new FlowLayout(FlowLayout.LEFT); jdialog.add(findtxt); jdialog.add(findenter); jdialog.setDefaultCloseOperation(HIDE_ON_CLOSE); jdialog.setBounds(this.getX+100,this.getY+150,300,100); jdialog.show; public void find startf=text.getText.indexOf(findtxt.getText,0+endf); endf=startf+findtxt.getText.length; text.select(startf,endf); if(startf<0) jdialog.show(false); JOptionPane.showMessageDialog(jdialog, "没有找到"); startf=0; public void actionPerformed(ActionEvent e) if(e.getSource instanceof JButton) if(e.getSource=findenter) if(text.getText.equals(" ") JOptionPane.showMessageDialog(jdialog, "没有找到"); return; find; if(e.getSource instanceof JRadioButton) if(e.getSource=radiob_color0) rbmi_color0.setSelected(true); color=new Color(255,0,0); if(e.getSource=radiob_color1) rbmi_color1.setSelected(true); color=new Color(0,255,0); if(e.getSource=radiob_color2) rbmi_color2.setSelected(true); color=new Color(0,0,255); if(e.getSource=radiob_color3) Color useSelectColor=JColorChooser.showDialog(this,"选Color.red); color = useSelectColor; ", 择颜色 text.setForeground(color); return ; if(e.getSource instanceof JCheckBoxMenuItem|e.getSource JRadioButtonMenuItem) int size=0; try String fontname=(String)combox_name.getSelectedItem; size=Integer.parseInt(String)combox_size.getSelectedItem); if(size<4|size>120) throw new Exception("SizeException"); java.awt.Font font=text.getFont; int style =font.getStyle; if(e.getSource=jcbmi0) if(jcbmi0.isSelected=true) checkb_bold.setSelected(true); else checkb_bold.setSelected(false); style=style1; if(e.getSource=jcbmi1) if(jcbmi1.isSelected=true) checkb_italic.setSelected(true); else checkb_italic.setSelected(false); style=style2; if(e.getSource=rbmi_color0) radiob_color0.setSelected(true); color=new Color(255,0,0); if(e.getSource=rbmi_color1) radiob_color1.setSelected(true); color=new Color(0,255,0); instanceof if(e.getSource=rbmi_color2) radiob_color2.setSelected(true); color=new Color(0,0,255); if(e.getSource=rbmi_color3) radiob_color3.setSelected(true); Color useSelectColor=JColorChooser.showDialog(this,"选择颜色", Color.red); color = useSelectColor; text.setForeground(color); text.setFont(new Font(fontname,style,size); catch(NumberFormatException nfe) JOptionPane.showMessageDialog(this,""" +(String)combox_size.getSelectedItem+""不能转化成整数新输入!"); catch(Exception ex) if(ex.getMessage="SizeException") JOptionPane.showMessageDialog(this,size+"字号不合入!"); finally if(e.getSource instanceof JMenuItem) if(e.getSource=file0) this.filename=null; this.setTitle("未命名"); this.text.setText(" "); if(e.getSource=file1) openFile; if(e.getSource=file2) ,请重,请重新输适 saveFile; if(e.getSource=file2&&file=null|e.getSource=file3) saveFileAs; if(e.getSource=file4) closeFile; if(e.getSource=editor0) if(iexc) substrline=text.getText; text.setText(strline); iexc=false; else text.setText(substrline); iexc=true; if(e.getActionCommand="剪切"|e.getSource=editor1) text.cut; if(e.getActionCommand="复制"|e.getSource=editor2) text.copy; if(e.getActionCommand="粘贴"|e.getSource=editor3) text.paste; if(e.getSource=editor4) String temp = this.text.getSelectedText; int start = this.text.getSelectionStart; int end = this.text.getSelectionEnd; this.text.replaceRange("",start,end); if(e.getSource=editor5) Search; if(e.getSource=editor6) find; if(e.getSource=editor7) text.selectAll; if(e.getSource=editor8) Calendar date = Calendar.getInstance; text.append(""+date.get(Calendar.HOUR)+":" +date.get(Calendar.MINUTE)+":" +date.get(Calendar.SECOND)+" " +date.get(Calendar.YEAR)+"-" +(date.get(Calendar.MONTH)+1)+"-" +date.get(Calendar.DATE); if(e.getSource=other0) JOptionPane.showOptionDialog(null, " n有困难,找警察,n"+ " n有疑问,找肖毅,n"+ " n还疑问,找老吴,n"+ " n再疑问,找API。 n", "关于EditorJFrame", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); if(e.getSource=other1) JOptionPane.showOptionDialog(null, "程序名称:n EditorJFrame n" + "程序设计:n 毅哥哥 n" + "简介:n 一个简单的文字编辑器n" + " 可以基本实现电脑自带记事本功能n" + " 功能可能有所不足,望指正nn" + " /", "关于EditorJFrame", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); if(e.getSource=style0) if(xx) text.setLineWrap(true); text.setWrapStyleWord(true); /换行不断 英文字符 style0.setForeground(new Color(169,169,169); /灰色 xx=false; else text.setLineWrap(false); text.setWrapStyleWord(false); style0.setForeground(new Color(0,0,0); xx=true; if(e.getSource instanceof JComboBox| e.getSource instanceof JCheckBox) int size=0; try String fontname=(String)combox_name.getSelectedItem; size=Integer.parseInt(String)combox_size.getSelectedItem); if(size<4|size>120) throw new Exception("SizeException"); java.awt.Font font=text.getFont; int style =font.getStyle; if(e.getSource=checkb_bold) if(checkb_bold.isSelected=true)