?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);//就這一行的區(qū)別
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的風(fēng)格以后界面順眼多了
但是有點(diǎn)不理解
為啥不能自動(dòng)判斷操作系統(tǒng)去改變界面風(fēng)格?
該類型文件沒有對(duì)應(yīng)的默認(rèn)編輯程序的時(shí)候,會(huì)拋出異常,不能像WINDOWS下面那樣彈出一個(gè)對(duì)話框讓你從已知的程序列表中選擇一個(gè)。
posted on 2007-01-25 15:36
交口稱贊 閱讀(1427)
評(píng)論(0) 編輯 收藏 所屬分類:
Java6