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

📄 textbatchprintingdemo.java

📁 java tutorial.sun公司官方出品。java入门书籍。最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * *   - Redistributions of source code must retain the above copyright *     notice, this list of conditions and the following disclaimer. * *   - Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution. * *   - Neither the name of Sun Microsystems nor the names of its *     contributors may be used to endorse or promote products derived *     from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package misc;/* * TextBatchPrintingDemo.java requires the following files: * index.html * chapter1.html * chapter2.html * chapter3.html * chapter4.html * chapter5.html * chapter6.html * chapter7.html * chapter8.html * chapter9.html * chapter10.html */import java.awt.Container;import java.awt.Dimension;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.KeyEvent;import java.awt.print.PrinterException;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import java.util.Map;import javax.print.PrintService;import javax.print.PrintServiceLookup;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.BoxLayout;import javax.swing.DefaultListModel;import javax.swing.JEditorPane;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.KeyStroke;import javax.swing.ListModel;import javax.swing.ListSelectionModel;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkEvent.EventType;import javax.swing.event.HyperlinkListener;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.text.Document;/** * This is a simple web browser that allows to store visited pages on the  * "print list" and then prints all selected pages at once, in batch mode. */public class TextBatchPrintingDemo                        implements HyperlinkListener, ListSelectionListener {        /**     * A class representing pages in the page cache and in the print list.     */    static class PageItem extends JEditorPane {        String title;                /**         * If the loaded document has a title, use it; otherwise use the         * string representation of the document URL.         */        public String toString() {            if (title == null) {                String s = (String) getDocument().getProperty(                                                        Document.TitleProperty);                title = (s == null ? getPage().toString() : s);            }            return title;        }    }    /** Default start page, could be changed from the command line.  */    static String defaultPage = "index.html";        /** Default message area contents.  */    static String defaultMessage = "Select: Alt-A  Print: Alt-P  Quit: Alt-Q";        /** Default print service.  */    static PrintService printService =                                PrintServiceLookup.lookupDefaultPrintService();        /** The currently displayed page item.  */    PageItem pageItem;        /** Start page for the page browser.  */    URL homePage;        /** Cache for the visited page items indexed by page URL.  */    Map<URL, PageItem> pageCache = new HashMap<URL, PageItem>();        /** Pages currently selected for printing (aka "print list").  */    JList selectedPages;        /** An area holding the informational and error messages.  */    JLabel messageArea;    /**     * The demo has three logical parts.  The first is the batch printing     * method, the second is UI controller routines and the third is the UI     * initialization and other (non-UI) setup code.     */    /* Part 1: Batch printing.  */    /**     * Print all selected pages in separate threads, one thread per page.     */    void printSelectedPages() {        DefaultListModel pages = (DefaultListModel) selectedPages.getModel();        int n = pages.getSize();        if (n < 1) {            messageArea.setText("No pages selected");            return;        }        if (printService == null) {            messageArea.setText("No print service");            return;        }        for (int i = 0; i < n; i++) {            final PageItem item = (PageItem) pages.getElementAt(i);            // This method is called from EDT.  Printing is a time-consuming            // task, so it should be done outside EDT, in a separate thread.            Runnable printTask = new Runnable() {                public void run() {                    try {                        item.print(                                // Two "false" args mean "no print dialog" and                                // "non-interactive" (ie, batch-mode printing).                                null, null, false, printService, null, false);                    } catch (PrinterException pe) {                        JOptionPane.showMessageDialog(null,                                "Error printing " + item.getPage() + "\n" + pe,                                "Print Error", JOptionPane.WARNING_MESSAGE);                    }                }            };            new Thread(printTask).start();        }        pages.removeAllElements();        messageArea.setText(n + (n > 1 ? " pages" : " page") + " printed");    }    /* Part 2: UI controller.  */    /**     * Called when something is happened on a hyperlink in the page browser.     * This is the {@code HyperlinkListener} method.     */    public void hyperlinkUpdate(HyperlinkEvent e) {        URL url = e.getURL();        EventType type = e.getEventType();                if (type == EventType.ENTERED) {            messageArea.setText("Go to " + url);        } else if (type == EventType.EXITED) {            messageArea.setText(defaultMessage);        } else if (type == EventType.ACTIVATED) {            setPage(url);            messageArea.setText(defaultMessage);        }    }    /**     * Called when the print list selection state is changed.  This is the      * {@code ListSelectionListener} method.     */    public void valueChanged(ListSelectionEvent e) {        if (!e.getValueIsAdjusting()) {            int index = ((JList) e.getSource()).getSelectedIndex();            if (index >= 0) {                // Load the currently selected URL into the page browser.                PageItem item =                        (PageItem) selectedPages.getModel().getElementAt(index);

⌨️ 快捷键说明

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