编辑器 java_Java编写的Java编辑器_JavaIDE

这个博客介绍了一个简单的Java IDE实现。它包含输入文本、编译器和运行程序的面板,支持文件保存、编译Java源代码以及查看运行输出。用户可以输入Java代码,点击按钮进行编译和运行,输出会显示在指定区域。
摘要由CSDN通过智能技术生成

importjava.awt.CardLayout;importjava.awt.Color;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedInputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.InputStream;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTextArea;importjavax.swing.JTextField;public classJavaIDE {public static voidmain(String[] args) {

FileWindow win= newFileWindow();

win.pack();

win.addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {

System.exit(0);

}

});

win.setBounds(200, 180, 550, 360);

win.setVisible(true);

}

}

@SuppressWarnings("serial")class FileWindow extends JFrame implementsActionListener, Runnable {

Thread compiler= null;

Thread run_prom= null;boolean bn = true;

CardLayout mycard;

File file_saved= null;

JButton button_input_text,

button_compiler_text, button_compiler, button_run_prom, button_see_doswin;

JPanel p= newJPanel();

JTextArea input_text= newJTextArea();

JTextArea compiler_text= newJTextArea();

JTextArea dos_out_text= newJTextArea();

JTextField input_file_name_text= newJTextField();

JTextField run_file_name_text= newJTextField();publicFileWindow() {super("JavaIDE");

mycard= newCardLayout();

compiler= new Thread(this);

run_prom= new Thread(this);

button_input_text= new JButton("ProgramInput(white)");

button_compiler_text= new JButton("Compiler(pick)");

button_see_doswin= new JButton("Run(blue)");

button_compiler= new JButton("compiler");

button_run_prom= new JButton("run");

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

p1.setLayout(new GridLayout(3, 3));

p1.add(button_input_text);

p1.add(button_compiler_text);

p1.add(button_see_doswin);

p1.add(new JLabel("Input File Name(.java):"));

p1.add(input_file_name_text);

p1.add(button_compiler);

p1.add(new JLabel("Input Class Name"));

p1.add(run_file_name_text);

p1.add(button_run_prom);

add(p1,"North");

button_input_text.addActionListener(this);

button_compiler_text.addActionListener(this);

button_compiler.addActionListener(this);

button_run_prom.addActionListener(this);

button_see_doswin.addActionListener(this);

}public voidrun() {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= newFile(file_name);

FileOutputStream writefile= null;

writefile= newFileOutputStream(file_saved);

writefile.write(buffer,0, b);

writefile.close();

}catch(Exception e) {

System.out.println("ERROR");

}try{

Runtime rt=Runtime.getRuntime();

InputStream in= rt.exec("javac " +file_name).getErrorStream();

BufferedInputStream bufIn= newBufferedInputStream(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("Compiler Succeed!");

}

}catch(Exception e) {

e.printStackTrace();

}

}else if (Thread.currentThread() ==run_prom) {

dos_out_text.setText(null);try{

Runtime rt=Runtime.getRuntime();

String path=run_file_name_text.getText().trim();

Process stream= rt.exec("java " +path);

InputStream in=stream.getInputStream();

BufferedInputStream bisErr= newBufferedInputStream(stream.getErrorStream());

BufferedInputStream bisIn= newBufferedInputStream(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) {

}

}

}public voidactionPerformed(ActionEvent e) {if (e.getSource() ==button_input_text) {

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

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

e2.printStackTrace();

}

mycard.show(p,"dos");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值