Java代碼運行器

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  import java.awt.BorderLayout;

  import java.awt.Color;

  import java.awt.EventQueue;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.io.BufferedReader;

  import java.io.BufferedWriter;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileOutputStream;

  import java.io.InputStreamReader;

  import java.io.OutputStreamWriter;

  import javax.swing.JButton;

  import javax.swing.JColorChooser;

  import javax.swing.JFileChooser;

  import javax.swing.JFrame;

  import javax.swing.JLabel;

  import javax.swing.JMenu;

  import javax.swing.JMenuBar;

  import javax.swing.JMenuItem;

  import javax.swing.JOptionPane;

  import javax.swing.JPanel;

  import javax.swing.JScrollPane;

  import javax.swing.JTextArea;

  import javax.swing.JTextField;

  import javax.swing.JTextPane;

  import javax.swing.SwingUtilities;

  import javax.swing.UIManager;

  import javax.swing.border.EtchedBorder;

  import javax.swing.text.BadLocationException;

  import javax.swing.text.Document;

  import javax.swing.text.SimpleAttributeSet;

  import javax.swing.text.StyleConstants;

  import javax.swing.text.StyledEditorKit;

  public class RunJava extends JFrame {

  private JTextField textField_1;

  private JTextField textField;

  private String url = System.getProperty("user.dir") "\\";

  private JTextPane textArea_1;

  private JTextArea textArea;

  private static String cmd = "first";

  private static Process pro = null;

  private Thread InPutThread = null;

  private Thread OutPutThread = null;

  private Thread ErrorThread = null;

  private JMenu File, Edit;

  private JMenuBar Part;

  private JMenuItem Open, InsertMain, SaveAs, Save, Exit,MarkColor;

  private File file;

  private String [] Keyword = {"abstract","default","if","private","this","boolean","do","implements","protected","throw","break","double","import","public","throws","byte","else","instanceof","return","transient","case","extends","int","short","try","catch","final","interface","static","void","char","finally","long","strictfp","volatile","class","float","native","super","while","const","for","new","switch","continue","goto","package","synchronized"};

  public static void main(String args[]) {

  EventQueue.invokeLater(new Runnable() {

  public void run() {

  try {

  RunJava frame = new RunJava();

  frame.setVisible(true);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  });

  }

  public RunJava() throws Exception {

  super("Java代碼運行器");

  File = new JMenu("文件");

  Part = new JMenuBar();

  Edit = new JMenu("編輯");

  Part.add(File);

  Part.add(Edit);

  Open = new JMenuItem("打開");

  Open.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg0) {

  JFileChooser fileChooser = new JFileChooser(); // 實例化文件選擇器

  fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 設置文件選擇模式,此處為文件和目錄均可

  if (fileChooser.showOpenDialog(RunJava.this) == JFileChooser.APPROVE_OPTION) { // 彈出文件選擇器,並判斷是否點擊了打開按鈕

  String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到選擇文件或目錄的絕對路徑

  System.out.println(fileName);

  file = new File(fileName);

  InputData();

  }

  }

  });

  Save = new JMenuItem("保存");

  Save.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg0) {

  if(file==null){

  JFileChooser fileChooser = new JFileChooser(); // 實例化文件選擇器

  fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 設置文件選擇模式,此處為文件和目錄均可

  if (fileChooser.showSaveDialog(RunJava.this) == JFileChooser.APPROVE_OPTION) { // 彈出文件選擇器,並判斷是否點擊了打開按鈕

  String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到選擇文件或目錄的絕對路徑

  System.out.println(fileName);

  file = new File(fileName);

  OutputData();

  }

  }else{

  OutputData();

  }

  }

  });

  SaveAs = new JMenuItem("另存為");

  SaveAs.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg0) {

  JFileChooser fileChooser = new JFileChooser(); // 實例化文件選擇器

  fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 設置文件選擇模式,此處為文件和目錄均可

  if (fileChooser.showSaveDialog(RunJava.this) == JFileChooser.APPROVE_OPTION) { // 彈出文件選擇器,並判斷是否點擊了打開按鈕

  String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到選擇文件或目錄的絕對路徑

  file = new File(fileName);

  OutputData();

  }

  }

  });

  Exit = new JMenuItem("退出");

  Exit.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg0) {

  int j=JOptionPane.showConfirmDialog(RunJava.this,"真的要退出嗎?","退出",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);

  if (j == JOptionPane.YES_OPTION){

  System.exit(0);

  }

  }

  });

  File.add(Open);

  File.add(Save);

  File.add(SaveAs);

  File.add(Exit);

  InsertMain = new JMenuItem("新建");

  InsertMain.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg0) {

  String str = "public class test {\n"

   " public static void main(String[] args) {\n"

   " System.out.println(\"hello java\");\n"

   " }\n" "}";

  textArea_1.setText(str);

  textField.setText(getName(textArea_1.getText()));

  }

  });

  MarkColor = new JMenuItem("標註顏色");

  MarkColor.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent e) {

  String str = "public class test {\n"

   " public static void main(String[] args) {\n"

   " System.out.println(\"hello java\");\n"

   " }\n" "}";

  int a = str.indexOf("class");

  // System.out.println(a);

  setDocs(str.substring(a,a 5), Color.RED, true, 12);

  }

  });

  Edit.add(InsertMain);

  Edit.add(MarkColor);

  setJMenuBar(Part);

  getContentPane().setLayout(null);

  setBounds(100, 100, 880, 542);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  final JPanel panel = new JPanel();

  panel.setLayout(new BorderLayout());

  panel.setBounds(0, 32, 872, 231);

  getContentPane().add(panel);

  textArea_1 = new JTextPane();

  panel.add(new JScrollPane(textArea_1), BorderLayout.CENTER);

  final JPanel panel_1 = new JPanel();

  panel_1.setLayout(new BorderLayout());

  panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED));

  panel_1.setForeground(Color.BLUE);

  panel_1.setBounds(0, 269, 872, 216);

  getContentPane().add(panel_1);

  textArea = new JTextArea();

  panel_1.add(new JScrollPane(textArea), BorderLayout.CENTER);

  try {

  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

  } catch (Exception ex1) {

  try {

  UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");

  } catch (Exception ex2) {

  UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

  }

  }

  SwingUtilities.updateComponentTreeUI(this);

  textField = new JTextField();

  textField.setToolTipText("不填自動從Java代碼中提取類名");

  textField.setBounds(58, 4, 183, 22);

  getContentPane().add(textField);

  final JLabel label = new JLabel();

  label.setText("類名:");

  label.setBounds(10, 5, 42, 18);

  getContentPane().add(label);

  setResizable(false);// 屏蔽最大化按鈕

  textField_1 = new JTextField();

  textField_1.setBounds(393, 3, 275, 22);

  textField_1.setToolTipText("可以和Java交互信息");

  getContentPane().add(textField_1);

  final JButton button_1 = new JButton();

  button_1.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg0) {

  cmd = textField_1.getText();

  }

  });

  button_1.setText("CMD命令發送");

  button_1.setBounds(674, 1, 123, 28);

  getContentPane().add(button_1);

  final JButton button_2 = new JButton();

  button_2.addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg0) {

  String name = textField.getText();

  if (name.trim().length() == 0) {

  textField.setText(getName(textArea_1.getText()));

  }

  FileOut(textField.getText() ".java", textArea_1.getText());

  File file = new File(url textField.getText() ".class");

  if (file.exists()) {

  file.delete();

  }

  String exe = "cmd";

  try {

  pro = Runtime.getRuntime().exec(exe);

  if (InPutThread != null)InPutThread.stop();

  if (OutPutThread != null)InPutThread.stop();

  if (ErrorThread != null)InPutThread.stop();

  InPutThread = new Thread(new RunTimeInput());

  OutPutThread = new Thread(new RunTimeOutput(new InputStreamReader(pro.getInputStream())));

  ErrorThread = new Thread(new RunTimeOutput(new InputStreamReader(pro.getErrorStream())));

  InPutThread.start();

  OutPutThread.start();

  ErrorThread.start();

  exec("javac " textField.getText() ".java");

  Thread.sleep(1500);

  exec("java " textField.getText() "");

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  });

  button_2.setText("編譯/運行");

  button_2.setBounds(264, 1, 89, 28);

  getContentPane().add(button_2);

  }

  public void FileOut(String name, String Content) {

  File file = new File(url name);

  if (file.exists())

  file.delete();

  try {

  file.createNewFile();

  FileOutputStream out = new FileOutputStream(file);

  byte buf[] = Content.getBytes();

  out.write(buf);

  out.flush();

  out.close();

  } catch (Exception ex) {

  ex.printStackTrace();

  }

  }

  public class RunTimeInput implements Runnable {

  private BufferedWriter bw = null;

  public void run() {

  bw = new BufferedWriter(new OutputStreamWriter(pro.getOutputStream()));

  while (true) {

  try {

  if (cmd != null && cmd.length() > 0) {

  bw.write(cmd "\n\n");

  bw.flush();

  cmd = null;

  }

  Thread.sleep(600);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  }

  public class RunTimeOutput implements Runnable {

  InputStreamReader isr = null;

  public RunTimeOutput(InputStreamReader is) {

  isr = is;

  }

  private BufferedReader br = null;

  public void run() {

  br = new BufferedReader(isr);

  while (true) {

  try {

  String s = br.readLine();

  if (s != null) {

  textArea.setText(textArea.getText() "\n" s);

  textArea.setSelectionStart(textArea.getText().length());

  }

  Thread.sleep(10);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  }

  public void exec(String Content) {

  cmd = Content;

  }

  public String getName(String str) {

  String name = "";

  if (str.indexOf("class") != -1) {

  String[] s = str.split("\\s ");

  for (int i = 0; i < s.length; i ) {

  if (s[i].equals("class")) {

  if ((i 1) < s.length)

  name = s[i 1];

  int a = 0;

  if((a=name.indexOf("{"))!=-1){

  name= name.substring(0,a);

  }

  }

  }

  }

  return name;

  }

  public void OutputData() {

  try {

  FileOutputStream out = new FileOutputStream(file);

  byte buf[] = textArea_1.getText().getBytes();

  out.write(buf);

  out.flush();

  out.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  public void InputData() {

  try {

  FileInputStream in = new FileInputStream(file);

  int a = (int)file.length();

  byte buf[] = new byte[a];

  int len = in.read(buf);

  if (len == -1){

  System.out.println("文件為空");

  }else{

  textArea_1.setText(new String(buf, 0, len));

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  /**

  * @param str 內容

  * @param col 顏色

  * @param bold 是否粗體

  * @param fontSize 字體大小

  */

  public void setDocs(String str, Color col, boolean bold, int fontSize) {

  SimpleAttributeSet attrSet = new SimpleAttributeSet();

  StyleConstants.setForeground(attrSet, col);

  // 顏色

  if (bold == true) {

  StyleConstants.setBold(attrSet, true);

  }// 字體類型

  StyleConstants.setFontSize(attrSet, fontSize);

  // 字體大小

  Document doc = textArea_1.getDocument();

  try {

  doc.insertString(doc.getLength(), str, attrSet);

  } catch (BadLocationException e) {

  System.out.println("BadLocationException: " e);

  }

  }

  }

  //未寫完的...我想寫成Eclipse一樣 讓Java關鍵字變色``





[火星人 ] Java代碼運行器已經有403次圍觀

http://coctec.com/docs/java/show-post-60165.html