知识大全 Java代码运行器

Posted 文件

篇首语:学而不厌,诲人不倦。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java代码运行器相关的知识,希望对你有一定的参考价值。

Java代码运行器  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

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

  private JTextField textField;

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

  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 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 arg )

  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 arg )

  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 arg )

  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 arg )

  int j=JOptionPane showConfirmDialog(RunJava this 真的要退出吗? 退出 JOptionPane YES_OPTION JOptionPane QUESTION_MESSAGE);

  if (j == JOptionPane YES_OPTION)

  System exit( );

  

  

  );

  File add(Open);

  File add(Save);

  File add(SaveAs);

  File add(Exit);

  InsertMain = new JMenuItem( 新建 );

  InsertMain addActionListener(new ActionListener()

  public void actionPerformed(final ActionEvent arg )

  String str = public class test \\n

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

  +     System out println(\\ hello java\\ );\\n

  +    \\n + ;

  textArea_ setText(str);

  textField setText(getName(textArea_ 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+ ) Color RED true );

  

  );

  Edit add(InsertMain);

  Edit add(MarkColor);

  setJMenuBar(Part);

  getContentPane() setLayout(null);

  setBounds( );

  setDefaultCloseOperation(JFrame EXIT_ON_CLOSE);

  final JPanel panel = new JPanel();

  panel setLayout(new BorderLayout());

  panel setBounds( );

  getContentPane() add(panel);

  textArea_ = new JTextPane();

  panel add(new JScrollPane(textArea_ ) BorderLayout CENTER);

  final JPanel panel_ = new JPanel();

  panel_ setLayout(new BorderLayout());

  panel_ setBorder(new EtchedBorder(EtchedBorder LOWERED));

  panel_ setForeground(Color BLUE);

  panel_ setBounds( );

  getContentPane() add(panel_ );

  textArea = new JTextArea();

  panel_ add(new JScrollPane(textArea) BorderLayout CENTER);

  try

  UIManager setLookAndFeel( sun java swing plaf nimbus NimbusLookAndFeel );

   catch (Exception ex )

  try

  UIManager setLookAndFeel( sun java swing plaf windows WindowsClassicLookAndFeel );

   catch (Exception ex )

  UIManager setLookAndFeel( sun java swing plaf windows WindowsLookAndFeel );

  

  

  SwingUtilities updateComponentTreeUI(this);

  textField = new JTextField();

  textField setToolTipText( 不填自动从Java代码中提取类名 );

  textField setBounds( );

  getContentPane() add(textField);

  final JLabel label = new JLabel();

  label setText( 类名: );

  label setBounds( );

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

  if (name trim() length() == )

  textField setText(getName(textArea_ getText()));

  

  FileOut(textField getText() + java textArea_ 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( );

  exec( java + textField getText() + );

   catch (Exception e)

  e printStackTrace();

  

  

  );

  button_ setText( 编译/运行 );

  button_ setBounds( );

  getContentPane() add(button_ );

  

  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() > )

  bw write(cmd + \\n\\n );

  bw flush();

  cmd = null;

  

  Thread sleep( );

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

   catch (Exception e)

  e printStackTrace();

  

  

  

  

  public void exec(String Content)

  cmd = Content;

  

  public String getName(String str)

  String name = ;

  if (str indexOf( class ) != )

  String[] s = str split( \\\\s+ );

  for (int i = ; i < s length; i++)

  if (s[i] equals( class ))

  if ((i + ) < s length)

  name = s[i + ];

  int a = ;

  if((a=name indexOf( ))!= )

  name= name substring( a);

  

  

  

  

  return name;

  

  public void OutputData()

  try

  FileOutputStream out = new FileOutputStream(file);

  byte buf[] = textArea_ 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 == )

  System out println( 文件为空 );

  else

  textArea_ setText(new String(buf 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_ getDocument();

  try

  doc insertString(doc getLength() str attrSet);

   catch (BadLocationException e)

  System out println( BadLocationException: + e);

  

  

  

cha138/Article/program/Java/hx/201311/26653

相关参考