用java程序写一个窗口打开exe文件

用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();
	        }
	    }
	
}

效果图
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值