📄 compilejava.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class CompileJava{
public static void main(String args[]){
Compiler compile = new Compiler();
compile.pack();
//窗口增加适配器
compile.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}
});
//设定程序界面尺寸及可见
compile.setBounds(100,120,700,360);
compile.setVisible(true);
}
}
class Compiler extends Frame implements ActionListener,Runnable {
//负责编译的线程
Thread compiler = null;
//负责运行程序的线程
Thread runProm = null,runApplet = null;
boolean bn = true;
CardLayout mycard;
Panel p = new Panel();
File fileSaved = null;
//程序输入区
TextArea inputText = new TextArea();
//编译出错显示区
TextArea compilerText = new TextArea();
//程序运行时,负责显示在dos窗口的输出信息
TextArea dosText=new TextArea();
Button btnInput,btnCompiler,
btnCom,btnRun,btnSee;
TextField fileText = new TextField("输入文件名字.java");
TextField nameText = new TextField("输入主类的名字");
Compiler() {
//程序界面初始化
super("Java编程小软件");
mycard = new CardLayout();
btnInput = new Button("程序输入区(白色)");
btnCompiler = new Button("编译结果区(粉色)");
btnCom = new Button("编译程序");
btnRun = new Button("运行应用程序");
btnSee = new Button("查看应用程序运行时在dos窗口输出的信息");
p.setLayout(mycard);
p.add("input",inputText);
p.add("compiler",compilerText);
p.add("dos",dosText);
add(p,"Center");
add( btnSee,"South");
compilerText.setBackground(Color.pink);
Panel p1 = new Panel();p1.setLayout(new GridLayout(4,2));
p1.add(new Label("按扭输入源程序:"));
p1.add(btnInput);
p1.add(new Label("按扭看编译结果:"));
p1.add(btnCompiler);
p1.add(fileText);
p1.add(btnCom);
p1.add(nameText);
p1.add(btnRun);
add(p1,"North");
btnInput.addActionListener(this);
btnCompiler.addActionListener(this);
btnCom.addActionListener(this);
btnRun.addActionListener(this);
btnSee.addActionListener(this);
}
//定义按钮点击事件
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnInput) {
mycard.show(p,"input");
}
else if(e.getSource() == btnCompiler) {
mycard.show(p,"compiler");
}
else if(e.getSource() == btnSee) {
mycard.show(p,"dos");
}
else if(e.getSource() == btnCom) {
compiler = new Thread(this);
compiler.start();
mycard.show(p,"compiler");
}
else if(e.getSource() == btnRun) {
runProm = new Thread(this);
runProm.start();
mycard.show(p,"dos");
}
}
public void run() {
if(Thread.currentThread() == compiler) {
compilerText.setText(null);
String temp = inputText.getText().trim();
byte buffer[] = temp.getBytes();
int b = buffer.length;
String flie_name = fileText.getText().trim();
try{
//建立文件输入流
fileSaved=new File(flie_name);
FileOutputStream whritefile=new FileOutputStream(fileSaved);
whritefile.write(buffer,0,b);
whritefile.close();
} catch(IOException e5){
System.out.println("Error ");
}
try{
//执行外部命令
Runtime ce=Runtime.getRuntime();
InputStream in=ce.exec("javac "+flie_name).getErrorStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte shuzu[]=new byte[100];
int n;
boolean bn=true;
while((n=bin.read(shuzu,0,100))!=-1) {
String s=null;
s=new String(shuzu,0,100);
compilerText.append(s);
if(s!=null)
bn=false;
}
if(bn){
compilerText.append("编译正确");
}
}
catch(IOException e1){ }
}
else if(Thread.currentThread()==runProm) {
dosText.setText(null);
try{
//执行外部命令
Runtime ce=Runtime.getRuntime();
String path=nameText.getText().trim();
InputStream in=ce.exec("java "+path).getInputStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte zu[]=new byte[150];
int fine;
String s=null;
while((fine=bin.read(zu,0,150))!=-1);{
s=new String(zu,0,150);
dosText.append(s);
}
}
catch(IOException e1){ }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -