java 文本編輯器
《java 文本編輯器》由會(huì)員分享,可在線閱讀,更多相關(guān)《java 文本編輯器(11頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、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.e
2、vent.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.JMe
3、nu; 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 J
4、Frame { /** @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
5、 JMenu("編輯"); JMenu option = new JMenu("選項(xiàng)"); JMenu about = new JMenu("關(guān)于"); JMenuItem newFile = new JMenuItem("新建"); JMenuItem open = new JMenuItem("打開(kāi)"); JMenuItem save = new JMenuItem("保存"); JMenuItem saveAs = new JMenuItem("另存為"); JMenuItem exit = n
6、ew 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("加粗");
7、 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 Str
8、ing(); String filePath = ""; int dot, mark; public MyFrame() { chooser.setSize(400, 350); chooser.setDialogTitle("學(xué)海無(wú)涯苦作舟"); chooser.setVisible(true); la.setSize(200, 20); la.setBackground(Color.orange); Font f = new Font
9、("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
10、(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); op
11、tion.add(italic); option.add(bold); about.add(version); about.add(help); newFile.addActionListener(new ListenActionForJfilechooser()); open.addActionListener(new ListenActionForJfilechooser()); save.addActionListener(new saveListener());
12、 saveAs.addActionListener(new ListenActionForJfilechooser()); exit.addActionListener(new exitListener()); copy.addActionListener(new copyListener()); cut.addActionListener(new cutListener()); paste.addActionListener(new pasteListener()); delete.addActionL
13、istener(new deleteListener()); italic.addActionListener(new italicListener()); bold.addActionListener(new boldListener()); version.addActionListener(new showversion()); ta.addCaretListener(new taListener()); ta.setFont(f); /* * 關(guān)閉事件
14、 */ this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowevent) { System.exit(0); } }); } class saveListener implements ActionListener { public void a
15、ctionPerformed(ActionEvent e) { if ("".equals(filePath)) { filePath = FileUtils.getFilePath(); File file = new File(filePath); if (!file.exists()) { try { file.createNewFile();
16、 } catch (IOException e1) { e1.printStackTrace(); } } } FileOutputStream fileOutStream = null; try { fileOutStream = new FileOutputStream(filePath);
17、} catch (FileNotFoundException fe) { la.setText("File Not Found"); return; } String content = ta.getText(); try { fileOutStream.write(content.getBytes()); } catch (IOException ioe) {
18、 la.setText("寫入文件錯(cuò)誤"); } finally { try { if (fileOutStream != null) fileOutStream.close(); } catch (IOException ioe2) { } } } } class boldListener imple
19、ments 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) {
20、 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(n
21、ull); ta.setFont(f); } } class cutListener implements ActionListener { public void actionPerformed(ActionEvent e) { String s1 = new String(); selected = selectedString(); s1 = ta.getText(); int length =
22、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 Act
23、ionListener { public void actionPerformed(ActionEvent e) { ta.paste(); } } class taListener implements CaretListener { public void caretUpdate(CaretEvent e) { dot = e.getDot(); // 取得光標(biāo)開(kāi)始位置 mark = e.getMark();// 取得光標(biāo)結(jié)束位置
24、 } } class copyListener implements ActionListener { public void actionPerformed(ActionEvent e) { selected = selectedString(); } } class exitListener implements ActionListener { public void actionPerformed(ActionEvent e) {
25、 System.exit(0); } } class ListenActionForJfilechooser implements ActionListener { int result; File file; public void actionPerformed(ActionEvent e) { if (e.getSource() == newFile) { filePath = FileUtils.getFilePath();
26、 file = new File(filePath); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e1) { e1.printStackTrace(); } }
27、 try { filestream = new FileInputStream(file); la.setText("打開(kāi)的文件是:" + file.getName()); } catch (Exception e1) { e1.printStackTrace(); } try { ta.read(fi
28、lestream, this); } catch (Exception e1) { e1.printStackTrace(); } } if (e.getSource() == open) { int state = chooser.showOpenDialog(null); file = chooser.getSelectedFile();
29、 if (file != null && state == JFileChooser.APPROVE_OPTION) { try { filestream = new FileInputStream(file); la.setText("打開(kāi)的文件是:" + file.getName()); filePath =file.getPath();
30、} catch (Exception e1) { e1.printStackTrace(); } try { ta.read(filestream, this); } catch (Exception e1) { e1.printStackTrace(); }
31、 } } if (e.getSource() == saveAs) { result = chooser.showSaveDialog(ta); file = null; if (result == JFileChooser.APPROVE_OPTION) { file = chooser.getSelectedFile(); la.set
32、Text("存儲(chǔ)文件名為:" + file.getName()); filePath =file.getPath(); } else if (result == JFileChooser.CANCEL_OPTION) { la.setText("您沒(méi)有選擇任何文件"); } FileOutputStream fileOutStream = null; if (file
33、!= null) { try { fileOutStream = new FileOutputStream(file); } catch (FileNotFoundException fe) { la.setText("File Not Found"); return; } S
34、tring content = ta.getText(); try { fileOutStream.write(content.getBytes()); } catch (IOException ioe) { la.setText("寫入文件錯(cuò)誤"); } finally { try {
35、 if (fileOutStream != null) fileOutStream.close(); } catch (IOException ioe2) { } } } } } } class showversion implements ActionLis
36、tener { public void actionPerformed(ActionEvent e) { exitversion.setVisible(true); } } class Myversion extends Frame { /** @Fields serialVersionUID: */ private static final long serialVersionUID = -3636792638101875168L; La
37、bel 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
38、.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 wind
39、owClosing(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(ma
40、rk, 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 LI
41、FENG * @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
42、= 1; while (true) { file = new File(path, "新建文本文檔(" + count + ").txt"); if (!file.exists()) { return "D:/新建文本文檔(" + count + ").txt"; } count ++; } } } package com.lf.frame; /** * @Description: 編輯器啟動(dòng) * @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); } }
- 溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 川渝旅游日記成都重慶城市介紹推薦景點(diǎn)美食推薦
- XX國(guó)有企業(yè)黨委書記個(gè)人述責(zé)述廉報(bào)告及2025年重點(diǎn)工作計(jì)劃
- 世界濕地日濕地的含義及價(jià)值
- 20XX年春節(jié)節(jié)后復(fù)工安全生產(chǎn)培訓(xùn)人到場(chǎng)心到崗
- 大唐女子圖鑒唐朝服飾之美器物之美繪畫之美生活之美
- 節(jié)后開(kāi)工第一課輕松掌握各要點(diǎn)節(jié)后常見(jiàn)的八大危險(xiǎn)
- 廈門城市旅游介紹廈門景點(diǎn)介紹廈門美食展示
- 節(jié)后開(kāi)工第一課復(fù)工復(fù)產(chǎn)十注意節(jié)后復(fù)工十檢查
- 傳統(tǒng)文化百善孝為先孝道培訓(xùn)
- 深圳城市旅游介紹景點(diǎn)推薦美食探索
- 節(jié)后復(fù)工安全生產(chǎn)培訓(xùn)勿忘安全本心人人講安全個(gè)個(gè)會(huì)應(yīng)急
- 預(yù)防性維修管理
- 常見(jiàn)閥門類型及特點(diǎn)
- 設(shè)備預(yù)防性維修
- 2.乳化液泵工理論考試試題含答案