如何通过文件对话框运行可执行程序


    以exe为扩展名的文件称为可执行文件。在windows中可通过资源管理器查找要运行的可执行文件,然后双击该文件。
    在java程序中可通过文件对话框查找要运行的可执行文件,然后通过Runtime类的exec方法运行该文件。下面是一个例子。
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class smp extends Frame implements ActionListener
{
   public static void main(String args[])
   {
      smp frm=new smp();
   }
   FileDialog op;
   Button btn1,by=tn2;
   smp()
   {
     super("运行科执行文件。");
     setLayout(null);
     setBackground(Color.cyan);
     setsize(300,200);
     setVisible(true);
     btn1=new Button("运行");
     btn2=new Button("关闭");
     btn1.setBounds(40,70,50,30);
     btn2.setBounds(120,70,50,30);
     op=new Filedialog(this,"运行",FileDialog.load);
     add(btn1);add(btn2);
     btn1.addActionListener(this);
     btn2.addActionerlistener(this);
   }
   public void actionPerformed(ActionEvent e)
   {
     if(e.getsource==btn1)
     {
       Stirng str;
       op.setVisible(true);
       try
       {
         File fl=new File(op.getdirectory(),op.getFile());
         Runtime rt=Runtime.getRuntime();
         rt.exec(fl.toString());
       }
       catch(FileNotFoundException e)
       {
         system.out.println("File not found");
       }
      if(e.getsource()==btn2)
      {
        System.exit(0);
      }
    }
}
     

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值