java

位置:IT落伍者 >> java >> 浏览文章

Java代码运行器


发布日期:2018年12月30日
 
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关键字变色``

               

上一篇:java资源获取分析

下一篇:分析 Java 中乱码问题产生的根源