`
mmdev
  • 浏览: 12928199 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

JAVA实现可设置背景的MDI窗口

 
阅读更多
我们都知道,MDI(Multiple Document Interface)即多文档界面。使用MDI窗体时,将在一个父窗体内建立工作区,父窗体能够令一个以上的子窗体限制于其中活动及操作。在office系列及VS/VS.Net等很多软件中都使用了MDI的表现形式。
而遗憾的是,虽然很多编程语言都提供了显著的MDI属性,但Java却算是个例外,基本上只能通过JDesktopPane结合JInternalFrame进行实现,而且局限性也比较多。
其实,利用Swing完成MDI,还有更简单的捷径可循。
下面,我给出一个简单的例子:
packageorg.loon.test;
/***//**
*<p>Title:LoonFramework</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2007</p>
*<p>Company:LoonFramework</p>
*
@authorchenpeng
*@email:ceponline@yahoo.com.cn
*
@version0.1
*/

importjavax.imageio.ImageIO;
importjavax.swing.BorderFactory;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JLayeredPane;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.SwingConstants;
importjavax.swing.WindowConstants;
importjavax.swing.border.Border;

importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.EventQueue;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.Insets;
importjava.awt.Point;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.event.MouseMotionListener;
importjava.io.IOException;



publicclassJavaMDIextendsJPanel...{
/***//**
*
*/

privatestaticfinallongserialVersionUID=1L;


privatestaticfinalintBACKLAYER=1;


FontFONT
=newFont("宋体",Font.PLAIN,12);

privatefinalBackImagePanelayerPane;
//子窗体背景色
privateint[]colors=...{0xdddddd,0xaaaaff,0xffaaaa,0xaaffaa,0xffffaa,0xffaaff,0xaaffff,0xddddff};

privateColorgetColor(inti,floatf)...{
intb=(int)((i&0xff)*f);
i
=i>>8;
intg=(int)((i&0xff)*f);
i
=i>>8;
intr=(int)((i&0xff)*f);
returnnewColor(r,g,b);
}


publicJavaMDI()...{
super(newBorderLayout());

Imageimage;
try...{
image
=ImageIO.read(getClass().getResource("javamdi.jpg"));
}
catch(IOExceptione)...{
image
=null;
}

layerPane
=newBackImagePane();
layerPane.setImage(image);

//随机生成个子面板,作为内部窗体,实际使用时替换JPanel内部容器即可。
for(inti=0;i<colors.length;i++)...{
JPanelp
=createChildPanel(i);
p.setLocation(i
*80+20,i*50+15);
layerPane.add(p,BACKLAYER);
}

add(layerPane,BorderLayout.CENTER);

}



/***//**
*创建子面板,作为在内部移动的窗体
*
@parami
*
@return
*/

privateJPanelcreateChildPanel(inti)...{
//使用html标记设定颜色
Stringhtml="<html><fontcolor=#333333>子窗体ID"+i+"</font></html>";
JLabellabel
=newJLabel(html);
label.setFont(FONT);
label.setOpaque(
true);
label.setHorizontalAlignment(SwingConstants.CENTER);
//设定背景色
label.setBackground(getColor(colors[i],0.85f));
//设定边距
Borderborder1=BorderFactory.createEmptyBorder(4,4,4,4);;
label.setBorder(border1);

JTextAreatext
=newJTextArea();
text.setBackground(
newColor(colors[i]));
text.setMargin(
newInsets(4,4,4,4));
text.setLineWrap(
true);

JPanelp
=newJPanel();

Colorcol
=getColor(colors[i],0.5f);
Borderborder
=BorderFactory.createLineBorder(col,1);
p.setBorder(border);

//移动监听
DragMouseListenerli=newDragMouseListener(p);
p.addMouseListener(li);
p.addMouseMotionListener(li);

p.setLayout(
newBorderLayout());
p.add(label,BorderLayout.NORTH);
p.add(text,BorderLayout.CENTER);
//子窗体大小
p.setSize(newDimension(200,150));
returnp;
}



/***//**
*子窗体拖拽监听
*
@authorchenpeng
*
*/

classDragMouseListenerimplementsMouseListener,MouseMotionListener...{
Pointorigin;
JPanelpanel;

DragMouseListener(JPanelp)
...{
panel
=p;
}

publicvoidmousePressed(MouseEvente)...{
origin
=newPoint(e.getX(),e.getY());
//移动
layerPane.moveToFront(panel);
}

publicvoidmouseDragged(MouseEvente)...{
if(origin==null)return;
intdx=e.getX()-origin.x;
intdy=e.getY()-origin.y;
Pointp
=panel.getLocation();
panel.setLocation(p.x
+dx,p.y+dy);
}


publicvoidmouseClicked(MouseEvente)...{}
publicvoidmouseEntered(MouseEvente)...{}
publicvoidmouseExited(MouseEvente)...{}
publicvoidmouseReleased(MouseEvente)...{}
publicvoidmouseMoved(MouseEvente)...{}
}


//用分层面板JLayeredPane制作MDI背景
classBackImagePaneextendsJLayeredPane...{
/***//**
*
*/

privatestaticfinallongserialVersionUID=1L;

publicBackImagePane()...{
super();
}


voidsetImage(Imageimg)...{
bgImage
=img;
}

privateImagebgImage;

publicvoidpaint(Graphicsg)...{
if(bgImage!=null)...{
intimageh=bgImage.getHeight(null);
intimagew=bgImage.getWidth(null);
Dimensiond
=getSize();
for(inth=0;h<d.getHeight();h+=imageh)...{
for(intw=0;w<d.getWidth();w+=imagew)...{
g.drawImage(bgImage,w,h,
this);
}

}

}

super.paint(g);
}

}


publicstaticvoidmain(String[]args)...{
EventQueue.invokeLater(
newRunnable()...{
publicvoidrun()...{
createGUI();
}

}
);
}

publicstaticvoidcreateGUI()...{
finalJFrameframe=newJFrame("JAVA实现可设置背景的MDI窗口");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(
newDimension(800,600));
frame.getContentPane().add(
newJavaMDI());
frame.setAlwaysOnTop(
true);
frame.setLocationRelativeTo(
null);
frame.setVisible(
true);
}

}




效果图如下:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics