熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java代碼運行器

2013-11-23 19:19:21  來源: Java核心技術 

  import javaawtBorderLayout;

  import javaawtColor;

  import javaawtEventQueue;

  import javaawteventActionEvent;

  import javaawteventActionListener;

  import javaioBufferedReader;

  import javaioBufferedWriter;

  import javaioFile;

  import javaioFileInputStream;

  import javaioFileOutputStream;

  import javaioInputStreamReader;

  import javaioOutputStreamWriter;

  import javaxswingJButton;

  import javaxswingJColorChooser;

  import javaxswingJFileChooser;

  import javaxswingJFrame;

  import javaxswingJLabel;

  import javaxswingJMenu;

  import javaxswingJMenuBar;

  import javaxswingJMenuItem;

  import javaxswingJOptionPane;

  import javaxswingJPanel;

  import javaxswingJScrollPane;

  import javaxswingJTextArea;

  import javaxswingJTextField;

  import javaxswingJTextPane;

  import javaxswingSwingUtilities;

  import javaxswingUIManager;

  import javaxswingborderEtchedBorder;

  import javaxswingtextBadLocationException;

  import javaxswingtextDocument;

  import javaxswingtextSimpleAttributeSet;

  import javaxswingtextStyleConstants;

  import javaxswingtextStyledEditorKit;

  public class RunJava extends JFrame {

  private JTextField textField_;

  private JTextField textField;

  private String url = SystemgetProperty(userdir) + \\;

  private JTextPane textArea_;

  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 ExitMarkColor;

  private File file;

  private String [] Keyword = {abstractdefaultifprivatethisbooleandoimplementsprotectedthrowbreakdoubleimportpublicthrowsbyteelseinstanceofreturntransientcaseextendsintshorttrycatchfinalinterfacestaticvoidcharfinallylongstrictfpvolatileclassfloatnativesuperwhileconstfornewswitchcontinuegotopackagesynchronized};

  public static void main(String args[]) {

  EventQueueinvokeLater(new Runnable() {

  public void run() {

  try {

  RunJava frame = new RunJava();

  framesetVisible(true);

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  });

  }

  public RunJava() throws Exception {

  super(Java代碼運行器);

  File = new JMenu(文件);

  Part = new JMenuBar();

  Edit = new JMenu(編輯);

  Partadd(File);

  Partadd(Edit);

  Open = new JMenuItem(打開);

  OpenaddActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg) {

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

  fileChoosersetFileSelectionMode(JFileChooserFILES_AND_DIRECTORIES); // 設置文件選擇模式此處為文件和目錄均可

  if (fileChoosershowOpenDialog(RunJavathis) == JFileChooserAPPROVE_OPTION) { // 彈出文件選擇器並判斷是否點擊了打開按鈕

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

  Systemoutprintln(fileName);

  file = new File(fileName);

  InputData();

  }

  }

  });

  Save = new JMenuItem(保存);

  SaveaddActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg) {

  if(file==null){

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

  fileChoosersetFileSelectionMode(JFileChooserFILES_AND_DIRECTORIES); // 設置文件選擇模式此處為文件和目錄均可

  if (fileChoosershowSaveDialog(RunJavathis) == JFileChooserAPPROVE_OPTION) { // 彈出文件選擇器並判斷是否點擊了打開按鈕

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

  Systemoutprintln(fileName);

  file = new File(fileName);

  OutputData();

  }

  }else{

  OutputData();

  }

  }

  });

  SaveAs = new JMenuItem(另存為);

  SaveAsaddActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg) {

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

  fileChoosersetFileSelectionMode(JFileChooserFILES_AND_DIRECTORIES); // 設置文件選擇模式此處為文件和目錄均可

  if (fileChoosershowSaveDialog(RunJavathis) == JFileChooserAPPROVE_OPTION) { // 彈出文件選擇器並判斷是否點擊了打開按鈕

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

  file = new File(fileName);

  OutputData();

  }

  }

  });

  Exit = new JMenuItem(退出);

  ExitaddActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg) {

  int j=JOptionPaneshowConfirmDialog(RunJavathis真的要退出嗎?退出JOptionPaneYES_OPTIONJOptionPaneQUESTION_MESSAGE);

  if (j == JOptionPaneYES_OPTION){

  Systemexit();

  }

  }

  });

  Fileadd(Open);

  Fileadd(Save);

  Fileadd(SaveAs);

  Fileadd(Exit);

  InsertMain = new JMenuItem(新建);

  InsertMainaddActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg) {

  String str = public class test {\n

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

  +     Systemoutprintln(\hello java\);\n

  +    }\n + };

  textArea_setText(str);

  textFieldsetText(getName(textArea_getText()));

  }

  });

  MarkColor = new JMenuItem(標注顏色);

  MarkColoraddActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent e) {

  String str = public class test {\n

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

  +     Systemoutprintln(\hello java\);\n

  +    }\n + };

  int a = strindexOf(class);

  //     Systemoutprintln(a);

  setDocs(strsubstring(aa+) ColorRED true );

  }

  });

  Editadd(InsertMain);

  Editadd(MarkColor);

  setJMenuBar(Part);

  getContentPane()setLayout(null);

  setBounds( );

  setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  final JPanel panel = new JPanel();

  panelsetLayout(new BorderLayout());

  panelsetBounds( );

  getContentPane()add(panel);

  textArea_ = new JTextPane();

  paneladd(new JScrollPane(textArea_) BorderLayoutCENTER);

  final JPanel panel_ = new JPanel();

  panel_setLayout(new BorderLayout());

  panel_setBorder(new EtchedBorder(EtchedBorderLOWERED));

  panel_setForeground(ColorBLUE);

  panel_setBounds( );

  getContentPane()add(panel_);

  textArea = new JTextArea();

  panel_add(new JScrollPane(textArea) BorderLayoutCENTER);

  try {

  UIManagersetLookAndFeel(comsunjavaswingplafnimbusNimbusLookAndFeel);

  } catch (Exception ex) {

  try {

  UIManagersetLookAndFeel(comsunjavaswingplafwindowsWindowsClassicLookAndFeel);

  } catch (Exception ex) {

  UIManagersetLookAndFeel(comsunjavaswingplafwindowsWindowsLookAndFeel);

  }

  }

  SwingUtilitiesupdateComponentTreeUI(this);

  textField = new JTextField();

  textFieldsetToolTipText(不填自動從Java代碼中提取類名);

  textFieldsetBounds( );

  getContentPane()add(textField);

  final JLabel label = new JLabel();

  labelsetText(類名:);

  labelsetBounds( );

  getContentPane()add(label);

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

  textField_ = new JTextField();

  textField_setBounds( );

  textField_setToolTipText(可以和Java交互信息);

  getContentPane()add(textField_);

  final JButton button_ = new JButton();

  button_addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg) {

  cmd = textField_getText();

  }

  });

  button_setText(CMD命令發送);

  button_setBounds( );

  getContentPane()add(button_);

  final JButton button_ = new JButton();

  button_addActionListener(new ActionListener() {

  public void actionPerformed(final ActionEvent arg) {

  String name = textFieldgetText();

  if (nametrim()length() == ) {

  textFieldsetText(getName(textArea_getText()));

  }

  FileOut(textFieldgetText() + java textArea_getText());

  File file = new File(url + textFieldgetText() + class);

  if (fileexists()) {

  filedelete();

  }

  String exe = cmd;

  try {

  pro = RuntimegetRuntime()exec(exe);

  if (InPutThread != null)InPutThreadstop();

  if (OutPutThread != null)InPutThreadstop();

  if (ErrorThread != null)InPutThreadstop();

  InPutThread = new Thread(new RunTimeInput());

  OutPutThread = new Thread(new RunTimeOutput(new InputStreamReader(progetInputStream())));

  ErrorThread = new Thread(new RunTimeOutput(new InputStreamReader(progetErrorStream())));

  InPutThreadstart();

  OutPutThreadstart();

  ErrorThreadstart();

  exec(javac + textFieldgetText() + java);

  Threadsleep();

  exec(java + textFieldgetText() + );

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  });

  button_setText(編譯/運行);

  button_setBounds( );

  getContentPane()add(button_);

  }

  public void FileOut(String name String Content) {

  File file = new File(url + name);

  if (fileexists())

  filedelete();

  try {

  filecreateNewFile();

  FileOutputStream out = new FileOutputStream(file);

  byte buf[] = ContentgetBytes();

  outwrite(buf);

  outflush();

  outclose();

  } catch (Exception ex) {

  exprintStackTrace();

  }

  }

  public class RunTimeInput implements Runnable {

  private BufferedWriter bw = null;

  public void run() {

  bw = new BufferedWriter(new OutputStreamWriter(progetOutputStream()));

  while (true) {

  try {

  if (cmd != null && cmdlength() > ) {

  bwwrite(cmd + \n\n);

  bwflush();

  cmd = null;

  }

  Threadsleep();

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  }

  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 = brreadLine();

  if (s != null) {

  textAreasetText(textAreagetText() + \n + s);

  textAreasetSelectionStart(textAreagetText()length());

  }

  Threadsleep();

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  }

  public void exec(String Content) {

  cmd = Content;

  }

  public String getName(String str) {

  String name = ;

  if (strindexOf(class) != ) {

  String[] s = strsplit(\\s+);

  for (int i = ; i < slength; i++) {

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

  if ((i + ) < slength)

  name = s[i + ];

  int a = ;

  if((a=nameindexOf({))!=){

  name= namesubstring(a);

  }

  }

  }

  }

  return name;

  }

  public void OutputData() {

  try {

  FileOutputStream out = new FileOutputStream(file);

  byte buf[] = textArea_getText()getBytes();

  outwrite(buf);

  outflush();

  outclose();

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  public void InputData() {

  try {

  FileInputStream in = new FileInputStream(file);

  int a = (int)filelength();

  byte buf[] = new byte[a];

  int len = inread(buf);

  if (len == ){

  Systemoutprintln(文件為空);

  }else{

  textArea_setText(new String(buf len));

  }

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  /**

  * @param str 內容

  * @param col 顏色

  * @param bold 是否粗體

  * @param fontSize 字體大小

  */

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

  SimpleAttributeSet attrSet = new SimpleAttributeSet();

  StyleConstantssetForeground(attrSet col);

  // 顏色

  if (bold == true) {

  StyleConstantssetBold(attrSet true);

  }// 字體類型

  StyleConstantssetFontSize(attrSet fontSize);

  // 字體大小

  Document doc = textArea_getDocument();

  try {

  docinsertString(docgetLength() str attrSet);

  } catch (BadLocationException e) {

  Systemoutprintln(BadLocationException: + e);

  }

  }

  }

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


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26653.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.