Java文本編輯器實(shí)現(xiàn)方法詳解
本文實(shí)例講述了Java文本編輯器實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
結(jié)構(gòu)分析:
- 界面布局 : EditFrame
- main方法所在: EditText
- 打開功能實(shí)現(xiàn): FileReadThread
- 保存跟能實(shí)現(xiàn): save
實(shí)際運(yùn)行效果:
附:完整代碼實(shí)現(xiàn)
一、 EditFrame
包括一個(gè)菜單Menu
底部:日期時(shí)間
代碼附上:
public class EditFrame extends JFrame { // TODO 自動(dòng)生成的構(gòu)造函數(shù)存根 boolean saveFlag = false; File saveFileRoot = null; JFrame jFrame; JPanel jPanelSouth; JMenuBar jMenuBar1; JMenu jMenu1; JMenuItem jMenuItem1; JMenuItem jMenuItem2; JMenuItem jMenuItem3; JMenuItem jMenuItem4; JSeparator jSeparator1; JTextArea jTextArea; JScrollPane scrollPane;// 滾動(dòng)條 public EditFrame() { // TODO 自動(dòng)生成的構(gòu)造函數(shù)存根 jFrame = new JFrame("水中魚之1999-文本編輯器"); jPanelSouth = new JPanel(); jMenuBar1 = new JMenuBar(); jMenu1 = new JMenu("文件"); jMenuItem1 = new JMenuItem("打開"); jMenuItem2 = new JMenuItem("保存"); jMenuItem3 = new JMenuItem("另存為"); jMenuItem4 = new JMenuItem("退出"); jSeparator1 = new JSeparator(); jTextArea = new JTextArea(); scrollPane = new JScrollPane(jTextArea); jFrame.setSize(800, 500); jFrame.setLocationRelativeTo(null); jFrame.setVisible(false); setLayout(); setSouthPanel(); // set relationship for your component setRelationShip(); // 設(shè)置 scrollPane for TextArea setScscrollPane(); iniClick(); } private void setRelationShip() { jFrame.add(BorderLayout.CENTER, scrollPane); jFrame.add(BorderLayout.SOUTH, jPanelSouth); jMenu1.add(jMenuItem1); jMenu1.add(jMenuItem2); jMenu1.add(jMenuItem3); jMenu1.add(jSeparator1); jMenu1.add(jMenuItem4); jMenuBar1.add(jMenu1); jFrame.setJMenuBar(jMenuBar1); } private void setLayout() { GridLayout gridLayout = new GridLayout(1, 2); jPanelSouth.setLayout(gridLayout); } private void setScscrollPane() { // jTextArea.setLineWrap(true);// 設(shè)置滿一行自動(dòng)換行 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); } private void setSouthPanel() { // add time for SouthPanel JLabel jLabelDate = new JLabel("Date"); JLabel jLabelTime = new JLabel("Time"); Timer timeAction = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); // 轉(zhuǎn)換日期顯示格式 SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd 日 "); jLabelDate.setText(" 當(dāng)前日期: " + date.format(new Date(timemillis))); SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss "); jLabelTime.setText(" 當(dāng)前時(shí)間: " + time.format(new Date(timemillis))); } }); jPanelSouth.add(jLabelDate); jPanelSouth.add(jLabelTime); timeAction.start(); } private void iniClick() { jFrame.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub int x = JOptionPane.showConfirmDialog(null, "確認(rèn)退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (x == 0) { System.exit(0); } } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } }); jMenuItem4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int x = JOptionPane.showConfirmDialog(null, "確認(rèn)退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (x == 0) { System.exit(0); } } }); jMenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub FileReadThread fileReadThread = new FileReadThread(EditFrame.this);// 開啟文件讀取線程 fileReadThread.start(); System.out.println(saveFileRoot); saveFlag = true; jTextArea.setText(""); } }); jMenuItem3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Save save = new Save(EditFrame.this); save.start(); saveFlag = true; } }); jMenuItem2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (!saveFlag) { Save save = new Save(EditFrame.this); save.start(); saveFlag = true; } else { new Save(EditFrame.this, saveFileRoot); } } }); } public JTextArea getjTextArea() { return jTextArea; } public void setjTextArea(JTextArea jTextArea) { this.jTextArea = jTextArea; } public File getSaveFileRoot() { return saveFileRoot; } public void setSaveFileRoot(File saveFileRoot) { this.saveFileRoot = saveFileRoot; } public JFrame getjFrame() { return jFrame; } public void setjFrame(JFrame jFrame) { this.jFrame = jFrame; } }
二、測(cè)試類 EditText
分兩部分:
1.閃屏 由于加載頁(yè)面需要時(shí)間 原想用 SplashScreen 由于打包成jar包才能用所以這里用jframe進(jìn)行替代
閃屏圖片直接粘貼到:
2.new 一個(gè)EditFrame 對(duì)象, 閃屏結(jié)束后設(shè)置為可見
public class EditText { public static void main(String[] args) { new Thread() { @Override public void run() { // TODO Auto-generated method stub EditFrame editFrame = new EditFrame(); JFrame jFrame = new JFrame(); JPanel jPanel = new javax.swing.JPanel(){ protected void paintComponent(java.awt.Graphics g){ super.paintComponent(g); g.drawImage(new ImageIcon("experiment_bac.jpg").getImage(),0,0,400,250,null); } }; jFrame.add(jPanel); jFrame.setVisible(true); jFrame.setSize(400, 300); jFrame.setLocationRelativeTo(null); try { sleep(1500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } jFrame.dispose(); editFrame.getjFrame().setVisible(true); } }.start(); } }
三、FileReadThread
調(diào)用swing自帶的 JFileChooser
選擇文件路徑
class FileReadThread extends Thread { private EditFrame test; public FileReadThread(EditFrame test ) { this.test = test; } @Override public void run() { JFileChooser chooser = new JFileChooser("d:/"); chooser.setFileFilter(new FileFilter() {// 定義文件過(guò)濾器,僅顯示文件夾和txt文本 @Override public String getDescription() { return null; } @Override public boolean accept(File file) { if (file.isDirectory() || file.getName().endsWith(".txt")) return true; return false; } }); int option = chooser.showOpenDialog(test); if (option == JFileChooser.APPROVE_OPTION) { File selFile = chooser.getSelectedFile(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(selFile), "gbk")); test.setSaveFileRoot(selFile); String line = null; while ((line = reader.readLine()) != null) { test.getjTextArea().append(line + "\n"); Thread.sleep(30);// 線程暫停,以看到讀取過(guò)程效果 } } catch (Exception e1) { e1.printStackTrace(); } JOptionPane.showMessageDialog(test, "讀取完畢"); } } }
四、save 保存
保存的調(diào)用分三種:
- 如果該文件是打開的 那么保存到打開文件中
- 如果該文件還未保存 這調(diào)用:進(jìn)行保存
- 如果該文件已經(jīng)另存為,則直接保存到另存為得的目錄下
public class Save extends Thread { private EditFrame area; private File saveFileRoot = null; public Save(EditFrame area, File saveFileRoot) { System.out.println(saveFileRoot + "123"); String text = area.getjTextArea().getText(); String[] lines = text.trim().split("\n"); try { PrintWriter out = new PrintWriter(new FileOutputStream(saveFileRoot), true); for (String line : lines) out.println(line); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Save(EditFrame area) { this.area = area; JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".txt"); } @Override public String getDescription() { return "SAVE TO"; } }); int r = chooser.showSaveDialog(area); if (r != JFileChooser.APPROVE_OPTION) return; File f = chooser.getSelectedFile(); area.setSaveFileRoot(f); String text = area.getjTextArea().getText(); String[] lines = text.trim().split("\n"); try { PrintWriter out = new PrintWriter(new FileOutputStream(f), true); for (String line : lines) out.println(line); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public File getSaveFileRoot() { return saveFileRoot; } public void setSaveFileRoot(File saveFileRoot) { this.saveFileRoot = saveFileRoot; } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
一文了解SpringBoot是如何連接數(shù)據(jù)庫(kù)的
Spring Boot提供了一系列的開箱即用的功能和特性,使得開發(fā)人員可以快速構(gòu)建和部署應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于SpringBoot是如何連接數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2023-06-06spring-cloud入門之eureka-client(服務(wù)注冊(cè))
本篇文章主要介紹了spring-cloud入門之eureka-client(服務(wù)注冊(cè)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Java Socket使用加密協(xié)議進(jìn)行傳輸對(duì)象的方法
這篇文章主要介紹了Java Socket使用加密協(xié)議進(jìn)行傳輸對(duì)象的方法,結(jié)合實(shí)例形式分析了java socket加密協(xié)議相關(guān)接口與類的調(diào)用方法,以及服務(wù)器、客戶端實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06springboot中使用@NotNull注解無(wú)效解決方法
這篇文章主要給大家介紹了關(guān)于springboot中使用@NotNull注解無(wú)效的解決方法,進(jìn)行參數(shù)校驗(yàn)的時(shí)候,加了@NotNull注解,@Validated注解和@Valid注解,但是參數(shù)校驗(yàn)的時(shí)候不生效,需要的朋友可以參考下2023-08-08Java的Jackson庫(kù)的使用及其樹模型的入門學(xué)習(xí)教程
這篇文章主要介紹了Java的Jackson庫(kù)的使用及其樹模型入門學(xué)習(xí)教程,Jackson庫(kù)通常被用來(lái)作Java對(duì)象和JSON的互相轉(zhuǎn)換,需要的朋友可以參考下2016-01-01java解析XML Node與Element的區(qū)別(推薦)
下面小編就為大家分享一篇java解析XML Node與Element的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Java 手動(dòng)解析不帶引號(hào)的JSON字符串的操作
這篇文章主要介紹了Java 手動(dòng)解析不帶引號(hào)的JSON字符串的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10