⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
字号:
/* Demo.java -- Simple Java Print Service Demo   Copyright (C) 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.classpath.examples.print;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.HashSet;import java.util.Iterator;import javax.print.DocFlavor;import javax.print.DocPrintJob;import javax.print.PrintException;import javax.print.PrintService;import javax.print.PrintServiceLookup;import javax.print.ServiceUI;import javax.print.SimpleDoc;import javax.print.attribute.Attribute;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTabbedPane;import javax.swing.JTextField;/** * A simple demo showing the use of the Java Print Service API. * @author Wolfgang Baer (WBaer@gmx.de) */public class Demo extends JFrame   implements ActionListener{  // The discovered print services  private static PrintService[] services;    // variables for the PrintPanel demo  private HashPrintRequestAttributeSet atts;  private PrintService dialogSelectedService;  private JTextField dialogSelectedService_Tf;  private JList dialogSelectedServiceAtts;  private JComboBox dialogSelectedServicedocFormat;  private JTextField selectedFileTf;  private File selectedFile;    // variables for the PrintServicePanel demo  private JComboBox serviceBox;  private JList docFormat;  private JList attCategories;    static  {    // lookup all services without any constraints    services = PrintServiceLookup.lookupPrintServices(null, null);     }    /**   * Constructs the Print Demo   * @param title - the demo title.   */  public Demo(String title)   {    super(title);    JPanel content = new JPanel(new BorderLayout());        JTabbedPane tabbed = new JTabbedPane();    tabbed.addTab("Discover print services", createPrintServicePanel());    tabbed.addTab("Print a file", createPrintPanel());        JPanel closePanel = new JPanel();    JButton closeButton = new JButton("Close");    closeButton.setActionCommand("CLOSE");    closeButton.addActionListener(this);    closePanel.add(closeButton);        content.add(tabbed, BorderLayout.CENTER);    content.add(closePanel, BorderLayout.SOUTH);    getContentPane().add(content);  }      private JPanel createPrintServicePanel()   {       JPanel panel = new JPanel(new GridBagLayout());    GridBagConstraints c = new GridBagConstraints();        c.insets = new Insets(5,5,5,5);    c.gridx = 0;    c.gridy = 0;       c.anchor = GridBagConstraints.WEST;    c.fill = GridBagConstraints.HORIZONTAL;    JLabel serviceBoxLb = new JLabel("Available print services: ");    panel.add(serviceBoxLb, c);         c.gridx = 1;    c.gridy = 0;    serviceBox = new JComboBox(services);    serviceBox.setActionCommand("SERVICE");    serviceBox.addActionListener(this);    panel.add(serviceBox, c);         c.gridx = 0;    c.gridy = 1;    JLabel docFormatLb = new JLabel("Supported DocFormat: ");    panel.add(docFormatLb, c);         c.gridx = 1;    c.gridy = 1;    docFormat = new JList(services[0].getSupportedDocFlavors());    docFormat.setVisibleRowCount(3);    JScrollPane scrollPane = new JScrollPane(docFormat);    panel.add(scrollPane, c);         c.gridx = 0;    c.gridy = 2;    JLabel categoriesLb = new JLabel("Supported Attribute categories: ");    panel.add(categoriesLb, c);         c.gridx = 1;    c.gridy = 2;    attCategories = new JList(services[0].getSupportedAttributeCategories());    attCategories.setVisibleRowCount(3);    JScrollPane scrollPane2 = new JScrollPane(attCategories);        panel.add(scrollPane2, c);         return panel;  }    private JPanel createPrintPanel()   {    JPanel panel = new JPanel(new GridBagLayout());        GridBagConstraints c = new GridBagConstraints();        c.insets = new Insets(5,5,5,5);    c.gridx = 0;    c.gridy = 0;    c.gridwidth = 2;        JButton serviceBtn = new JButton("Show print dialog ...");    serviceBtn.addActionListener(this);    panel.add(serviceBtn, c);         c.gridx = 0;    c.gridy = 1;    c.gridwidth = 1;    c.anchor = GridBagConstraints.WEST;    c.fill = GridBagConstraints.HORIZONTAL;    JLabel selectedLb = new JLabel("Selected print service: ");    panel.add(selectedLb, c);        c.gridx = 1;    c.gridy = 1;    dialogSelectedService_Tf = new JTextField(25);    panel.add(dialogSelectedService_Tf, c);        c.gridx = 0;    c.gridy = 2;    JLabel selectedAttsLb = new JLabel("Selected Attributes: ");    panel.add(selectedAttsLb, c);        c.gridx = 1;    c.gridy = 2;    c.weighty = 1.5;    c.fill = GridBagConstraints.BOTH;    dialogSelectedServiceAtts = new JList();    dialogSelectedServiceAtts.setVisibleRowCount(3);        JScrollPane scrollPane = new JScrollPane(dialogSelectedServiceAtts);    panel.add(scrollPane, c);        c.gridx = 0;    c.gridy = 3;    c.fill = GridBagConstraints.HORIZONTAL;    JLabel fileLb = new JLabel("File to print: ");    panel.add(fileLb, c);        c.gridx = 1;    c.gridy = 3;    selectedFileTf = new JTextField(25);    panel.add(selectedFileTf, c);        c.gridx = 2;    c.gridy = 3;    c.fill = GridBagConstraints.NONE;    JButton fileBt = new JButton("Choose file");    fileBt.addActionListener(this);    panel.add(fileBt, c);        c.gridx = 0;    c.gridy = 4;    c.fill = GridBagConstraints.HORIZONTAL;    JLabel docFormatLb = new JLabel("Document format of file: ");    panel.add(docFormatLb, c);        c.gridx = 1;    c.gridy = 4;    dialogSelectedServicedocFormat = new JComboBox();    panel.add(dialogSelectedServicedocFormat, c);        c.gridx = 0;    c.gridy = 5;    c.gridwidth = 2;    c.anchor = GridBagConstraints.CENTER;    c.fill = GridBagConstraints.NONE;    JButton printBt = new JButton("Print");    printBt.setActionCommand("PRINT");    printBt.addActionListener(this);    panel.add(printBt, c);        return panel;  }  /**   * Simple action control - only one listener   * @param event   */  public void actionPerformed(ActionEvent event)  {    if (event.getActionCommand().equals("CLOSE"))      {        System.exit(0);      }     else if (event.getActionCommand().equals("Choose file"))      {        JFileChooser chooser = new JFileChooser();        chooser.showOpenDialog(this);                selectedFile = chooser.getSelectedFile();                if (selectedFile != null)          selectedFileTf.setText(selectedFile.getName());        else          selectedFileTf.setText("None selected");      }    else if (event.getActionCommand().equals("Show print dialog ..."))      {        atts = new HashPrintRequestAttributeSet();                dialogSelectedService = ServiceUI.printDialog(null, 50, 50, services,                                                      null, null, atts);                if (dialogSelectedService != null)          {            dialogSelectedService_Tf.setText(dialogSelectedService.getName());                        // we do not want to have the class representation in the dialog            // as we later always use an InputStream to open the file selected                        // use set to remove duplicates            DocFlavor[] docflavors = dialogSelectedService.getSupportedDocFlavors();            HashSet set = new HashSet();            for (int i=0; i < docflavors.length; i++)              {                String charset = docflavors[i].getParameter("charset");                String mimetype = docflavors[i].getMediaType() + "/" + docflavors[i].getMediaSubtype();                if (charset != null)                  mimetype += "; charset=" + charset;                set.add(mimetype);              }                        dialogSelectedServicedocFormat.removeAllItems();                   for (Iterator it = set.iterator(); it.hasNext(); )              dialogSelectedServicedocFormat.addItem(it.next());          }        else          dialogSelectedService_Tf.setText("None selected");        Attribute[] attsArray = atts.toArray();        String[] attsSTr = new String[attsArray.length];        for (int i = 0; i < attsSTr.length; i++)          attsSTr[i] = attsArray[i].getName() + " - " + attsArray[i].toString();        dialogSelectedServiceAtts.setListData(attsSTr);                    validate();                }     else if (event.getActionCommand().equals("PRINT"))      {          if (selectedFile != null && dialogSelectedService != null)           {            DocPrintJob job = dialogSelectedService.createPrintJob();                        // choose correct docflavor            String mimetype = (String) dialogSelectedServicedocFormat.getSelectedItem();                                    DocFlavor flavor = null;            if (mimetype.equals(DocFlavor.INPUT_STREAM.GIF.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.GIF;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.AUTOSENSE.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.JPEG.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.JPEG;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.PCL.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.PCL;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.PDF.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.PDF;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.PNG.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.PNG;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.POSTSCRIPT.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.TEXT_HTML_HOST.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.TEXT_HTML_HOST;            else if (mimetype.equals(DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST.getMimeType()))              flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;            else              flavor = new DocFlavor(mimetype, "java.io.InputStream");                        try              {                SimpleDoc doc = new SimpleDoc(new FileInputStream(selectedFile), flavor, null);                job.print(doc, atts);              }            catch (FileNotFoundException e)              {                JOptionPane.showMessageDialog(this, "The file was not found.");                e.printStackTrace();              }            catch (PrintException e)              {                JOptionPane.showMessageDialog(this, e, "PrintException", JOptionPane.ERROR_MESSAGE);                e.printStackTrace();              }                     }        else          JOptionPane.showMessageDialog(this, "Please select a file to print using the FileChooser", "No file selected", JOptionPane.INFORMATION_MESSAGE);      }    else if (event.getActionCommand().equals("SERVICE"))      {  // A new service was selected        PrintService selected = (PrintService) serviceBox.getSelectedItem();                DocFlavor[] flavors = selected.getSupportedDocFlavors();        docFormat.setListData(flavors);        attCategories.setListData(selected.getSupportedAttributeCategories());      }  }  /**   * Main method.   * @param args - nothing defined.   */  public static void main(String[] args)  {    Demo app = new Demo("GNU Classpath printing demo");    app.pack();    app.setVisible(true);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -