1、先仔细阅读以下程序代码(JDesktopPane_1.java):
- package com.han;
- import java.awt.BorderLayout;
- import java.net.URL;
- import javax.swing.ImageIcon;
- import javax.swing.JDesktopPane;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class JDesktopPane_1 extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 6415712755180575102L;
- public JDesktopPane_1() {
- // TODO Auto-generated constructor stub
- JDesktopPane desktopPane = new JDesktopPane();
- getContentPane().add(desktopPane, BorderLayout.CENTER);
- JLabel backLabel = new JLabel();
- // same as JDesktopPane_1.class.getResource()
- URL resource = this.getClass().getResource("/images/LightHouse.jpg");
- ImageIcon imageIcon = new ImageIcon(resource);
- backLabel.setIcon(imageIcon);
- // Very important ! because this is a virtual desktop and so one
- // should specify the position and the size of the component or
- // frame which are to placed on the desktop. Or else, the component
- // or the frame will not be displayed.
- backLabel.setBounds(0, 0, imageIcon.getIconWidth(),
- imageIcon.getIconHeight());
- // "new Integer(Integer.MIN_VALUE)" ensures that its layer is always under the others.
- desktopPane.add(backLabel, BorderLayout.CENTER, new Integer(Integer.MIN_VALUE));
- setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- JDesktopPane_1 frame = new JDesktopPane_1();
- frame.setTitle("系统桌面");
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
2、再反复理解下面这段话:
用于创建多文档界面或虚拟桌面的容器。用户可创建 JInternalFrame
对象并将其添加到 JDesktopPane
。JDesktopPane
扩展了 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):
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class JInternalFrameDemo1 extends JFrame implements ActionListener{
- JDesktopPane desktopPane;
- int count = 1;
- public JInternalFrameDemo1(){
- super("JInternalFrameDemo1");
- Container contentPane = this.getContentPane();
- contentPane.setLayout(new BorderLayout());
- JButton button = new JButton("Crate New Internal Frames");
- button.addActionListener(this);
- contentPane.add(button,BorderLayout.SOUTH);
- desktopPane = new JDesktopPane();
- contentPane.add(desktopPane);//將虛擬桌面加入到content pane中
- this.setSize(new Dimension(300,300));
- this.setVisible(true);
- this.addWindowListener(new WindowAdapter(){
- @Override
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- }
- @Override
- public void actionPerformed(ActionEvent e){
- JInternalFrame internalFrame = new JInternalFrame("Internale Frame"+count++,true,true,true,true);
- //internalFrame.setLocation(20,20);
- internalFrame.setSize(new Dimension(200,200));
- internalFrame.setVisible(true);
- Container icontentPane = internalFrame.getContentPane();
- JTextArea textArea = new JTextArea();
- JButton b = new JButton("Internale Frame Button");
- icontentPane.add(textArea,"Center");
- icontentPane.add(b,"South");
- desktopPane.add(internalFrame);//將internalFrame加入到desktopPane,通過虛擬桌面來管理internalFrame
- //this.getContentPane().add(internalFrame);
- try{
- internalFrame.setSelected(true);//獲得焦點
- }catch(java.beans.PropertyVetoException ex){
- System.out.println("Exception while selecting");
- }
- }
- public static void main(String...args){
- new JInternalFrameDemo1();
- }
- }