zoukankan      html  css  js  c++  java
  • Java JLayeredPane 使用

    public class JLayeredPaneextends JComponentimplements Accessible

    JLayeredPane 为 JFC/Swing 容器添加了深度,允许组件在需要时互相重叠Integer 对象指定容器中每个组件的深度,其中编号较高的组件位于其他组件之上。有关面向任务的文档和使用分层窗格的示例,请参阅 The Java Tutorial 中的 How to Use a Layered Pane 一节。


    DEFAULT_LAYER
    为方便起见,JLayeredPane 将该深度范围分成几个不同的层。将组件放入相应的层,这样更容易确保组件正确地重叠,而不必担心为具体的深度指定编号:

    大多数组件位于的标准层。这是最底层。
    PALETTE_LAYER
    调色板层位于默认层之上。它们对于浮动工具栏和调色板很有用,因此可以位于其他组件之上。
    MODAL_LAYER
    该层用于模式对话框。它们将出现在容器中所有工具栏、调色板或标准组件的上面。
    POPUP_LAYER
    弹出层显示在对话框的上面。这样,与组合框、工具提示和其他帮助文本关联的弹出式窗口将出现在组件、调色板或生成它们的对话框之上。
    DRAG_LAYER
    拖动一个组件时,将该组件重分配到拖动层可确保将其定位在容器中的其他所有组件之上。完成拖动后,可将该组件重分配到其正常层。

    可以使用 JLayeredPane 的方法 moveToFront(Component)moveToBack(Component) 和 setPosition 在组件所在层中对其进行重定位。还可以使用 setLayer 方法更改该组件的当前层。

    详细信息

    JLayeredPane 以类似 Container 的方式管理其子级列表,但允许在其内部定义多个层。对同一层中子级的管理就像普通的 Container 对象一样,但添加的功能是,当子组件重叠时,高层中的子组件显示在低层中的子组件之上。

    每一层都是一个不同的整数。可以在调用 add 的过程中通过传递 Integer 对象,从而在 Component 上设置 layer 属性。
    例如:

         layeredPane.add(child, JLayeredPane.DEFAULT_LAYER);
    或者
    layeredPane.add(child, new Integer(10));
     

    还可以通过在 JLayeredPane 上进行如下调用在 Component 上设置 layer 属性:

         layeredPaneParent.setLayer(child, 10)

    JLayeredPane 是该 Component 的父组件。应该将子组件添加到父组件之前 设置 layer 属性。

    编号较高的层显示在编号较低的层之上。因此,对于每个组件,使用其层编号和字母即可,有代表性的列表顺序如下所示:

           5a, 5b, 5c, 2a, 2b, 2c, 1a 

    其中最左边的组件最接近显示区的顶部。

    可以通过调用 moveToFront 或 moveToBack 将组件移入到其所在层的顶部或底部位置。

    还可以直接指定某层中组件的位置。有效的位置范围是从 0 到该层中组件数减一所得的值。值 -1 指示最底层位置。值 0 指示最顶层位置。与层的编号不同,较高的位置值显示在较低 处。

    注: 此序列(通过 java.awt.Container 定义)与层的编号序列相反。可是通常使用的是 moveToFrontmoveToBack 和 setLayer 方法。

    以下是使用方法 add(Component, layer, position) 的一些示例:调用 add(5x, 5, -1) 得到:

           5a, 5b, 5c, 5x, 2a, 2b, 2c, 1a 

    调用 add(5z, 5, 2) 得到:

           5a, 5b, 5z, 5c, 5x, 2a, 2b, 2c, 1a 

    调用 add(3a, 3, 7) 得到:

           5a, 5b, 5z, 5c, 5x, 3a, 2a, 2b, 2c, 1a 

    使用正常的 paint/event 机制会使 1a 出现在底部,5a 出现在所有其他组件之上。

    注: 这些层只是一个逻辑构造,LayoutManager 将影响此容器的所有子组件,而不管其层设置如何。

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

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

    setLayer

    public void setLayer(Component c,

                         int layer)
    设置指定组件的 layer 属性,使之成为该层中最底部的组件。应该在将组件添加到其父组件之前调用该方法。
    参数:
    c - 要为其设置层的 Component
    layer - 一个 int 值,指定要设置的层,其中较低编号更接近底部
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    
    
    public class JLayeredPaneTest extends JFrame implements ActionListener{
         private JLayeredPane layeredPane;
         private JButton b_a,b_b;
         
         public JLayeredPaneTest()
         {
             super("Test");
             layeredPane=this.getLayeredPane();//获取JLayeredPane
             
             b_a=new JButton("A");
             b_b=new JButton("B");
             
             b_a.addActionListener(this);
             b_b.addActionListener(this);
             
             layeredPane.add(b_a,new Integer(200));
             layeredPane.add(b_b,new Integer(300));
             
              b_a.setBounds(new Rectangle(100, 100, 100, 100)); // Button出现位置
              b_a.setVisible(true); // 显示
    
              b_b.setBounds(new Rectangle(50, 50, 100, 100));
              b_b.setVisible(true);
    
              this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              this.setSize(360, 260);
              this.setVisible(true);
         }
      
        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand().equals("A"))
            {
                layeredPane.setLayer(b_a,new Integer(300));
                layeredPane.setLayer(b_b, new Integer(200));
            }
            else
            {
                layeredPane.setLayer(b_a,new Integer(200));
                layeredPane.setLayer(b_b, new Integer(300));
            }
        
        }
                
     
    
        public static void main(String[] args) 
        {
            new JLayeredPaneTest();
    
        }
    
    }
    JFrame.getLayeredPane()

    Returns the layeredPane object for this frame.

    Every Swing container that has a root pane — such as JFrameJAppletJDialog, or JInternalFrame — automatically has a layered pane. Most programs do not explicitly use the root pane's layered pane, so this section will not discuss it. You can find information about it in The Root Pane, which provides an overview, and The Layered Pane, which has further details. This section tells you how to create your own layered pane and use it anywhere you can use a regular Swing container.

    Swing provides two layered pane classes. The first, JLayeredPane, is the class that root panes use and is the class used by the example in this section. The second, JDesktopPane, is a JLayeredPane subclass that is specialized for the task of holding internal frames. For examples of using JDesktopPane, see How to Use Internal Frames.http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

    每个swing容器都有一个root pane根面板,如JFrame等,每个root pane自动有一个Layered pane

    上面的网址给了一个实例程序,非常好。

  • 相关阅读:
    排序算法的稳定性
    字符串处理常用函数
    判断两棵二叉树是否相等
    约瑟夫环
    自加++
    Linux 安装配置 Tomcat
    在 eclipse 中将 web 项目部署到 tomcat 服务器上
    PHP连接MySQL数据库
    logback
    Log4J
  • 原文地址:https://www.cnblogs.com/youxin/p/2783652.html
Copyright © 2011-2022 走看看