1 / 15

Veri Transferi Kes ve Yapıştır

Veri Transferi Kes ve Yapıştır. import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; /** * This program demonstrates how to add string cut-and-paste capabilities * to an application. **/ public class StringCutAndPaste extends Frame implements ActionListener {

india
Télécharger la présentation

Veri Transferi Kes ve Yapıştır

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Veri TransferiKes ve Yapıştır

  2. import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; /** * This program demonstrates how to add string cut-and-paste capabilities * to an application. **/ public class StringCutAndPaste extends Frame implements ActionListener { public static void main(String[] args) { Frame f = new StringCutAndPaste(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.pack(); f.show(); }

  3. TextField field; public StringCutAndPaste() { this.setFont(new Font("SansSerif", Font.PLAIN, 14)); // Set up the Cut button Button cut = new Button("Cut"); cut.addActionListener(this); cut.setActionCommand("cut"); this.add(cut, "West"); // Set up the Paste button Button paste = new Button("Paste"); paste.addActionListener(this); paste.setActionCommand("paste"); this.add(paste, "East"); // Set up the text field that they both operate on field = new TextField(); this.add(field, "North"); }

  4. public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("cut")) cut(); else if (cmd.equals("paste")) paste(); } public void cut() { String s = field.getText(); StringSelection ss = new StringSelection(s); this.getToolkit().getSystemClipboard().setContents(ss, ss); }

  5. public void paste() { Clipboard c = this.getToolkit().getSystemClipboard(); Transferable t = c.getContents(this); try { String s = (String) t.getTransferData(DataFlavor.stringFlavor); field.setText(s); } catch (Exception e) { this.getToolkit().beep(); return; } } }

  6. ScribbleCutAndPaste.java import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; // Clipboard, Transferable, DataFlavor, etc. import java.util.Vector; // To store the scribble in public class ScribbleCutAndPaste extends Frame { public static void main(String[] args) { new ScribbleCutAndPaste(); } protected static int num_windows = 0; public ScribbleCutAndPaste() { super("ScribbleCutAndPaste"); // Create the window num_windows++; // Count it ScribblePanel scribble = new ScribblePanel(this, 400, 300); this.add(scribble, "Center"); MenuBar menubar = new MenuBar(); // Create menubar this.setMenuBar(menubar); // Add it to the frame Menu file = new Menu("File"); // Create a File menu menubar.add(file); // Add to menubar MenuItem n, c, q;

  7. file.add(n = new MenuItem("New Window", new MenuShortcut(KeyEvent.VK_N))); file.add(c = new MenuItem("Close Window",new MenuShortcut(KeyEvent.VK_W))); file.addSeparator(); file.add(q = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q))); n.addActionListener(new ActionListener() { // Open a new window public void actionPerformed(ActionEvent e) { new ScribbleCutAndPaste(); } }); c.addActionListener(new ActionListener() { // Close this window public void actionPerformed(ActionEvent e) { close(); } }); q.addActionListener(new ActionListener() { // Quit the program public void actionPerformed(ActionEvent e) { System.exit(0); } }); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { close(); } }); this.pack(); this.show(); }

  8. void close() { if (--num_windows == 0) System.exit(0); else this.dispose(); } static class ScribblePanel extends Canvas implements ActionListener { protected short last_x, last_y; // Coordinates of last click protected Vector lines = new Vector(256,256); // Store the scribbles protected int width, height; // The preferred size protected PopupMenu popup; // The popup menu protected Frame frame; // The frame we are within public ScribblePanel(Frame frame, int width, int height) { this.frame = frame; this.width = width; this.height = height; this.enableEvents(AWTEvent.MOUSE_EVENT_MASK); this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); String[] labels = new String[] { "Clear", "Cut", "Copy", "Paste" }; String[] commands = new String[] { "clear", "cut", "copy", "paste" };

  9. popup = new PopupMenu(); // Create the menu for(int i = 0; i < labels.length; i++) { MenuItem mi = new MenuItem(labels[i]); // Create a menu item mi.setActionCommand(commands[i]); // Set its action command mi.addActionListener(this); // And its action listener popup.add(mi); // Add item to the popup menu } this.add(popup); } public Dimension getPreferredSize() {return new Dimension(width, height);} public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("clear")) clear(); else if (command.equals("cut")) cut(); else if (command.equals("copy")) copy(); else if (command.equals("paste")) paste(); }

  10. public void paint(Graphics g) { for(int i = 0; i < lines.size(); i++) { Line l = (Line)lines.elementAt(i); g.drawLine(l.x1, l.y1, l.x2, l.y2); } } public void processMouseEvent(MouseEvent e) { if (e.isPopupTrigger()) // If popup trigger, popup.show(this, e.getX(), e.getY()); // Pop up the menu else if (e.getID() == MouseEvent.MOUSE_PRESSED) { // Otherwise last_x = (short)e.getX(); last_y = (short)e.getY(); // Save position } else super.processMouseEvent(e); // Pass other event types on } public void processMouseMotionEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_DRAGGED) { Graphics g = getGraphics(); // Object to draw with g.drawLine(last_x, last_y, e.getX(), e.getY()); // Draw this line lines.addElement(new Line(last_x, last_y, // And save it, too. (short) e.getX(), (short)e.getY())); last_x = (short) e.getX(); // Remember current mouse coordinates last_y = (short) e.getY(); } else super.processMouseMotionEvent(e); // Important! }

  11. void clear() { lines.removeAllElements(); // Throw out the saved scribble repaint(); // And redraw everything. } public static final DataFlavor dataFlavor = new DataFlavor(Vector.class, "ScribbleVectorOfLines"); public void copy() { Clipboard c = this.getToolkit().getSystemClipboard(); SimpleSelection s = new SimpleSelection(lines.clone(), dataFlavor); c.setContents(s, s); } public void cut() { copy(); clear(); }

  12. public void paste() { Clipboard c = this.getToolkit().getSystemClipboard(); // Get clipboard Transferable t = c.getContents(this); // Get its contents if (t == null) { // If there is nothing to paste, beep this.getToolkit().beep(); return; } try { // Ask for clipboard contents to be converted to our data flavor. // This will throw an exception if our flavor is not supported. Vector newlines = (Vector) t.getTransferData(dataFlavor); // Add all those pasted lines to our scribble. for(int i = 0; i < newlines.size(); i++) lines.addElement(newlines.elementAt(i)); // And redraw the whole thing repaint(); } catch (UnsupportedFlavorException e) { this.getToolkit().beep(); // If clipboard has some other type of data } catch (Exception e) { this.getToolkit().beep(); // Or if anything else goes wrong... } }

  13. static class SimpleSelection implements Transferable, ClipboardOwner { protected Object selection; // The data to be transferred protected DataFlavor flavor; // The one data flavor supported public SimpleSelection(Object selection, DataFlavor flavor) { this.selection = selection; // Specify data this.flavor = flavor; // Specify flavor } public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[] { flavor }; } public boolean isDataFlavorSupported(DataFlavor f) { return f.equals(flavor); } public Object getTransferData(DataFlavor f) throws UnsupportedFlavorException { if (f.equals(flavor)) return selection; else throw new UnsupportedFlavorException(f); } public void lostOwnership(Clipboard c, Transferable t) { selection = null; } } static class Line { public short x1, y1, x2, y2; public Line(short x1, short y1, short x2, short y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } } }}

More Related