学习Java6(七)Desktop(4)使用默认程序编辑所选文件
2007-04-18 20:24
267 查看
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 import javax.swing.UIManager;
10
11 public class TestDesktopEditFile {
12 private JFrame frame;
13
14 /**
15 * Launch the application
16 *
17 * @param args
18 */
19 public static void main(String args[]) {
20 try {
21 TestDesktopEditFile window = new TestDesktopEditFile();
22 window.frame.setVisible(true);
23 } catch (Exception e) {
24 e.printStackTrace();
25 }
26 }
27
28 /**
29 * Create the application
30 */
31 public TestDesktopEditFile() {
32 initialize();
33 }
34
35 /**
36 * Initialize the contents of the frame
37 */
38 private void initialize() {
39 try {
40 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
41 } catch (Exception ex) {
42 ex.printStackTrace();
43 }
44 frame = new JFrame();
45 frame.getContentPane().setLayout(null);
46 frame.setBounds(100, 100, 225, 86);
47 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48 final JButton openButton = new JButton();
49 openButton.setBounds(53, 10, 106, 31);
50 openButton.addActionListener(new ActionListener() {
51 public void actionPerformed(ActionEvent e) {
52 if (Desktop.isDesktopSupported()) {
53 Desktop desktop = Desktop.getDesktop();
54 JFileChooser jfc = new JFileChooser();
55 jfc.showOpenDialog(frame);
56 File file = jfc.getSelectedFile();
57 if (file != null) {
58 try {
59 desktop.edit(file);//就这一行的区别
60 } catch (IOException e1) {
61 e1.printStackTrace();
62 }
63 }
64 } else {
65 System.out.println("不支持desktop");
66 }
67 }
68 });
69 openButton.setText("edit");//哦哦哦,还有这一行也不一样,呵呵
70 frame.getContentPane().add(openButton);
71 }
72 }
73
换了windows的风格以后界面顺眼多了
但是有点不理解
为啥不能自动判断操作系统去改变界面风格?
该类型文件没有对应的默认编辑程序的时候,会抛出异常,不能像WINDOWS下面那样弹出一个对话框让你从已知的程序列表中选择一个。
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 import javax.swing.UIManager;
10
11 public class TestDesktopEditFile {
12 private JFrame frame;
13
14 /**
15 * Launch the application
16 *
17 * @param args
18 */
19 public static void main(String args[]) {
20 try {
21 TestDesktopEditFile window = new TestDesktopEditFile();
22 window.frame.setVisible(true);
23 } catch (Exception e) {
24 e.printStackTrace();
25 }
26 }
27
28 /**
29 * Create the application
30 */
31 public TestDesktopEditFile() {
32 initialize();
33 }
34
35 /**
36 * Initialize the contents of the frame
37 */
38 private void initialize() {
39 try {
40 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
41 } catch (Exception ex) {
42 ex.printStackTrace();
43 }
44 frame = new JFrame();
45 frame.getContentPane().setLayout(null);
46 frame.setBounds(100, 100, 225, 86);
47 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48 final JButton openButton = new JButton();
49 openButton.setBounds(53, 10, 106, 31);
50 openButton.addActionListener(new ActionListener() {
51 public void actionPerformed(ActionEvent e) {
52 if (Desktop.isDesktopSupported()) {
53 Desktop desktop = Desktop.getDesktop();
54 JFileChooser jfc = new JFileChooser();
55 jfc.showOpenDialog(frame);
56 File file = jfc.getSelectedFile();
57 if (file != null) {
58 try {
59 desktop.edit(file);//就这一行的区别
60 } catch (IOException e1) {
61 e1.printStackTrace();
62 }
63 }
64 } else {
65 System.out.println("不支持desktop");
66 }
67 }
68 });
69 openButton.setText("edit");//哦哦哦,还有这一行也不一样,呵呵
70 frame.getContentPane().add(openButton);
71 }
72 }
73
换了windows的风格以后界面顺眼多了
但是有点不理解
为啥不能自动判断操作系统去改变界面风格?
该类型文件没有对应的默认编辑程序的时候,会抛出异常,不能像WINDOWS下面那样弹出一个对话框让你从已知的程序列表中选择一个。
相关文章推荐
- 学习Java6(七)Desktop(3)使用默认程序打开所选文件
- 关于使用Eclipse编辑Android程序出现R文件消失的问题
- [小代码]使用intent用默认的程序打开文件
- Windows Store apps开发[47]使用默认程序打开文件
- IIS起用"默认网站"出现提示"另一程序正在使用此文件,进程无法访问"
- 修改windows的默认使用程序及文件类型与程序关联方法
- Windows7设置默认程序图文教程(使用某一程序打开某一文件类型)
- Delphi使用系统默认程序打开外部文件
- JDK6的新API使用默认关联程序打开文件
- 使用系统默认的程序打开文件
- 转-Java调用本机默认程序打开各种文件(Desktop类)
- VB 调用打开各种文件(自动查找使用默认的程序)
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 使用Qt打开外部文件,调用外部程序(QDesktopServices,QUrl ,QProcess,QProcess)
- 如何使用C#程序给PDF文件添加编辑域
- VS 2005使用map文件查找程序崩溃原因
- 使用windbg分析dmp文件定位程序bug
- 使用 Java 程序写文件时,记得要 flush()
- linux下在命令行中用默认程序打开特定文件如.mp3 .jpg 甚至是网页也可以
- iis无法启动 “另一个程序正在使用此文件,进程无法访问”