JDesktopPane类详解

来源:互联网 发布:下载听故事软件 编辑:程序博客网 时间:2024/06/14 22:24

1、先仔细阅读以下程序代码(JDesktopPane_1.java):

  1. package com.han;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.net.URL;  
  5.   
  6. import javax.swing.ImageIcon;  
  7. import javax.swing.JDesktopPane;  
  8. import javax.swing.JFrame;  
  9. import javax.swing.JLabel;  
  10.   
  11. public class JDesktopPane_1 extends JFrame {  
  12.   
  13.     /** 
  14.      *  
  15.      */  
  16.     private static final long serialVersionUID = 6415712755180575102L;  
  17.   
  18.     public JDesktopPane_1() {  
  19.         // TODO Auto-generated constructor stub  
  20.         JDesktopPane desktopPane = new JDesktopPane();  
  21.         getContentPane().add(desktopPane, BorderLayout.CENTER);  
  22.         JLabel backLabel = new JLabel();  
  23.           
  24.         // same as JDesktopPane_1.class.getResource()  
  25.         URL resource = this.getClass().getResource("/images/LightHouse.jpg");  
  26.           
  27.         ImageIcon imageIcon = new ImageIcon(resource);  
  28.         backLabel.setIcon(imageIcon);  
  29.           
  30.         // Very important ! because this is a virtual desktop and so one  
  31.         // should specify the position and the size of the component or  
  32.         // frame which are to placed on the desktop. Or else, the component  
  33.         // or the frame will not be displayed.  
  34.         backLabel.setBounds(00, imageIcon.getIconWidth(),  
  35.                 imageIcon.getIconHeight());   
  36.           
  37.         // "new Integer(Integer.MIN_VALUE)" ensures that its layer is always under the others.  
  38.         desktopPane.add(backLabel, BorderLayout.CENTER, new Integer(Integer.MIN_VALUE));   
  39.           
  40.         setBounds(00, imageIcon.getIconWidth(), imageIcon.getIconHeight());  
  41.     }  
  42.   
  43.     /** 
  44.      * @param args 
  45.      */  
  46.     public static void main(String[] args) {  
  47.         // TODO Auto-generated method stub  
  48.         JDesktopPane_1 frame = new JDesktopPane_1();  
  49.         frame.setTitle("系统桌面");  
  50.         frame.setVisible(true);  
  51.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  52.     }  
  53.   
  54. }  

2、再反复理解下面这段话:

用于创建多文档界面或虚拟桌面的容器。用户可创建 JInternalFrame 对象并将其添加到 JDesktopPaneJDesktopPane 扩展了JLayeredPane,以管理可能的重叠内部窗体。它还维护了对DesktopManager 实例的引用,这是由 UI 类为当前的外观 (L&F) 所设置的。注意,JDesktopPane 不支持边界。

此类通常用作 JInternalFrames 的父类,为 JInternalFrames 提供一个可插入的DesktopManager 对象。特定于 L&F 的实现 installUI 负责正确设置 desktopManager 变量。JInternalFrame 的父类是JDesktopPane 时,它应该将其大部分行为(关闭、调整大小等)委托给 desktopManager

有关此内容更多的文档和示例,请参阅 The Java Tutorial 的 How to Use Internal Frames 一节。

警告: Swing 不是线程安全的。有关更多信息,请参阅 Swing's Threading Policy。

警告:此类的序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同 Swing 版本的应用程序之间进行 RMI(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见java.beans.XMLEncoder

3、最后理解下面程序代码(JInternalFrameDemo1):

  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import javax.swing.*;   
  4.   
  5. public class JInternalFrameDemo1 extends JFrame implements ActionListener{   
  6.     JDesktopPane desktopPane;   
  7.     int count = 1;   
  8.        
  9.     public JInternalFrameDemo1(){   
  10.         super("JInternalFrameDemo1");   
  11.         Container contentPane = this.getContentPane();   
  12.         contentPane.setLayout(new BorderLayout());   
  13.            
  14.         JButton button = new JButton("Crate New Internal Frames");   
  15.         button.addActionListener(this);   
  16.         contentPane.add(button,BorderLayout.SOUTH);   
  17.            
  18.         desktopPane = new JDesktopPane();   
  19.         contentPane.add(desktopPane);//將虛擬桌面加入到content pane中  
  20.            
  21.         this.setSize(new Dimension(300,300));   
  22.         this.setVisible(true);   
  23.         this.addWindowListener(new WindowAdapter(){   
  24.             @Override  
  25.             public void windowClosing(WindowEvent e){   
  26.                 System.exit(0);   
  27.             }   
  28.         });   
  29.     }   
  30.        
  31.     @Override  
  32.     public void actionPerformed(ActionEvent e){   
  33.         JInternalFrame internalFrame = new JInternalFrame("Internale Frame"+count++,true,true,true,true);   
  34.         //internalFrame.setLocation(20,20);  
  35.         internalFrame.setSize(new Dimension(200,200));   
  36.         internalFrame.setVisible(true);   
  37.            
  38.         Container icontentPane = internalFrame.getContentPane();   
  39.         JTextArea textArea = new JTextArea();   
  40.         JButton b = new JButton("Internale Frame Button");   
  41.         icontentPane.add(textArea,"Center");   
  42.         icontentPane.add(b,"South");   
  43.            
  44.         desktopPane.add(internalFrame);//將internalFrame加入到desktopPane,通過虛擬桌面來管理internalFrame  
  45.         //this.getContentPane().add(internalFrame);  
  46.         try{   
  47.             internalFrame.setSelected(true);//獲得焦點  
  48.         }catch(java.beans.PropertyVetoException ex){   
  49.             System.out.println("Exception while selecting");   
  50.         }   
  51.     }   
  52.     public static void main(String...args){   
  53.         new JInternalFrameDemo1();   
  54.     }   
  55. }