⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example10_19.java

📁 不错的教程 适合中高级人员的使用
💻 JAVA
字号:

import java.awt.*;import java.io.*;import java.awt.event.*;
public class Example10_19
{  public static void main(String args[])
   {  JDK f=new JDK();
      f.pack();
      f.addWindowListener(new WindowAdapter()  //窗口增加适配器。
                   {public void windowClosing(WindowEvent e)
                          {  System.exit(0);
                          }
                    });
      f.setBounds(100,120,700,360);
      f.setVisible(true);
   }
}
class  JDK extends Frame implements ActionListener,Runnable
{  Thread compiler=null;  //负责编译的线程。
   Thread run_prom=null; //负责运行程序的线程。
   boolean bn=true;
   CardLayout mycard;
   Panel p=new Panel();
   File file_saved=null;
   TextArea input_text=new TextArea(),//程序输入区。
   compiler_text=new TextArea(),     //编译出错显示区。
   dos_out_text=new TextArea();      //程序运行时,负责显示在dos窗口的输出信息。
   Button button_input_text,button_compiler_text,
   button_compiler,button_run_prom,button_see_doswin;
   TextField input_flie_name_text=new TextField("输入被编译的文件名字.java");
   TextField run_file_name_text=new TextField("输入应用程序主类的名字");
   JDK()
   {  super("Java编程小软件");
      mycard=new CardLayout();
      compiler=new Thread(this);
      run_prom=new Thread(this);
      button_input_text=new Button("程序输入区(白色)");
      button_compiler_text=new Button("编译结果区(粉色)");
      button_compiler=new Button("编译程序");
      button_run_prom=new Button("运行应用程序");
      button_see_doswin=new  Button("查看应用程序运行时在dos窗口输出的信息");
      p.setLayout(mycard);
      p.add("input",input_text);p.add("compiler",compiler_text);
      p.add("dos",dos_out_text);
      add(p,"Center");
      add( button_see_doswin,"South"); 
      compiler_text.setBackground(Color.pink);
      dos_out_text.setBackground(Color.yellow);
      Panel p1=new Panel();p1.setLayout(new GridLayout(4,2));
      p1.add(new Label("按扭输入源程序:"));p1.add(button_input_text);
      p1.add(new Label("按扭看编译结果:"));p1.add(button_compiler_text); 
      p1.add(input_flie_name_text);  p1.add(button_compiler);
      p1.add(run_file_name_text); p1.add(button_run_prom);
      add(p1,BorderLayout.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=input_flie_name_text.getText().trim();
           try{  file_saved=new File(flie_name);
                 FileOutputStream writefile=new FileOutputStream(file_saved);
                 writefile.write(buffer,0,b);writefile.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,n);
                      compiler_text.append(s);
                      if(s!=null) bn=false;
                    }
                if(bn) 
                    {  compiler_text.append("编译正确"); 
                    } 
             } 
           catch(IOException e1){} 
       }
      else if(Thread.currentThread()==run_prom)
       {   dos_out_text.setText(null);
           try{  Runtime ce=Runtime.getRuntime();
                 String path=run_file_name_text.getText().trim();
                 InputStream in=ce.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){} 
        } 
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -