模块  java.desktop
软件包  javax.swing

Class JLayer<V extends Component>

  • 参数类型
    V - JLayer的视图组件的类型
    实现的所有接口
    ImageObserverMenuContainerPropertyChangeListenerSerializableEventListenerAccessibleScrollable

    public final class JLayer<V extends Component>extends JComponentimplements Scrollable, PropertyChangeListener, Accessible
    JLayer是Swing组件的通用装饰器,使您能够实现各种高级绘画效果,并接收在其边界内生成的所有AWTEvent的通知。

    JLayer将绘制和输入事件的处理委托给LayerUI对象,该对象执行实际装饰。

    LayerUI实现的自定义LayerUI和事件通知适用于JLayer本身及其所有子组件。 这种组合使您可以通过添加新的高级功能来丰富现有组件,例如临时锁定层次结构,复合组件的数据提示,增强的鼠标滚动等等。

    JLayer是一个很好的解决方案,如果您只需要对复合组件进行自定义绘制或从其子组件捕获输入事件。

      import javax.swing.*; import javax.swing.plaf.LayerUI; import java.awt.*; public class JLayerSample {     private static JLayer<JComponent> createLayer() {         // This custom layerUI will fill the layer with translucent green         // and print out all mouseMotion events generated within its borders         LayerUI<JComponent> layerUI = new LayerUI<JComponent>() {             public void paint(Graphics g, JComponent c) {                 // paint the layer as is                 super.paint(g, c);                 // fill it with the translucent green                 g.setColor(new Color(0, 128, 0, 128));                 g.fillRect(0, 0, c.getWidth(), c.getHeight());             }             public void installUI(JComponent c) {                 super.installUI(c);                 // enable mouse motion events for the layer's subcomponents                 ((JLayer) c).setLayerEventMask(AWTEvent.MOUSE_MOTION_EVENT_MASK);             }             public void uninstallUI(JComponent c) {                 super.uninstallUI(c);                 // reset the layer event mask                 ((JLayer) c).setLayerEventMask(0);             }             // overridden method which catches MouseMotion events             public void eventDispatched(AWTEvent e, JLayer<? extends JComponent> l) {                 System.out.println("AWTEvent detected: " + e);             }         };         // create a component to be decorated with the layer         JPanel panel = new JPanel();         panel.add(new JButton("JButton"));         // create the layer for the panel using our custom layerUI         return new JLayer<JComponent>(panel, layerUI);     }     private static void createAndShowGUI() {         final JFrame frame = new JFrame();         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);         // work with the layer as with any other Swing component         frame.add(createLayer());         frame.setSize(200, 200);         frame.setLocationRelativeTo(null);         frame.setVisible(true);     }     public static void main(String[] args) throws Exception {         SwingUtilities.invokeAndWait(new Runnable() {             public void run() {                 createAndShowGUI();             }         });     } } 
    注意: JLayer不支持以下方法: 使用其中任何一个都会导致UnsupportedOperationException被抛出,添加一个组件到JLayer使用setView(Component)setGlassPane(JPanel)
    从以下版本开始:
    1.7
    另请参见:
    JLayer(Component)setView(Component)getView()LayerUIJLayer(Component, LayerUI)setUI(javax.swing.plaf.LayerUI)getUI()Serialized Form