Java Reference
In-Depth Information
A
multiple-document interface
(
MDI
) is a main window (called the
parent window
) con-
taining other windows (called
child windows
) and is often used to manage several open doc-
uments. For example, many e-mail programs allow you to have several windows open at the
same time, so you can compose or read multiple e-mail messages simultaneously. Similarly,
many word processors allow the user to open multiple documents in separate windows with-
in a main window, making it possible to switch between them without having to close one
to open another. The application in Figs. 22.11-22.12 demonstrates Swing's
JDesktopPane
and
JInternalFrame
classes for implementing multiple-document interfaces.
1
// Fig. 22.11: DesktopFrame.java
2
// Demonstrating JDesktopPane.
3
import
java.awt.BorderLayout;
4
import
java.awt.Dimension;
5
import
java.awt.Graphics;
6
import
java.awt.event.ActionListener;
7
import
java.awt.event.ActionEvent;
8
import
java.util.Random;
9
import
javax.swing.JFrame;
10
11
import
javax.swing.JDesktopPane;
import
javax.swing.JMenuBar;
12
import
javax.swing.JMenu;
13
import
javax.swing.JMenuItem;
14
15
import
javax.swing.JInternalFrame;
import
javax.swing.JPanel;
16
import
javax.swing.ImageIcon;
17
18
public
class
DesktopFrame
extends
JFrame
19
{
20
private final
JDesktopPane theDesktop;
21
22
// set up GUI
23
public
DesktopFrame()
24
{
25
super
(
"Using a JDesktopPane"
);
26
27
JMenuBar bar =
new
JMenuBar();
28
JMenu addMenu =
new
JMenu(
"Add"
);
29
JMenuItem newFrame =
new
JMenuItem(
"Internal Frame"
);
30
31
addMenu.add(newFrame);
// add new frame item to Add menu
32
bar.add(addMenu);
// add Add menu to menu bar
33
setJMenuBar(bar);
// set menu bar for this application
34
35
theDesktop =
new
JDesktopPane();
add(theDesktop);
// add desktop pane to frame
36
37
38
// set up listener for newFrame menu item
39
newFrame.addActionListener(
Fig. 22.11
|
Multiple-document interface. (Part 1 of 2.)