ScribbleFramepublic class ScribbleFrame extends Frame 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. |
Fields Summary |
---|
protected static int | num_windowsRemember # of open windows so we can quit when last one is closed |
Constructors Summary |
---|
public ScribbleFrame()Create a Frame, Menu, and ScrollPane for the scribble component
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.
// 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();
|
Methods Summary |
---|
void | close()Close a window. If this is the last open window, just quit.
if (--num_windows == 0) System.exit(0);
else this.dispose();
| public static void | main(java.lang.String[] args)A very simple main() method for our program. new ScribbleFrame();
|
|