学习Java6(七)Desktop(4)使用默认程序编辑所选文件
来源:互联网 发布:js的表单事件有哪些? 编辑:程序博客网 时间:2024/05/16 00:35
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
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(4)使用默认程序编辑所选文件
- 学习Java6(七)Desktop(3)使用默认程序打开所选文件
- 学习Java6(七)Desktop(5)打印所选文件
- 学习Java6(七)Desktop(1)调用默认浏览器
- 学习Java6(七)Desktop(2)调用默认邮件客户端
- Flex调用js获取本地所选文件的路径
- [AHK]用热键生成所选文件的副本
- CFileDialog 多选文件
- JS计算input-file所选文件大小(一个或多个)
- CFileDialog多选文件问题
- Java6学习笔记35——Java6程序的发布
- 让CFileDialog可以多选文件
- 获取GetOpenFileName多选文件名
- 获取GetOpenFileName多选文件名
- 打开文件对话框选文件或者目录
- [ATL/WTL]_[初级]_[如何使用GetOpenFileName多选文件-根据文件名长度计算lpstrFile长度]
- 学习Java6(一) WebServices (4)传递Object
- Java6学习笔记32——Java6中向量类的使用
- 在web中使用toolbar的DorpDownList
- 学习Java6(七)Desktop(1)调用默认浏览器
- 学习Java6(七)Desktop(2)调用默认邮件客户端
- 学习Java6(七)Desktop(3)使用默认程序打开所选文件
- 在html中接收地址栏中的参数
- 学习Java6(七)Desktop(4)使用默认程序编辑所选文件
- SQL Server 中的错误15023
- JNDI介绍
- 学习Java6(七)Desktop(5)打印所选文件
- 学习Java6(八)Splash(1)HellpSplash
- 学习Java6(八)Splash(2)控制Splash时间
- 图论和网络爬虫 (Web Crawlers)
- BSS段 数据段 代码段 堆栈
- 学习Java6(八)Splash(3)Splash其它