JAVA实现可设置背景的MDI窗口
生活随笔
收集整理的這篇文章主要介紹了
JAVA实现可设置背景的MDI窗口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JAVA實現可設置背景的MDI窗口 我們都知道,MDI(Multiple Document Interface)即多文檔界面。使用MDI窗體時,將在一個父窗體內建立工作區,父窗體能夠令一個以上的子窗體限制于其中活動及操作。在office系列及VS/VS.Net等很多軟件中都使用了MDI的表現形式。
?? 而遺憾的是,雖然很多編程語言都提供了顯著的MDI屬性,但Java卻算是個例外,基本上只能通過JDesktopPane結合JInternalFrame進行實現,而且局限性也比較多。
? 其實,利用Swing完成MDI,還有更簡單的捷徑可循。
? 下面,我給出一個簡單的例子:
? package?org.loon.test;
/**?*//**
?*?<p>Title:?LoonFramework</p>
?*?<p>Description:</p>
?*?<p>Copyright:?Copyright?(c)?2007</p>
?*?<p>Company:?LoonFramework</p>
?*?@author?chenpeng??
?*?@email:ceponline@yahoo.com.cn?
?*?@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;
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;
public?class?JavaMDI?extends?JPanel?...{
????/**?*//**
?????*?
?????*/
????private?static?final?long?serialVersionUID?=?1L;
????private?static?final?int?BACKLAYER?=?1;
????
????Font?FONT?=?new?Font("宋體",?Font.PLAIN,?12);
????private?final?BackImagePane?layerPane;
????//子窗體背景色
????private?int[]?colors?=?...{?0xdddddd,?0xaaaaff,?0xffaaaa,?0xaaffaa,?0xffffaa,?0xffaaff,?0xaaffff?,?0xddddff};
??
????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);
????}
????public?JavaMDI()?...{
????????super(new?BorderLayout());
????????
????????Image?image;
????????try?...{
????????????image?=?ImageIO.read(getClass().getResource("javamdi.jpg"));
????????}?catch?(IOException?e)?...{
????????????image=null;
????????}
????????layerPane?=?new?BackImagePane();
????????layerPane.setImage(image);
????????//隨機生成個子面板,作為內部窗體,實際使用時替換JPanel內部容器即可。
????????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);
????????
????}
????/**?*//**
?????*?創建子面板,作為在內部移動的窗體
?????*?@param?i
?????*?@return
?????*/
????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);
????????JTextArea?text?=?new?JTextArea();
????????text.setBackground(?new?Color(colors[i]));
????????text.setMargin(new?Insets(4,4,4,4));
????????text.setLineWrap(true);
????????JPanel?p?=?new?JPanel();
????????Color?col?=?getColor(colors[i],?0.5f);
????????Border?border?=?BorderFactory.createLineBorder(col,?1);
????????p.setBorder(border);
????????//移動監聽
????????DragMouseListener??li?=?new?DragMouseListener(p);
????????p.addMouseListener(li);
????????p.addMouseMotionListener(li);
????????p.setLayout(?new?BorderLayout());
????????p.add(label,?BorderLayout.NORTH);
????????p.add(text,?BorderLayout.CENTER);
????????//子窗體大小
????????p.setSize(?new?Dimension(200,?150));
????????return?p;
????}
????/**?*//**
?????*?子窗體拖拽監聽
?????*?@author?chenpeng
?????*
?????*/
????class?DragMouseListener?implements?MouseListener,?MouseMotionListener?...{
????????Point?origin;
????????JPanel?panel;
????????DragMouseListener(JPanel?p)?...{
????????????panel?=?p;
????????}
????????public?void?mousePressed(MouseEvent?e)?...{
????????????origin?=?new?Point(?e.getX(),?e.getY());
????????????//移動
????????????layerPane.moveToFront(panel);
????????}
????????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);
????????}
????????public?void?mouseClicked(MouseEvent?e)?...{}
????????public?void?mouseEntered(MouseEvent?e)?...{}
????????public?void?mouseExited(MouseEvent?e)?...{}
????????public?void?mouseReleased(MouseEvent?e)?...{}
????????public?void?mouseMoved(MouseEvent?e)?...{}
????}
????//用分層面板JLayeredPane制作MDI背景
????class?BackImagePane?extends?JLayeredPane?...{
????????/**?*//**
?????????*?
?????????*/
????????private?static?final?long?serialVersionUID?=?1L;
????????public?BackImagePane()?...{
????????????super();
????????}
????????void?setImage(Image?img)?...{
????????????bgImage?=?img;
????????}
????????private?Image?bgImage;
????????public?void?paint(Graphics?g)?...{
????????????if?(bgImage?!=?null)?...{
????????????????int?imageh?=?bgImage.getHeight(null);
????????????????int?imagew?=?bgImage.getWidth(null);
????????????????Dimension?d?=?getSize();
????????????????for(int?h=0;?h<d.getHeight();?h?+=?imageh)?...{
????????????????????for(int?w=0;?w<d.getWidth();?w?+=?imagew)?...{
????????????????????????g.drawImage(bgImage,?w,?h,?this);
????????????????????}
????????????????}
????????????}
????????????super.paint(g);
????????}
????}
????public?static?void?main(String[]?args)?...{
????????EventQueue.invokeLater(new?Runnable()?...{
????????????public?void?run()?...{
????????????????createGUI();
????????????}
????????});
????}
????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);
????}
}
效果圖如下:
posted on 2007-12-26 16:47 cping 閱讀(...) 評論(...) 編輯 收藏
?? 而遺憾的是,雖然很多編程語言都提供了顯著的MDI屬性,但Java卻算是個例外,基本上只能通過JDesktopPane結合JInternalFrame進行實現,而且局限性也比較多。
? 其實,利用Swing完成MDI,還有更簡單的捷徑可循。
? 下面,我給出一個簡單的例子:
? package?org.loon.test;
/**?*//**
?*?<p>Title:?LoonFramework</p>
?*?<p>Description:</p>
?*?<p>Copyright:?Copyright?(c)?2007</p>
?*?<p>Company:?LoonFramework</p>
?*?@author?chenpeng??
?*?@email:ceponline@yahoo.com.cn?
?*?@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;
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;
public?class?JavaMDI?extends?JPanel?...{
????/**?*//**
?????*?
?????*/
????private?static?final?long?serialVersionUID?=?1L;
????private?static?final?int?BACKLAYER?=?1;
????
????Font?FONT?=?new?Font("宋體",?Font.PLAIN,?12);
????private?final?BackImagePane?layerPane;
????//子窗體背景色
????private?int[]?colors?=?...{?0xdddddd,?0xaaaaff,?0xffaaaa,?0xaaffaa,?0xffffaa,?0xffaaff,?0xaaffff?,?0xddddff};
??
????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);
????}
????public?JavaMDI()?...{
????????super(new?BorderLayout());
????????
????????Image?image;
????????try?...{
????????????image?=?ImageIO.read(getClass().getResource("javamdi.jpg"));
????????}?catch?(IOException?e)?...{
????????????image=null;
????????}
????????layerPane?=?new?BackImagePane();
????????layerPane.setImage(image);
????????//隨機生成個子面板,作為內部窗體,實際使用時替換JPanel內部容器即可。
????????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);
????????
????}
????/**?*//**
?????*?創建子面板,作為在內部移動的窗體
?????*?@param?i
?????*?@return
?????*/
????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);
????????JTextArea?text?=?new?JTextArea();
????????text.setBackground(?new?Color(colors[i]));
????????text.setMargin(new?Insets(4,4,4,4));
????????text.setLineWrap(true);
????????JPanel?p?=?new?JPanel();
????????Color?col?=?getColor(colors[i],?0.5f);
????????Border?border?=?BorderFactory.createLineBorder(col,?1);
????????p.setBorder(border);
????????//移動監聽
????????DragMouseListener??li?=?new?DragMouseListener(p);
????????p.addMouseListener(li);
????????p.addMouseMotionListener(li);
????????p.setLayout(?new?BorderLayout());
????????p.add(label,?BorderLayout.NORTH);
????????p.add(text,?BorderLayout.CENTER);
????????//子窗體大小
????????p.setSize(?new?Dimension(200,?150));
????????return?p;
????}
????/**?*//**
?????*?子窗體拖拽監聽
?????*?@author?chenpeng
?????*
?????*/
????class?DragMouseListener?implements?MouseListener,?MouseMotionListener?...{
????????Point?origin;
????????JPanel?panel;
????????DragMouseListener(JPanel?p)?...{
????????????panel?=?p;
????????}
????????public?void?mousePressed(MouseEvent?e)?...{
????????????origin?=?new?Point(?e.getX(),?e.getY());
????????????//移動
????????????layerPane.moveToFront(panel);
????????}
????????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);
????????}
????????public?void?mouseClicked(MouseEvent?e)?...{}
????????public?void?mouseEntered(MouseEvent?e)?...{}
????????public?void?mouseExited(MouseEvent?e)?...{}
????????public?void?mouseReleased(MouseEvent?e)?...{}
????????public?void?mouseMoved(MouseEvent?e)?...{}
????}
????//用分層面板JLayeredPane制作MDI背景
????class?BackImagePane?extends?JLayeredPane?...{
????????/**?*//**
?????????*?
?????????*/
????????private?static?final?long?serialVersionUID?=?1L;
????????public?BackImagePane()?...{
????????????super();
????????}
????????void?setImage(Image?img)?...{
????????????bgImage?=?img;
????????}
????????private?Image?bgImage;
????????public?void?paint(Graphics?g)?...{
????????????if?(bgImage?!=?null)?...{
????????????????int?imageh?=?bgImage.getHeight(null);
????????????????int?imagew?=?bgImage.getWidth(null);
????????????????Dimension?d?=?getSize();
????????????????for(int?h=0;?h<d.getHeight();?h?+=?imageh)?...{
????????????????????for(int?w=0;?w<d.getWidth();?w?+=?imagew)?...{
????????????????????????g.drawImage(bgImage,?w,?h,?this);
????????????????????}
????????????????}
????????????}
????????????super.paint(g);
????????}
????}
????public?static?void?main(String[]?args)?...{
????????EventQueue.invokeLater(new?Runnable()?...{
????????????public?void?run()?...{
????????????????createGUI();
????????????}
????????});
????}
????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);
????}
}
效果圖如下:
posted on 2007-12-26 16:47 cping 閱讀(...) 評論(...) 編輯 收藏
轉載于:https://www.cnblogs.com/cping1982/archive/2007/12/26/2258076.html
總結
以上是生活随笔為你收集整理的JAVA实现可设置背景的MDI窗口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DRY
- 下一篇: java实现MD5加密的三种方式「建议收