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 JFrameframe;
12
13 /**
14 *Launchtheapplication
15 *
16 * @param args
17 */
18 public static void main(Stringargs[]){
19 try {
20 TestDesktopOpenFilewindow = new TestDesktopOpenFile();
21 window.frame.setVisible( true );
22 } catch (Exceptione){
23 e.printStackTrace();
24 }
25 }
26
27 /**
28 *Createtheapplication
29 */
30 public TestDesktopOpenFile(){
31 initialize();
32 }
33
34 /**
35 *Initializethecontentsoftheframe
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 JButtonopenButton = new JButton();
43 openButton.setBounds( 53 , 10 , 106 , 31 );
44 openButton.addActionListener( new ActionListener(){
45 public void actionPerformed(ActionEvente){
46 if (Desktop.isDesktopSupported()){
47 Desktopdesktop = Desktop.getDesktop();
48 JFileChooserjfc = new JFileChooser();
49 jfc.showOpenDialog(frame);
50 Filefile = jfc.getSelectedFile();
51 if (file != null ){
52 try {
53 desktop.open(file);
54 } catch (IOExceptione1){
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 JFrameframe;
12
13 /**
14 *Launchtheapplication
15 *
16 * @param args
17 */
18 public static void main(Stringargs[]){
19 try {
20 TestDesktopOpenFilewindow = new TestDesktopOpenFile();
21 window.frame.setVisible( true );
22 } catch (Exceptione){
23 e.printStackTrace();
24 }
25 }
26
27 /**
28 *Createtheapplication
29 */
30 public TestDesktopOpenFile(){
31 initialize();
32 }
33
34 /**
35 *Initializethecontentsoftheframe
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 JButtonopenButton = new JButton();
43 openButton.setBounds( 53 , 10 , 106 , 31 );
44 openButton.addActionListener( new ActionListener(){
45 public void actionPerformed(ActionEvente){
46 if (Desktop.isDesktopSupported()){
47 Desktopdesktop = Desktop.getDesktop();
48 JFileChooserjfc = new JFileChooser();
49 jfc.showOpenDialog(frame);
50 Filefile = jfc.getSelectedFile();
51 if (file != null ){
52 try {
53 desktop.open(file);
54 } catch (IOExceptione1){
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
唉,界面丑到我不忍心说了。。。。。。。。。
还是SWT好看一点