为了创建MDI风格的GUI,可以选择JInternalFrame,并将其放入JDesktopPane中。
...//In the constructor of InternalFrameDemo, a JFrame subclass:
desktop = new JDesktopPane();
createFrame(); //Create first window
setContentPane(desktop);
...
//Make dragging a little faster but perhaps uglier.
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
...
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
...//In the constructor of MyInternalFrame, a JInternalFrame subclass:
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
public MyInternalFrame() {
super("Document #" + (++openFrameCount),
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
//...Create the GUI and put it in the window...
//...Then set the window size or call pack...
...
//Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
1、JInternalFrame VS 普通Frame
这两者的使用方式很相似,但InteralFrame并不是Window或Top-level容器,使用InterlFrame时,必须将其放入一个容器,比如JDesktopPane。另外,InterlFrame并不产生Window event,而是产生internal frame event。另外,InternalFrame是被设计成与JDesktopPane协同工作的,因此很多方法,如moveToFront,只有在JDesktopPane中才有效。
2、使用InternalPane的规则
(1)setSize 或 setBounds
(2)setLocation
(3)将组件添加到IntenalPane的contentPane上
(4)InternalPane的对话框应该使用JOptionPane 的showInternalXxxDialog发病高发或 JInternalFrame实现
(5)必须将InternalPane加入一个容器
(6)setVisible
(7)发出Internal Frame event,而不是window event。