[ Pobierz całość w formacie PDF ]

}
public void windowActivated(WindowEvent event) {
}
public void windowDeactivated(WindowEvent event) {
}
public void windowOpened(WindowEvent event) {
}
}
class PrintCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.black);
g.setFont(new Font("SansSerif",Font.BOLD,16));
g.drawString("Hallo, World",50,50);
}
}
Das folgende Beispiel beschftigt sich mit verketteten Listen und implementiert dazu zwei
Methoden PrintList() zur Ausgabe der gesamten Liste und AppendList() zum Anhngen
eines Objekts an das Ende der Liste. Die Listenelemente knnen beliebige Objekte vom Typ
ListObject sein.
Da Java ber keine Zeiger im herkmmlichen Sinn verfgt, wird die Verkettung ber Objekte
realisiert. Das ist mglich, da bei der Erzeugung von Objekten Referenzen auf diese Objekte
bergeben werden.
Beispiel 2:
// WM, RZ Uni Os
public class linkedLists {
public ListObject myListObject;
public class ListObject {
public int i;
public ListObject nextListObject;
public ListObject(int i) {
this.i = i;
}
}
public void PrintList(ListObject lo) {
while (lo != null) {
System.out.println(lo.i);
lo = lo.nextListObject;
}
}
public ListObject AppendList(ListObject lo, int n) {
ListObject help;
if (lo == null)
lo = new ListObject(n);
else {
Rechenzentrum der Universitt Osnabrck
60
help = lo;
while (lo.nextListObject != null)
lo = lo.nextListObject;
lo.nextListObject = new ListObject(n);
lo = help;
}
return lo;
}
public void ListTest () {
myListObject = AppendList(myListObject,2);
myListObject = AppendList(myListObject,3);
myListObject = AppendList(myListObject,4);
myListObject = AppendList(myListObject,1);
PrintList(myListObject);
}
public static void main (String argv []) {
linkedLists mylinkedLists = new linkedLists();
mylinkedLists.ListTest();
}
}
Folgendes Beispiel ist aus [2] entnommen und demonstriert in hervorragender Weise die
neuen Mglichkeiten des JDK 1.1. Es handelt sich um eine Applikation mit Menpunkten und
Pop-up Men.
Beispiel 3:
// This example is from the book "Java in a Nutshell, Second Edition".
// Written by David Flanagan. Copyright (c) 1997 O Reilly & Associates.
// You may distribute this source code for non-commercial purposes only.
// You may study, modify, and use this example for any purpose, as long as
// this notice is retained. Note that this example is provided "as is",
// WITHOUT WARRANTY of any kind either expressed or implied.
import java.awt.*; // ScrollPane, PopupMenu, MenuShortcut, etc.
import java.awt.datatransfer.*; // Clipboard, Transferable, DataFlavor, etc.
import java.awt.event.*; // New event model.
import java.io.*; // Object serialization streams.
import java.util.zip.*; // Data compression/decompression streams.
import java.util.Vector; // To store the scribble in.
import java.util.Properties; // To store printing preferences in.
/**
* This class places a Scribble component in a ScrollPane container,
* puts the ScrollPane in a window, and adds a simple pulldown menu system.
* The menu uses menu shortcuts. Events are handled with anonymous classes.
*/
public class ScribbleFrame extends Frame {
/** A very simple main() method for our program. */
public static void main(String[] args) { new ScribbleFrame(); }
/** Remember # of open windows so we can quit when last one is closed */
protected static int num_windows = 0;
/** Create a Frame, Menu, and ScrollPane for the scribble component */
public ScribbleFrame() {
super("ScribbleFrame"); // Create the window.
num_windows++; // Count it.
ScrollPane pane = new ScrollPane(); // Create a ScrollPane.
pane.setSize(300, 300); // Specify its size.
this.add(pane, "Center"); // Add it to the frame.
Scribble scribble;
scribble = new Scribble(this, 500, 500); // Create a bigger scribble area.
pane.add(scribble); // Add it to the ScrollPane.
MenuBar menubar = new MenuBar(); // Create a menubar.
this.setMenuBar(menubar); // Add it to the frame.
Menu file = new Menu("File"); // Create a File menu.
menubar.add(file); // Add to menubar.
Rechenzentrum der Universitt Osnabrck
61
// Create three menu items, with menu shortcuts, and add to the menu.
MenuItem n, c, q;
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(); // Put a separator in the menu
file.add(q = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q)));
// Create and register action listener objects for the three menu items.
n.addActionListener(new ActionListener() { // Open a new window
public void actionPerformed(ActionEvent e) { new ScribbleFrame(); }
});
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); }
});
// Another event listener, this one to handle window close requests.
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { close(); }
});
// Set the window size and pop it up.
this.pack();
this.show();
}
/** Close a window. If this is the last open window, just quit. */
void close() {
if (--num_windows == 0) System.exit(0);
else this.dispose();
}
}
/**
* This class is a custom component that supports scribbling. It also has
* a popup menu that allows the scribble color to be set and provides access
* to printing, cut-and-paste, and file loading and saving facilities.
* Note that it extends Component rather than Canvas, making it "lightweight."
*/
class Scribble extends Component implements ActionListener {
protected short last_x, last_y; // Coordinates of last click.
protected Vector lines = new Vector(256,256); // Store the scribbles.
protected Color current_color = Color.black; // Current drawing color.
protected int width, height; // The preferred size.
protected PopupMenu popup; // The popup menu.
protected Frame frame; // The frame we are within.
/** This constructor requires a Frame and a desired size */
public Scribble(Frame frame, int width, int height) {
this.frame = frame;
this.width = width;
this.height = height;
// We handle scribbling with low-level events, so we must specify
// which events we are interested in.
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
// Create the popup menu using a loop. Note the separation of menu
// "action command" string from menu label. Good for internationalization.
String[] labels = new String[] {
"Clear", "Print", "Save", "Load", "Cut", "Copy", "Paste" };
String[] commands = new String[] {
"clear", "print", "save", "load", "cut", "copy", "paste" };
popup = new PopupMenu(); // Create the menu
for(int i = 0; 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.
}
Menu colors = new Menu("Color"); // Create a submenu.
popup.add(colors); // And add it to the popup.
String[] colornames = new String[] { "Black", "Red", "Green", "Blue"};
for(int i = 0; i
MenuItem mi = new MenuItem(colornames[i]); // Create the submenu items
mi.setActionCommand(colornames[i]); // in the same way.
mi.addActionListener(this);
colors.add(mi);
}
Rechenzentrum der Universitt Osnabrck
62
this.add(popup);
}
// Finally, register the popup menu with the component it appears over
/** Specifies big the component would like to be. It always returns the
* preferred size passed to the Scribble() constructor */
public Dimension getPreferredSize() { return new Dimension(width, height); }
/** This is the ActionListener method invoked by the popup menu items */
public void actionPerformed(ActionEvent event) {
// Get the "action command" of the event, and dispatch based on that.
// This method calls a lot of the interesting methods in this class.
String command = event.getActionCommand();
if (command.equals("clear")) clear(); [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • reyes.pev.pl