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 TestDesktopPrintFile{
12 private JFrameframe;
13
14 /**
15 *Launchtheapplication
16 *
17 * @param args
18 */
19 public static void main(Stringargs[]){
20 try {
21 TestDesktopPrintFilewindow = new TestDesktopPrintFile();
22 window.frame.setVisible( true );
23 } catch (Exceptione){
24 e.printStackTrace();
25 }
26 }
27
28 /**
29 *Createtheapplication
30 */
31 public TestDesktopPrintFile(){
32 initialize();
33 }
34
35 /**
36 *Initializethecontentsoftheframe
37 */
38 private void initialize(){
39 try {
40 UIManager.setLookAndFeel( " com.sun.java.swing.plaf.windows.WindowsLookAndFeel " );
41 } catch (Exceptionex){
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 JButtonopenButton = new JButton();
49 openButton.setBounds( 53 , 10 , 106 , 31 );
50 openButton.addActionListener( new ActionListener(){
51 public void actionPerformed(ActionEvente){
52 if (Desktop.isDesktopSupported()){
53 Desktopdesktop = Desktop.getDesktop();
54 JFileChooserjfc = new JFileChooser();
55 jfc.showOpenDialog(frame);
56 Filefile = jfc.getSelectedFile();
57 if (file != null ){
58 try {
59 desktop.print(file);
60 } catch (IOExceptione1){
61 e1.printStackTrace();
62 }
63 }
64 } else {
65 System.out.println( " 不支持desktop " );
66 }
67 }
68 });
69 openButton.setText( " print " );
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 TestDesktopPrintFile{
12 private JFrameframe;
13
14 /**
15 *Launchtheapplication
16 *
17 * @param args
18 */
19 public static void main(Stringargs[]){
20 try {
21 TestDesktopPrintFilewindow = new TestDesktopPrintFile();
22 window.frame.setVisible( true );
23 } catch (Exceptione){
24 e.printStackTrace();
25 }
26 }
27
28 /**
29 *Createtheapplication
30 */
31 public TestDesktopPrintFile(){
32 initialize();
33 }
34
35 /**
36 *Initializethecontentsoftheframe
37 */
38 private void initialize(){
39 try {
40 UIManager.setLookAndFeel( " com.sun.java.swing.plaf.windows.WindowsLookAndFeel " );
41 } catch (Exceptionex){
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 JButtonopenButton = new JButton();
49 openButton.setBounds( 53 , 10 , 106 , 31 );
50 openButton.addActionListener( new ActionListener(){
51 public void actionPerformed(ActionEvente){
52 if (Desktop.isDesktopSupported()){
53 Desktopdesktop = Desktop.getDesktop();
54 JFileChooserjfc = new JFileChooser();
55 jfc.showOpenDialog(frame);
56 Filefile = jfc.getSelectedFile();
57 if (file != null ){
58 try {
59 desktop.print(file);
60 } catch (IOExceptione1){
61 e1.printStackTrace();
62 }
63 }
64 } else {
65 System.out.println( " 不支持desktop " );
66 }
67 }
68 });
69 openButton.setText( " print " );
70 frame.getContentPane().add(openButton);
71 }
72 }
73
打印之前会先打开默认编辑器。。。。。。。。。。不知道为啥不是直接打印
没打印机的人飘过