Java開啟本地檔案/exe/開啟網址/調用cmd__Java

來源:互聯網
上載者:User


/**開啟檔案原理:調用cmd命令執行**/import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.io.IOException;public class ss extends JFrame { private JPanel contentPane; public static void main(String[] args) {  EventQueue.invokeLater(new Runnable() {   public void run() {    try {     ss frame = new ss();     frame.setVisible(true);    } catch (Exception e) {     e.printStackTrace();    }   }  }); } public ss() {  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  setBounds(100, 100, 450, 300);  contentPane = new JPanel();  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  setContentPane(contentPane);  contentPane.setLayout(null);    JButton btnNewButton = new JButton("\u6253\u5F00\u8BA1\u7B97\u5668");  btnNewButton.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e) {    try {     //開啟計算機     Runtime.getRuntime().exec("calc");     //用記事本開啟本地的txt檔案     //Runtime.getRuntime().exec("notepad D:/help.txt");              } catch (IOException e1) {     e1.printStackTrace();    }   }  });  btnNewButton.setBounds(79, 60, 137, 23);  contentPane.add(btnNewButton);    JButton btnexe = new JButton("\u8FD0\u884C\u672C\u5730exe");  btnexe.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e) {    try {     //開啟本地的exe檔案  可以絕對路徑 也可以相對路徑 相對路徑在工程目錄建立檔案夾     Runtime.getRuntime().exec("exe/註冊補丁");         } catch (IOException e1) {     e1.printStackTrace();    }   }  });  btnexe.setBounds(79, 166, 137, 23);  contentPane.add(btnexe);    JButton btnNewButton_1 = new JButton("\u53EF\u6253\u5F00\u8FD0\u884C\u672C\u5730\u7684\u6587\u4EF6  \u4EFB\u610F\u683C\u5F0F");  btnNewButton_1.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e) {    //開啟本地的任何檔案    String cmd="rundll32 url.dll FileProtocolHandler file://D:/mysqljdbc.png ";    try {     Process p = Runtime.getRuntime().exec(cmd);    } catch (IOException e1) {     e1.printStackTrace();    }   }  });  btnNewButton_1.setBounds(79, 117, 223, 23);  contentPane.add(btnNewButton_1);    JButton button = new JButton("\u6253\u5F00\u7F51\u5740");  button.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e) {    try {     //開啟網址     Runtime.getRuntime().exec("cmd /c start iexplore http://www.baidu.com");    } catch (IOException e1) {     e1.printStackTrace();    }   }  });  button.setBounds(81, 214, 93, 23);  contentPane.add(button); }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.