?? textbatchprintingdemo.java
字號:
URL page = item.getPage(); if (! page.equals(pageItem.getPage())) { setPage(page); } } } } /** * Load URL into the current page item. If the cached page item exists for * the given URL, use it; otherwise create new page item. */ void setPage(URL url) { PageItem item = pageCache.get(url); if (item == null) { item = createPageItem(url); pageCache.put(url, item); } if (pageItem != null) { Container p = pageItem.getParent(); if (p != null) { p.remove(pageItem); p.add(item); } } pageItem = item; updateSelectedPages(); } /** * Synchronize the selection in the print list with the current page item. * If the current page item isn't in the print list, clear selection. */ void updateSelectedPages() { ListModel pages = selectedPages.getModel(); int n = pages.getSize(); if (n > 0) { URL page = pageItem.getPage(); int index = selectedPages.getSelectedIndex(); if (index >= 0) { PageItem selected = (PageItem) pages.getElementAt(index); if (page.equals(selected.getPage())) { // Currently displayed page is selected in the print list. return; } } for (int i = 0; i < n; i++) { PageItem pi = (PageItem) pages.getElementAt(i); if (page.equals(pi.getPage())) { // Currently displayed page is in the print list, select it. selectedPages.setSelectedIndex(i); return; } } // Currently displayed page is not in the print list. selectedPages.clearSelection(); } } /* Part 3: Initialization and setup. */ /** * Create and return a page item initialized with the given URL. */ PageItem createPageItem(URL url) { PageItem item = new PageItem(); item.setPreferredSize(new Dimension(800, 600)); item.setEditable(false); item.addHyperlinkListener(this); try { item.setPage(url); } catch (IOException ioe) { messageArea.setText("Error loading " + url + ": " + ioe); } return item; } /** * Create and return a menu item with the specified action, mnemonics and * keyboad accelerator. */ JMenuItem createMenuItem(Action action, int mnemonics, KeyStroke accel) { JMenuItem item = new JMenuItem(action); item.setMnemonic(mnemonics); item.setAccelerator(accel); return item; } /** * Create and display the main application frame. */ void createAndShowGUI() { messageArea = new JLabel(defaultMessage); selectedPages = new JList(new DefaultListModel()); selectedPages.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); selectedPages.addListSelectionListener(this); setPage(homePage); JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(pageItem), new JScrollPane(selectedPages)); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); /** Menu item and keyboard shortcuts for the "add page" command. */ fileMenu.add(createMenuItem( new AbstractAction("Add Page") { public void actionPerformed(ActionEvent e) { DefaultListModel pages = (DefaultListModel) selectedPages.getModel(); pages.addElement(pageItem); selectedPages.setSelectedIndex(pages.getSize() - 1); } }, KeyEvent.VK_A, KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK))); /** Menu item and keyboard shortcuts for the "print selected" command.*/ fileMenu.add(createMenuItem( new AbstractAction("Print Selected") { public void actionPerformed(ActionEvent e) { printSelectedPages(); } }, KeyEvent.VK_P, KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK))); /** Menu item and keyboard shortcuts for the "clear selected" command.*/ fileMenu.add(createMenuItem( new AbstractAction("Clear Selected") { public void actionPerformed(ActionEvent e) { DefaultListModel pages = (DefaultListModel) selectedPages.getModel(); pages.removeAllElements(); } }, KeyEvent.VK_C, KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK))); fileMenu.addSeparator(); /** Menu item and keyboard shortcuts for the "home page" command. */ fileMenu.add(createMenuItem( new AbstractAction("Home Page") { public void actionPerformed(ActionEvent e) { setPage(homePage); } }, KeyEvent.VK_H, KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK))); /** Menu item and keyboard shortcuts for the "quit" command. */ fileMenu.add(createMenuItem( new AbstractAction("Quit") { public void actionPerformed(ActionEvent e) { for (Window w : Window.getWindows()) { w.dispose(); } } }, KeyEvent.VK_A, KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK))); JMenuBar menuBar = new JMenuBar(); menuBar.add(fileMenu); JPanel contentPane = new JPanel(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.add(pane); contentPane.add(messageArea); JFrame frame = new JFrame("Text Batch Printing Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(menuBar); frame.setContentPane(contentPane); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); if (printService == null) { // Actual printing is not possible, issue a warning message. JOptionPane.showMessageDialog(frame, "No default print service", "Print Service Alert", JOptionPane.WARNING_MESSAGE); } } public static void main(String[] args) { final TextBatchPrintingDemo demo = new TextBatchPrintingDemo(); demo.homePage = demo.getClass().getResource(defaultPage); // Custom home page can be specified on the command line. if (args.length > 0) { String pageName = args[0]; try { URL url = new URL(pageName); demo.homePage = url; } catch (MalformedURLException e) { System.out.println("Error parsing " + pageName + ": " + e); // Home page is unchanged from the default value. } } //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { //Turn off metal's use of bold fonts UIManager.put("swing.boldMetal", Boolean.FALSE); demo.createAndShowGUI(); } }); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -