java 文本編輯器
package com.lf.frame;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Frame;import java.awt.Label;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JTextPane;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;/* * Description: 文本編輯器 * modifyBy LIFENG * modifyDate 2014-6-24 上午09:55:58 */public class MyFrame extends JFrame /* Fields serialVersionUID: */ private static final long serialVersionUID = -2839804867272812883L; Container c = new Container(); Label la = new Label("書山有路勤為徑!"); JMenuBar jmb = new JMenuBar(); JMenu file = new JMenu("文件"); JMenu edit = new JMenu("編輯"); JMenu option = new JMenu("選項(xiàng)"); JMenu about = new JMenu("關(guān)于"); JMenuItem newFile = new JMenuItem("新建"); JMenuItem open = new JMenuItem("打開"); JMenuItem save = new JMenuItem("保存"); JMenuItem saveAs = new JMenuItem("另存為"); JMenuItem exit = new JMenuItem("退出"); JMenuItem copy = new JMenuItem("復(fù)制"); JMenuItem cut = new JMenuItem("剪切"); JMenuItem paste = new JMenuItem("粘貼"); JMenuItem delete = new JMenuItem("全部刪除"); JMenuItem italic = new JMenuItem("斜體"); JMenuItem bold = new JMenuItem("加粗"); JMenuItem version = new JMenuItem("版本"); JMenuItem help = new JMenuItem("幫助"); JTextPane ta = new JTextPane(); JFileChooser chooser = new JFileChooser(); FileInputStream filestream = null; Myversion exitversion = new Myversion(); String selected = new String(); String filePath = "" int dot, mark; public MyFrame() chooser.setSize(400, 350); chooser.setDialogTitle("學(xué)海無涯苦作舟"); chooser.setVisible(true); la.setSize(200, 20); la.setBackground(Color.orange); Font f = new Font("TimesRoman", Font.PLAIN, 16); c = this.getContentPane(); c.setLayout(new BorderLayout(); c.add(ta, "Center"); this.setJMenuBar(jmb); jmb.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0); jmb.add(file); jmb.add(edit); jmb.add(option); jmb.add(about); jmb.add(la); file.add(newFile); file.add(open); file.add(save); file.add(saveAs); file.add(exit); edit.add(copy); edit.add(cut); edit.add(paste); edit.add(delete); option.add(italic); option.add(bold); about.add(version); about.add(help); newFile.addActionListener(new ListenActionForJfilechooser(); open.addActionListener(new ListenActionForJfilechooser(); save.addActionListener(new saveListener(); saveAs.addActionListener(new ListenActionForJfilechooser(); exit.addActionListener(new exitListener(); copy.addActionListener(new copyListener(); cut.addActionListener(new cutListener(); paste.addActionListener(new pasteListener(); delete.addActionListener(new deleteListener(); italic.addActionListener(new italicListener(); bold.addActionListener(new boldListener(); version.addActionListener(new showversion(); ta.addCaretListener(new taListener(); ta.setFont(f); /* * 關(guān)閉事件 */ this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent windowevent) System.exit(0); ); class saveListener implements ActionListener public void actionPerformed(ActionEvent e) if ("".equals(filePath) filePath = FileUtils.getFilePath(); File file = new File(filePath); if (!file.exists() try file.createNewFile(); catch (IOException e1) e1.printStackTrace(); FileOutputStream fileOutStream = null; try fileOutStream = new FileOutputStream(filePath); catch (FileNotFoundException fe) la.setText("File Not Found"); return; String content = ta.getText(); try fileOutStream.write(content.getBytes(); catch (IOException ioe) la.setText("寫入文件錯(cuò)誤"); finally try if (fileOutStream != null) fileOutStream.close(); catch (IOException ioe2) class boldListener implements ActionListener public void actionPerformed(ActionEvent e) Font f = new Font("TimesRoman", Font.BOLD, 16); ta.setFont(f); class italicListener implements ActionListener public void actionPerformed(ActionEvent e) Font f = new Font("TimesRoman", Font.ITALIC, 16); ta.setFont(f); class deleteListener implements ActionListener public void actionPerformed(ActionEvent e) Font f = new Font("TimesRoman", Font.PLAIN, 16); ta.setText(null); ta.setFont(f); class cutListener implements ActionListener public void actionPerformed(ActionEvent e) String s1 = new String(); selected = selectedString(); s1 = ta.getText(); int length = s1.length(); if (dot < mark) ta.setText(s1.substring(0, dot) + s1.substring(mark, length); else ta.setText(s1.substring(0, mark) + s1.substring(dot, length); class pasteListener implements ActionListener public void actionPerformed(ActionEvent e) ta.paste(); class taListener implements CaretListener public void caretUpdate(CaretEvent e) dot = e.getDot(); / 取得光標(biāo)開始位置 mark = e.getMark();/ 取得光標(biāo)結(jié)束位置 class copyListener implements ActionListener public void actionPerformed(ActionEvent e) selected = selectedString(); class exitListener implements ActionListener public void actionPerformed(ActionEvent e) System.exit(0); class ListenActionForJfilechooser implements ActionListener int result; File file; public void actionPerformed(ActionEvent e) if (e.getSource() = newFile) filePath = FileUtils.getFilePath(); file = new File(filePath); if (!file.exists() try file.createNewFile(); catch (IOException e1) e1.printStackTrace(); try filestream = new FileInputStream(file); la.setText("打開的文件是:" + file.getName(); catch (Exception e1) e1.printStackTrace(); try ta.read(filestream, this); catch (Exception e1) e1.printStackTrace(); if (e.getSource() = open) int state = chooser.showOpenDialog(null); file = chooser.getSelectedFile(); if (file != null && state = JFileChooser.APPROVE_OPTION) try filestream = new FileInputStream(file); la.setText("打開的文件是:" + file.getName(); filePath =file.getPath(); catch (Exception e1) e1.printStackTrace(); try ta.read(filestream, this); catch (Exception e1) e1.printStackTrace(); if (e.getSource() = saveAs) result = chooser.showSaveDialog(ta); file = null; if (result = JFileChooser.APPROVE_OPTION) file = chooser.getSelectedFile(); la.setText("存儲文件名為:" + file.getName(); filePath =file.getPath(); else if (result = JFileChooser.CANCEL_OPTION) la.setText("您沒有選擇任何文件"); FileOutputStream fileOutStream = null; if (file != null) try fileOutStream = new FileOutputStream(file); catch (FileNotFoundException fe) la.setText("File Not Found"); return; String content = ta.getText(); try fileOutStream.write(content.getBytes(); catch (IOException ioe) la.setText("寫入文件錯(cuò)誤"); finally try if (fileOutStream != null) fileOutStream.close(); catch (IOException ioe2) class showversion implements ActionListener public void actionPerformed(ActionEvent e) exitversion.setVisible(true); class Myversion extends Frame /* Fields serialVersionUID: */ private static final long serialVersionUID = -3636792638101875168L; Label l1 = new Label("Author: Lifeng"); Label l2 = new Label("Version 1.0"); Label l3 = new Label("2014.06.24"); Label l4 = new Label("Tel:18688746700"); Myversion() this.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 20); this.add(l1); this.add(l2); this.add(l3); this.add(l4); this.setBounds(200, 180, 300, 200); this.addWindowListener(new myWindowListener(); class myWindowListener extends WindowAdapter public void windowClosing(WindowEvent e) exitversion.setVisible(false); String selectedString() String s = new String(); s = ta.getText(); if (dot < mark) s = s.substring(dot, mark); else s = s.substring(mark, dot); return s; package com.lf.frame;import java.io.File;/* * Description: 文件工具類 * modifyBy EX-LIFENG001 * modifyDate 2014-6-24 上午10:38:46 */public class FileUtils /* * Description: 獲得文件名 * author LIFENG * date 2014-6-24上午10:38:59 * return String */ public static String getFilePath() File path = new File("D:/"); File file = new File(path, "新建文本文檔.txt"); if (!file.exists() return "D:/新建文本文檔.txt" int count = 1; while (true) file = new File(path, "新建文本文檔(" + count + ").txt"); if (!file.exists() return "D:/新建文本文檔(" + count + ").txt" count +; package com.lf.frame;/* * Description: 編輯器啟動 * modifyBy LIFENG * modifyDate 2014-6-24 上午09:56:25 */public class EditorMain public static void main(String args) MyFrame frame1 = new MyFrame(); frame1.setSize(500, 400); frame1.setVisible(true);