我们都知道,MDI(Multiple Document Interface)即多文档界面。使用MDI窗体时,将在一个父窗体内建立工作区,父窗体能够令一个以上的子窗体限制于其中活动及操作。在office系列及VS/VS.Net等很多软件中都使用了MDI的表现形式。
而遗憾的是,虽然很多编程语言都提供了显著的MDI属性,但Java却算是个例外,基本上只能通过JDesktopPane结合JInternalFrame进行实现,而且局限性也比较多。
其实,利用Swing完成MDI,还有更简单的捷径可循。
下面,我给出一个简单的例子:
package
org.loon.test;
![](http://img.e-com-net.com/image/product/0fb6e2b4c32f479da39f54e5c8b4890d.gif)
/** */
/**
* <p>Title: LoonFramework</p>
* <p>Description:</p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: LoonFramework</p>
* @author chenpeng
* @email:[email protected]
* @version 0.1
*/
import
javax.imageio.ImageIO;
import
javax.swing.BorderFactory;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JLayeredPane;
import
javax.swing.JPanel;
import
javax.swing.JTextArea;
import
javax.swing.SwingConstants;
import
javax.swing.WindowConstants;
import
javax.swing.border.Border;
![](http://img.e-com-net.com/image/product/ff68a22582454f78825e64e5346ca758.gif)
import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.EventQueue;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.Image;
import
java.awt.Insets;
import
java.awt.Point;
import
java.awt.event.MouseEvent;
import
java.awt.event.MouseListener;
import
java.awt.event.MouseMotionListener;
import
java.io.IOException;
![](http://img.e-com-net.com/image/product/ff68a22582454f78825e64e5346ca758.gif)
![](http://img.e-com-net.com/image/product/ff68a22582454f78825e64e5346ca758.gif)
![](http://img.e-com-net.com/image/product/ff68a22582454f78825e64e5346ca758.gif)
![](http://img.e-com-net.com/image/product/0fb6e2b4c32f479da39f54e5c8b4890d.gif)
public
class
JavaMDI
extends
JPanel
...
{
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
/** *//**
*
*/
private static final long serialVersionUID = 1L;
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
private static final int BACKLAYER = 1;
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
Font FONT = new Font("宋体", Font.PLAIN, 12);
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
private final BackImagePane layerPane;
//子窗体背景色
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
private int[] colors = ...{ 0xdddddd, 0xaaaaff, 0xffaaaa, 0xaaffaa, 0xffffaa, 0xffaaff, 0xaaffff , 0xddddff};
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
private Color getColor(int i, float f) ...{
int b = (int)((i & 0xff) * f);
i = i >> 8;
int g = (int)((i & 0xff) * f);
i = i >> 8;
int r = (int)((i & 0xff) * f);
return new Color(r,g,b);
}
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public JavaMDI() ...{
super(new BorderLayout());
Image image;
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
try ...{
image = ImageIO.read(getClass().getResource("javamdi.jpg"));
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
} catch (IOException e) ...{
image=null;
}
layerPane = new BackImagePane();
layerPane.setImage(image);
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
//随机生成个子面板,作为内部窗体,实际使用时替换JPanel内部容器即可。
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
for (int i=0; i<colors.length; i++) ...{
JPanel p = createChildPanel(i);
p.setLocation(i*80 + 20, i*50 + 15);
layerPane.add(p, BACKLAYER);
}
add(layerPane, BorderLayout.CENTER);
}
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
/** *//**
* 创建子面板,作为在内部移动的窗体
* @param i
* @return
*/
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
private JPanel createChildPanel(int i) ...{
//使用html标记设定颜色
String html = "<html><font color=#333333> 子窗体ID "+ i +"</font></html>";
JLabel label = new JLabel(html);
label.setFont(FONT);
label.setOpaque(true);
label.setHorizontalAlignment(SwingConstants.CENTER);
//设定背景色
label.setBackground(getColor(colors[i], 0.85f));
//设定边距
Border border1 = BorderFactory.createEmptyBorder(4, 4, 4, 4);;
label.setBorder(border1);
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
JTextArea text = new JTextArea();
text.setBackground( new Color(colors[i]));
text.setMargin(new Insets(4,4,4,4));
text.setLineWrap(true);
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
JPanel p = new JPanel();
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
Color col = getColor(colors[i], 0.5f);
Border border = BorderFactory.createLineBorder(col, 1);
p.setBorder(border);
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
//移动监听
DragMouseListener li = new DragMouseListener(p);
p.addMouseListener(li);
p.addMouseMotionListener(li);
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
p.setLayout( new BorderLayout());
p.add(label, BorderLayout.NORTH);
p.add(text, BorderLayout.CENTER);
//子窗体大小
p.setSize( new Dimension(200, 150));
return p;
}
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
/** *//**
* 子窗体拖拽监听
* @author chenpeng
*
*/
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
class DragMouseListener implements MouseListener, MouseMotionListener ...{
Point origin;
JPanel panel;
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
DragMouseListener(JPanel p) ...{
panel = p;
}
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void mousePressed(MouseEvent e) ...{
origin = new Point( e.getX(), e.getY());
//移动
layerPane.moveToFront(panel);
}
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void mouseDragged(MouseEvent e) ...{
if (origin == null) return;
int dx = e.getX() - origin.x;
int dy = e.getY() - origin.y;
Point p = panel.getLocation();
panel.setLocation( p.x + dx, p.y + dy);
}
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void mouseClicked(MouseEvent e) ...{}
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void mouseEntered(MouseEvent e) ...{}
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void mouseExited(MouseEvent e) ...{}
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void mouseReleased(MouseEvent e) ...{}
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void mouseMoved(MouseEvent e) ...{}
}
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
//用分层面板JLayeredPane制作MDI背景
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
class BackImagePane extends JLayeredPane ...{
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
/** *//**
*
*/
private static final long serialVersionUID = 1L;
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public BackImagePane() ...{
super();
}
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
void setImage(Image img) ...{
bgImage = img;
}
private Image bgImage;
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void paint(Graphics g) ...{
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
if (bgImage != null) ...{
int imageh = bgImage.getHeight(null);
int imagew = bgImage.getWidth(null);
Dimension d = getSize();
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
for(int h=0; h<d.getHeight(); h += imageh) ...{
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
for(int w=0; w<d.getWidth(); w += imagew) ...{
g.drawImage(bgImage, w, h, this);
}
}
}
super.paint(g);
}
}
![](http://img.e-com-net.com/image/product/676e9d2fc4ab4565a433d97564e8b037.gif)
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public static void main(String[] args) ...{
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
EventQueue.invokeLater(new Runnable() ...{
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public void run() ...{
createGUI();
}
});
}
![](http://img.e-com-net.com/image/product/9e7f8c1d826f487fb4c8138af539c3e4.gif)
public static void createGUI() ...{
final JFrame frame = new JFrame("JAVA实现可设置背景的MDI窗口");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 600));
frame.getContentPane().add(new JavaMDI());
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
![](http://img.e-com-net.com/image/product/ff68a22582454f78825e64e5346ca758.gif)
![](http://img.e-com-net.com/image/product/ff68a22582454f78825e64e5346ca758.gif)
效果图如下: