您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關利用Java怎么實現一個文本編輯器,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Java的特點有哪些 1.Java語言作為靜態面向對象編程語言的代表,實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向對象、分布式、安全性、平臺獨立與可移植性、動態性等特點。 3.使用Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
結構分析:
界面布局 : EditFrame
main方法所在: EditText
打開功能實現: FileReadThread
保存跟能實現: save
一、 EditFrame
包括一個菜單Menu
底部:日期時間
代碼附上:
public class EditFrame extends JFrame { // TODO 自動生成的構造函數存根 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;// 滾動條 public EditFrame() { // TODO 自動生成的構造函數存根 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(); // 設置 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);// 設置滿一行自動換行 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(); // 轉換日期顯示格式 SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd 日 "); jLabelDate.setText(" 當前日期: " + date.format(new Date(timemillis))); SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss "); jLabelTime.setText(" 當前時間: " + 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, "確認退出么?", "友情提示", 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, "確認退出么?", "友情提示", 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; } }
二、測試類 EditText
分兩部分:
1.閃屏 由于加載頁面需要時間 原想用 SplashScreen 由于打包成jar包才能用所以這里用jframe進行替代
閃屏圖片直接粘貼到:
2.new 一個EditFrame 對象, 閃屏結束后設置為可見
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
調用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() {// 定義文件過濾器,僅顯示文件夾和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);// 線程暫停,以看到讀取過程效果 } } catch (Exception e1) { e1.printStackTrace(); } JOptionPane.showMessageDialog(test, "讀取完畢"); } } }
四、save 保存
保存的調用分三種:
如果該文件是打開的 那么保存到打開文件中
如果該文件還未保存 這調用:進行保存
如果該文件已經另存為,則直接保存到另存為得的目錄下
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怎么實現一個文本編輯器了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。