《JAVA文本编辑器》由会员分享,可在线阅读,更多相关《JAVA文本编辑器(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.setEnabled(false); editori.addActionListener(this); menu1.add(editori); editor3.setEnabled(true); editor0.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEven