用java程序写一个窗口打开exe文件
package test;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Test01 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField text;
JButton qq,wx,yd,llq;
public static void main(String[] args) {
new Test01();
}
public Test01(){
text =new JTextField(15);
this.setLocation(800,300);
this.setTitle("ces");
this.setSize(314,252);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocation(800,300);
Panel p = new Panel();
// p.add(new TextField(20));
qq=new JButton("QQ");
wx=new JButton("微信");
yd=new JButton("有道词典");
llq=new JButton("浏览器");
qq.setFocusPainted(false);
wx.setFocusPainted(false);
yd.setFocusPainted(false);
llq.setFocusPainted(false);
p.add(qq);
p.add(wx);
p.add(yd);
p.add(llq);
p.setLayout(new GridLayout(2,2));
this.add(p);
// this.add(text,BorderLayout.NORTH);
MyEvent();//事件处理
}
public void MyEvent(){
Text(qq);
Text(wx);
Text(yd);
Text(llq);
}
//各按钮的时间处理方法
public void Text(JButton button){
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String str=button.getText();
// System.out.println(str);
// text.setText(text.getText() + str);
if("QQ".equals(str)) {
System.out.println("点击了QQ");
Test01.openExe("D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\","QQScLauncher.exe");
}
if("微信".equals(str)) {
System.out.println("点击了微信");
Test01.openExe("D:\\Program Files (x86)\\Tencent\\WeChat\\","WeChat.exe");
}
if("有道词典".equals(str)) {
System.out.println("点击了有道词典");
Test01.openExe("D:\\Program Files (x86)\\Youdao\\Dict\\","YoudaoDict.exe");
}
if("浏览器".equals(str)) {
System.out.println("点击了浏览器");
Test01.openExe("D:\\Program Files (x86)\\360Chrome\\Chrome\\Application","360chrome.exe");
}
}
});
}
public static void openExe(String exeUrl , String ml){
Runtime rn = Runtime.getRuntime();
Process p = null;
File dir = new File(exeUrl);
try {
//到指定目录下执行exe程序,exe程序可直接执行
// p = rn.exec("colorcpl.exe /C start" , null , new File(exeUrl));
//到指定目录下通过cmd执行,以命令行执行的exe程序
p = rn.exec("cmd.exe /k start " + ml , null , dir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果图