您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)利用Java怎么實(shí)現(xiàn)一個(gè)文本編輯器,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
結(jié)構(gòu)分析:
界面布局 : EditFrame
main方法所在: EditText
打開功能實(shí)現(xiàn): FileReadThread
保存跟能實(shí)現(xiàn): save
一、 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; } }
上述就是小編為大家分享的利用Java怎么實(shí)現(xiàn)一個(gè)文本編輯器了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。