【計理01組32號】Eclipse實現Java編輯器
阿新 • • 發佈:2022-03-03
部落格推行版本更新,成果積累制度,已經寫過的部落格還會再次更新,不斷地琢磨,高質量高數量都是要追求的,工匠精神是學習必不可少的精神。因此,大家有何建議歡迎在評論區踴躍發言,你們的支援是我最大的動力,你們敢投,我就敢肝
在黑夜裡夢想著光,心中覆蓋悲傷,在悲傷裡忍受孤獨,空守一絲溫暖。
我的淚水是無底深海,對你的愛已無言,相信無盡的力量,那是真愛永在。
我的信仰是無底深海,澎湃著心中火焰,燃燒無盡的力量,那是忠誠永在
專案分析
程式碼實現
FileWindow.java
import java.awt.CardLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class FileWindow extends JFrame implements ActionListener, Runnable { /* * 注意:因為實現了ActionListener * 和Runnable介面,所以必須要實現這兩個介面的方法。這裡我們先把這兩個方法簡單實現以下。下節課將徹底完成這兩個方法。 */ Thread compiler = null; Thread run_prom = null; boolean bn = true; CardLayout mycard; // 宣告佈局,以後會用到 File file_saved = null; JButton button_input_txt, // 按鈕的定義 button_compiler_text, button_compiler, button_run_prom, button_see_doswin; JPanel p = new JPanel(); JTextArea input_text = new JTextArea(); // 程式輸入區 JTextArea compiler_text = new JTextArea();// 編譯錯誤顯示區 JTextArea dos_out_text = new JTextArea();// 程式的輸出資訊 JTextField input_file_name_text = new JTextField(); JTextField run_file_name_text = new JTextField(); public FileWindow() { // TODO Auto-generated constructor stub super("Java語言編譯器"); mycard = new CardLayout(); compiler = new Thread(this); run_prom = new Thread(this); button_input_txt = new JButton("程式輸入區(白色)"); button_compiler_text = new JButton("編譯結果區(粉紅色)"); button_see_doswin = new JButton("程式執行結果(淺藍色)"); button_compiler = new JButton("編譯程式"); button_run_prom = new JButton("執行程式"); p.setLayout(mycard);// 設定卡片佈局 p.add("input", input_text);// 定義卡片名稱 p.add("compiler", compiler_text); p.add("dos", dos_out_text); add(p, "Center"); compiler_text.setBackground(Color.pink); // 設定顏色 dos_out_text.setBackground(Color.cyan); JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(3, 3)); // 設定表格佈局 // 新增元件 p1.add(button_input_txt); p1.add(button_compiler_text); p1.add(button_see_doswin); p1.add(new JLabel("輸入編譯檔名(.java):")); p1.add(input_file_name_text); p1.add(button_compiler); p1.add(new JLabel("輸入應用程式主類名")); p1.add(run_file_name_text); p1.add(button_run_prom); add(p1, "North"); // 定義事件 button_input_txt.addActionListener(this); button_compiler.addActionListener(this); button_compiler_text.addActionListener(this); button_run_prom.addActionListener(this); button_see_doswin.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button_input_txt) { // 顯示程式輸入區 mycard.show(p, "input"); } else if (e.getSource() == button_compiler_text) { // 顯示編譯結果顯示區 mycard.show(p, "compiler"); } else if (e.getSource() == button_see_doswin) { // 顯示程式執行結果區 mycard.show(p, "dos"); } else if (e.getSource() == button_compiler) { // 如果是編譯按鈕,執行編譯檔案的方法 if (!(compiler.isAlive())) { compiler = new Thread(this); } try { compiler.start(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } mycard.show(p, "compiler"); } else if (e.getSource() == button_run_prom) { // 如果是執行按鈕,執行執行檔案的方法 if (!(run_prom.isAlive())) { run_prom = new Thread(this); } try { run_prom.start(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } mycard.show(p, "dos"); } } @Override public void run() { // TODO Auto-generated method stub if (Thread.currentThread() == compiler) { compiler_text.setText(null); String temp = input_text.getText().trim(); byte[] buffer = temp.getBytes(); int b = buffer.length; String file_name = null; file_name = input_file_name_text.getText().trim(); try { file_saved = new File(file_name); FileOutputStream writefile = null; writefile = new FileOutputStream(file_saved); writefile.write(buffer, 0, b); writefile.close(); } catch (Exception e) { // TODO: handle exception System.out.println("ERROR"); } try { // 獲得該程序的錯誤流,才可以知道執行結果到底是失敗了還是成功。 Runtime rt = Runtime.getRuntime(); InputStream in = rt.exec("javac " + file_name).getErrorStream(); // 通過Runtime呼叫javac命令。注意:“javac // ”這個字串是有一個空格的!! BufferedInputStream bufIn = new BufferedInputStream(in); byte[] shuzu = new byte[100]; int n = 0; boolean flag = true; // 輸入錯誤資訊 while ((n = bufIn.read(shuzu, 0, shuzu.length)) != -1) { String s = null; s = new String(shuzu, 0, n); compiler_text.append(s); if (s != null) { flag = false; } } // 判斷是否編譯成功 if (flag) { compiler_text.append("Compile Succeed!"); } } catch (Exception e) { // TODO: handle exception } } else if (Thread.currentThread() == run_prom) { // 執行檔案,並將結果輸出到dos_out_text dos_out_text.setText(null); try { Runtime rt = Runtime.getRuntime(); String path = run_file_name_text.getText().trim(); Process stream = rt.exec("java " + path);// 呼叫java命令 InputStream in = stream.getInputStream(); BufferedInputStream bisErr = new BufferedInputStream(stream.getErrorStream()); BufferedInputStream bisIn = new BufferedInputStream(in); byte[] buf = new byte[150]; byte[] err_buf = new byte[150]; @SuppressWarnings("unused") int m = 0; @SuppressWarnings("unused") int i = 0; String s = null; String err = null; // 列印編譯資訊及錯誤資訊 while ((m = bisIn.read(buf, 0, 150)) != -1) { s = new String(buf, 0, 150); dos_out_text.append(s); } while ((i = bisErr.read(err_buf)) != -1) { err = new String(err_buf, 0, 150); dos_out_text.append(err); } } catch (Exception e) { // TODO: handle exception } } } }
Main.java
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub FileWindow win=new FileWindow(); win.pack(); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); //設定視窗大小 win.setBounds(200, 180,550,360); win.setVisible(true); } }
執行結果
點選編譯按鈕會出現錯誤資訊,證明距離成功不遠了。
執行按鈕錯誤:
點選按鈕在程式輸入區(白色),寫一個簡單的測試小程式吧!程式碼如下:
class a { public static void main(String [] args) { System.out.println("Hello ShiYanLou"); } }
接著在輸入編譯檔名(.java)後面的文字框裡填入與類名相同的.java 檔案,如 a.java,點選編譯程式。
如果程式沒有出錯,那麼編譯結果顯示如下:
在輸入應用程式主類名後面的文字框裡填入類名,如 a,點選執行程式。
程式的執行結果將會顯示在淺藍色的文字域中。