/**開啟檔案原理:調用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); }}