📄 demo.java
字号:
/* Demo.java -- And example of copy/paste datatransfer Copyright (C) 2005 Free Software Foundation, Inc.This file is part of GNU Classpath examples.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. */package gnu.classpath.examples.datatransfer;import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.io.*;import java.net.URL;import java.util.Arrays;import java.util.Iterator;import java.util.Random;/** * An example how datatransfer works for copying and pasting data to * and from other programs. */class Demo extends Frame implements ActionListener, ItemListener, FlavorListener{ public static void main(String args[]) { new Demo(); } private TextArea text; private Button copyText; private Button pasteText; private ImageComponent image; private Button copyImage; private Button pasteImage; private ObjectComponent object; private Button copyObject; private Button pasteObject; private FilesComponent files; private Button copyFiles; private Button pasteFiles; private FlavorsComponent flavors; private FlavorDetailsComponent details; private Demo() { super("GNU Classpath datatransfer"); /* Add all the different panel to the main window in one row. */ setLayout(new GridLayout(5, 1, 10, 10)); add(createTextPanel()); add(createImagePanel()); add(createObjectPanel()); add(createFilesPanel()); add(createFlavorsPanel()); /* Add listeners for the various buttons and events we are interested in. */ addWindowListener(new WindowAdapter () { public void windowClosing (WindowEvent e) { dispose(); } }); flavors.addItemListener(this); Toolkit t = Toolkit.getDefaultToolkit(); Clipboard c = t.getSystemClipboard(); c.addFlavorListener(this); /* Show time! */ pack(); show(); } /** * The Text Panel will show simple text that can be copied and pasted. */ private Panel createTextPanel() { Panel textPanel = new Panel(); textPanel.setLayout(new BorderLayout()); text = new TextArea("GNU Everywhere!", 2, 80, TextArea.SCROLLBARS_VERTICAL_ONLY); text.setEditable(false); text.setEnabled(true); Panel textButtons = new Panel(); textButtons.setLayout(new FlowLayout()); copyText = new Button("Copy text"); copyText.addActionListener(this); pasteText = new Button("Paste text"); pasteText.addActionListener(this); textButtons.add(copyText); textButtons.add(pasteText); textPanel.add(text, BorderLayout.CENTER); textPanel.add(textButtons, BorderLayout.SOUTH); return textPanel; } /** * The Image Panel shows an image that can be copied to another * program or be replaced by pasting in an image from another * application. */ private Panel createImagePanel() { Panel imagePanel = new Panel(); imagePanel.setLayout(new BorderLayout()); URL imageurl = this.getClass() .getResource("/gnu/classpath/examples/icons/big-fullscreen.png"); Image img = Toolkit.getDefaultToolkit().createImage(imageurl); image = new ImageComponent(img); Panel imageButtons = new Panel(); copyImage = new Button("Copy image"); copyImage.addActionListener(this); pasteImage = new Button("Paste image"); pasteImage.addActionListener(this); imageButtons.add(copyImage); imageButtons.add(pasteImage); imagePanel.add(image, BorderLayout.CENTER); imagePanel.add(imageButtons, BorderLayout.SOUTH); return imagePanel; } /** * The Object Panel holds a simple (Point) object that can be copied * and pasted to another program that supports exchanging serialized * objects. */ private Panel createObjectPanel() { Panel objectPanel = new Panel(); objectPanel.setLayout(new BorderLayout()); Random random = new Random(); int x = (byte) random.nextInt(); int y = (byte) random.nextInt(); object = new ObjectComponent(new Point(x, y)); Panel objectButtons = new Panel(); copyObject = new Button("Copy object"); copyObject.addActionListener(this); pasteObject = new Button("Paste object"); pasteObject.addActionListener(this); objectButtons.add(copyObject); objectButtons.add(pasteObject); objectPanel.add(object, BorderLayout.CENTER); objectPanel.add(objectButtons, BorderLayout.SOUTH); return objectPanel; } /** * The Files Panel shows the files from the current working * directory. They can be copied and pasted between other * applications that support the exchange of file lists. */ private Panel createFilesPanel() { Panel filesPanel = new Panel(); filesPanel.setLayout(new BorderLayout()); files = new FilesComponent(new File(".").listFiles()); Panel filesButtons = new Panel(); copyFiles = new Button("Copy files"); copyFiles.addActionListener(this); pasteFiles = new Button("Paste files"); pasteFiles.addActionListener(this); filesButtons.add(copyFiles); filesButtons.add(pasteFiles); filesPanel.add(files, BorderLayout.CENTER); filesPanel.add(filesButtons, BorderLayout.SOUTH); return filesPanel; } /** * The Flavors Panel shows the different formats (mime-types) that * data on the clipboard is available in. By clicking on a flavor * details about the representation class and object is given. */ private Panel createFlavorsPanel() { Panel flavorsPanel = new Panel(); flavorsPanel.setLayout(new BorderLayout()); Label flavorsHeader = new Label("Flavors on clipboard:"); Toolkit t = Toolkit.getDefaultToolkit(); Clipboard c = t.getSystemClipboard(); DataFlavor[] dataflavors = c.getAvailableDataFlavors(); flavors = new FlavorsComponent(dataflavors); details = new FlavorDetailsComponent(null); flavorsPanel.add(flavorsHeader, BorderLayout.NORTH); flavorsPanel.add(flavors, BorderLayout.CENTER); flavorsPanel.add(details, BorderLayout.SOUTH); return flavorsPanel; } /** * FlavorListener implementation that updates the Flavors Panel * whenever a change in the mime-types available has been detected. */ public void flavorsChanged(FlavorEvent event) { Toolkit t = Toolkit.getDefaultToolkit(); Clipboard c = t.getSystemClipboard(); DataFlavor[] dataflavors = c.getAvailableDataFlavors(); flavors.setFlavors(dataflavors); details.setDataFlavor(null); } /** * ItemChangeListener implementation that updates the flavor details * whenever the user selects a different representation of the data * available on the clipboard. */ public void itemStateChanged(ItemEvent evt) { DataFlavor df = null; String s = flavors.getSelectedItem(); if (s != null) { try { df = new DataFlavor(s); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); } } details.setDataFlavor(df); } /** * ActionListener implementations that will copy or past data * to/from the clipboard when the user requests that for the text, * image, object of file component. */ public void actionPerformed (ActionEvent evt) { Button b = (Button) evt.getSource(); Toolkit t = Toolkit.getDefaultToolkit(); Clipboard c = t.getSystemClipboard(); if (b == copyText) c.setContents(new StringSelection(text.getText()), null); if (b == pasteText) { String s = null; try { s = (String) c.getData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException dfnse) { } catch (IOException ioe) { } catch (ClassCastException cce) { } if (s == null) t.beep(); else text.setText(s); } if (b == copyImage) c.setContents(new ImageSelection(image.getImage()), null); if (b == pasteImage) { Image i = null; try { i = (Image) c.getData(DataFlavor.imageFlavor); } catch (UnsupportedFlavorException dfnse) { } catch (IOException ioe) { } catch (ClassCastException cce) { } if (i == null) t.beep(); else image.setImage(i); } if (b == copyObject) c.setContents(new ObjectSelection(object.getObject()), null); if (b == pasteObject) { Serializable o = null; try { o = (Serializable) c.getData(ObjectSelection.objFlavor); } catch (UnsupportedFlavorException dfnse) { } catch (IOException ioe) { } catch (ClassCastException cce) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -