转-Java调用本机默认程序打开各种文件(Desktop类)
2011-11-22 20:06
591 查看
1 import java.awt.Desktop;
2 import java.awt.event.ActionEvent;
3 import java.awt.event.ActionListener;
4 import java.io.File;
5 import java.io.IOException;
6 import javax.swing.JButton;
7 import javax.swing.JFileChooser;
8 import javax.swing.JFrame;
9
10 public class TestDesktopOpenFile {
11 private JFrame frame;
12
13 /**
14 * Launch the application
15 *
16 * @param args
17 */
18 public static void main(String args[]) {
19 try {
20 TestDesktopOpenFile window = new TestDesktopOpenFile();
21 window.frame.setVisible(true);
22 } catch (Exception e) {
23 e.printStackTrace();
24 }
25 }
26
27 /**
28 * Create the application
29 */
30 public TestDesktopOpenFile() {
31 initialize();
32 }
33
34 /**
35 * Initialize the contents of the frame
36 */
37 private void initialize() {
38 frame = new JFrame();
39 frame.getContentPane().setLayout(null);
40 frame.setBounds(100, 100, 225, 86);
41 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42 final JButton openButton = new JButton();
43 openButton.setBounds(53, 10, 106, 31);
44 openButton.addActionListener(new ActionListener() {
45 public void actionPerformed(ActionEvent e) {
46 if (Desktop.isDesktopSupported()) {
47 Desktop desktop = Desktop.getDesktop();
48 JFileChooser jfc = new JFileChooser();
49 jfc.showOpenDialog(frame);
50 File file = jfc.getSelectedFile();
51 if (file != null) {
52 try {
53 desktop.open(file);
54 } catch (IOException e1) {
55 e1.printStackTrace();
56 }
57 }
58 } else {
59 System.out.println("不支持desktop");
60 }
61 }
62 });
63 openButton.setText("open");
64 frame.getContentPane().add(openButton);
65 }
66 }
67
2 import java.awt.event.ActionEvent;
3 import java.awt.event.ActionListener;
4 import java.io.File;
5 import java.io.IOException;
6 import javax.swing.JButton;
7 import javax.swing.JFileChooser;
8 import javax.swing.JFrame;
9
10 public class TestDesktopOpenFile {
11 private JFrame frame;
12
13 /**
14 * Launch the application
15 *
16 * @param args
17 */
18 public static void main(String args[]) {
19 try {
20 TestDesktopOpenFile window = new TestDesktopOpenFile();
21 window.frame.setVisible(true);
22 } catch (Exception e) {
23 e.printStackTrace();
24 }
25 }
26
27 /**
28 * Create the application
29 */
30 public TestDesktopOpenFile() {
31 initialize();
32 }
33
34 /**
35 * Initialize the contents of the frame
36 */
37 private void initialize() {
38 frame = new JFrame();
39 frame.getContentPane().setLayout(null);
40 frame.setBounds(100, 100, 225, 86);
41 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42 final JButton openButton = new JButton();
43 openButton.setBounds(53, 10, 106, 31);
44 openButton.addActionListener(new ActionListener() {
45 public void actionPerformed(ActionEvent e) {
46 if (Desktop.isDesktopSupported()) {
47 Desktop desktop = Desktop.getDesktop();
48 JFileChooser jfc = new JFileChooser();
49 jfc.showOpenDialog(frame);
50 File file = jfc.getSelectedFile();
51 if (file != null) {
52 try {
53 desktop.open(file);
54 } catch (IOException e1) {
55 e1.printStackTrace();
56 }
57 }
58 } else {
59 System.out.println("不支持desktop");
60 }
61 }
62 });
63 openButton.setText("open");
64 frame.getContentPane().add(openButton);
65 }
66 }
67
相关文章推荐
- Java调用系统默认程序打开本地文件
- Java调用系统默认程序打开本地文件
- Java调用系统默认程序打开本地文件
- VB 调用打开各种文件(自动查找使用默认的程序)
- Java调用本机程序完成打开网页、发送邮件和编辑文件
- Qt调用默认程序打开文件
- AIR程序调用本地默认应用程序打开本地文件
- 学习Java6(七)Desktop(3)使用默认程序打开所选文件
- 使用Qt打开外部文件,调用外部程序(QDesktopServices,QUrl ,QProcess,QProcess)
- 使用Intent调用系统其它程序打开本地各种类型的文件
- android 调用系统程序打开各种文件
- Java 调用系统默认程序打开
- Java调用本地相应程序打开文件
- java中用Windows默认程序打开文件
- 用Java作为默认程序打开Eclipse导出的jar文件
- Java 通过调用系统程序打开文件或者目录的方式
- WIN32,,打开文件没有默认程序,调用系统的默认程序选择窗口
- Java:调用CMD命令打开文件或系统程序
- AIR程序调用本地默认应用程序打开本地文件
- WindowForms调用系统默认程序打开文件