JAVA文本编辑器.docx

上传人:牧羊曲112 文档编号:3159587 上传时间:2023-03-11 格式:DOCX 页数:20 大小:42.61KB
返回 下载 相关 举报
JAVA文本编辑器.docx_第1页
第1页 / 共20页
JAVA文本编辑器.docx_第2页
第2页 / 共20页
JAVA文本编辑器.docx_第3页
第3页 / 共20页
JAVA文本编辑器.docx_第4页
第4页 / 共20页
JAVA文本编辑器.docx_第5页
第5页 / 共20页
亲,该文档总共20页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《JAVA文本编辑器.docx》由会员分享,可在线阅读,更多相关《JAVA文本编辑器.docx(20页珍藏版)》请在三一办公上搜索。

1、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.

2、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

3、; 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

4、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 EditorJF

5、rame 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.setFo

6、reground(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.setEdit

7、able(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

8、_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)

9、,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_c

10、olor=new JRadioButtoncolorstr.length; for(int i=0;iradiob_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.addWi

11、ndowListener(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;imenustr.length;i+) menui=new JMenu(menust

12、ri); menubar.add(menui); String filestr=新建(N) ,打开(O) ,保存(S) ,另存为(A). ,退出(X) ; file=new JMenuItemfilestr.length; for(int i=0;ifilestr.length;i+) if(i=4) menu0.addSeparator; filei=new JMenuItem(filestri); filei.addActionListener(this); menu0.add(filei); file0.setAccelerator(KeyStroke.getKeyStroke(KeyE

13、vent.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(Key

14、Stroke.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;ieditorstr.length;i+) if(i=1|i=6|i=9) menu1.addSeparator; editori=new JMenuItem(editorstri); editori.setEnable

15、d(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

16、(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.setAc

17、celerator(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 J

18、CheckBoxMenuItemstr.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 b

19、uttongroup=new ButtonGroup; String colorstr=红,绿,蓝,其他; rbmi_color=new JRadioButtonMenuItemcolorstr.length; for(int i=0;irbmi_color.length;i+) rbmi_colori=new JRadioButtonMenuItem(colorstri); buttongroup.add(rbmi_colori); rbmi_colori.addActionListener(this); menu_color.add(rbmi_colori); String stylest

20、r=自动换行(W) ; style=new JMenuItemstylestr.length; for(int i=0;istylestr.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 JMenuItemot

21、herstr.length; for(int i=0;iotherstr.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,InputEv

22、ent.CTRL_MASK); popupmenu= new JPopupMenu; String menuitemstr=剪切,复制,粘贴; JMenuItem popmenuitem=new JMenuItemmenuitemstr.length; for(int i=0;ipopmenuitem.length;i+) popmenuitemi=new JMenuItem(menuitemstri); popupmenu.add(popmenuitemi); popmenuitemi.addActionListener(this); JScrollPane panel = new JScr

23、ollPane(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.setBo

24、rder( 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(KeyStrok

25、e.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)

26、; 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(startf0) jdialog.show(false); JOptionPane.showMessageDialog(jdialog, 没有找到); startf=0; public

27、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(

28、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

29、; , 择颜色 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(size120) throw new Exception(SizeException); ja

30、va.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.setSelect

31、ed(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(

32、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)

33、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) openFi

34、le; 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=剪切

35、|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.

36、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(Cale

37、ndar.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) JO

38、ptionPane.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.setWrapStyleWor

39、d(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(size120) throw new Exception(SizeException); java.awt.Font font=text.getFont; int style =font.getStyle; if(e.getSource=checkb_bold) if(checkb_bold.isSelected=true)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号