📄 example8_8.java
字号:
/* Runtime类应用 */
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class Example8_8
{ public static void main(String args[])
{ FileWindow win=new FileWindow();
win.pack();
win.addWindowListener(new WindowAdapter() //窗口适配器。
{public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
win.setBounds(200,180,550,360);
win.setVisible(true);
}
}
class FileWindow extends JFrame implements ActionListener,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=new JPanel();
JTextArea input_text=new JTextArea(), //程序输入区。
compiler_text=new JTextArea(), //编译出错显示区。
dos_out_text=new JTextArea(); //程序的输出信息。
JTextField input_flie_name_text=new JTextField("编译文件");
JTextField run_file_name_text=new JTextField("主类");
FileWindow()
{ super("Java语言编译器");
mycard=new CardLayout();
compiler=new Thread(this);
run_prom=new Thread(this);
button_input_text=new JButton("程序输入区(白色)");
button_compiler_text=new JButton("编译结果区(粉红色)");
button_see_doswin=new JButton("程序运行结果(浅蓝色)");
button_compiler=new JButton("编译程序");
button_run_prom=new JButton("运行应用程序");
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=new JPanel();
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("输入编译文件名(.java) :"));
p1.add(input_flie_name_text);
p1.add(button_compiler);
p1.add(new JLabel("输入应用程序主类名:"));
p1.add(run_file_name_text);
p1.add(button_run_prom);
// add(p1,BorderLayout.NORTH);
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 void actionPerformed(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 eee){}
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 eee){}
mycard.show(p,"dos");
}
}
public void run()
{ if(Thread.currentThread()==compiler)
{ compiler_text.setText(null);
String temp=input_text.getText().trim();
byte buffer[]=temp.getBytes();
int b=buffer.length;
String flie_name;
flie_name=input_flie_name_text.getText().trim();
try{ file_saved=new File(flie_name);
FileOutputStream writefile;
writefile=new FileOutputStream(file_saved);
writefile.write(buffer,0,b);writefile.close();
}
catch(IOException e5)
{ System.out.println("Error "); }
try{ Runtime rt=Runtime.getRuntime();
InputStream in=
rt.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,n);
compiler_text.append(s);
if(s!=null) bn=false;
}
if(bn)
{ compiler_text.append("编译正确"); }
}
catch(IOException e1){ System.out.println(e1);}
}
else if(Thread.currentThread()==run_prom)
{ dos_out_text.setText(null);
try{ Runtime rt=Runtime.getRuntime();
String path=run_file_name_text.getText().trim();
InputStream in=rt.exec("java "+path).getInputStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte zu[]=new byte[150];
int m;String s=null;
while((m=bin.read(zu,0,150))!=-1)
{ s=new String(zu,0,m);
dos_out_text.append(s);
}
}
catch(IOException e1){ System.out.println(e1);}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -