Veri Transferi Kes ve Yapıştır 1. import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; /** * This program demonstrates how to add.

Slides:



Advertisements
Similar presentations
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
Advertisements

Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
James Tam An introduction into HCI: Task-Centered System Design An Introduction To Graphical User Interfaces The event-driven model Building a simple.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Io package as Java’s basic I/O system continue’d.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Programming 1 Java Programming II Events, AWT, and Swing.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Lecture 2 Exception handling Advanced Java Programming 1 dr inż. Wojciech Bieniecki
CSE 219 Patterns in Programming More Design Patterns.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Event Handling Mohanraj S AP / IT Angel College of Engg & Tech.,
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Creating a GUI Class An example of class design using inheritance and interfaces.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Lesson 28: More on the GUI button, frame and actions.
JoiMint Graphical objects and their XML configuration files Piotr K. Bartkiewicz, DESY, MST-1.
Lesson 33: Layout management and drawing – Java GUI.
C13b, AWT cont.. Panel Container that acts like a Component Can be added into other components (like Frames, other Panels) private Panel makeScrollBars()
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
Multiple buttons and action calls
CSC 205 Programming II Lecture 5 AWT - I.
INFSY 547: WEB-Based Technologies
GUI Components Part II.
Abstract Window ToolKit (AWT)
AWT Components and Containers
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Menu Bars and Menus.
Presentation transcript:

Veri Transferi Kes ve Yapıştır 1

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(); } 2

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

7 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; ScribbleCutAndPaste.java

8 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(); }

9 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" };

10 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(); }

11 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! }

12 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(); }

13 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... }

14 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; } } } }

15