Java JLayer:如何防止将事件删除到底层Component

时间:2016-11-10 07:47:20

标签: java swing events jlayer

您好我刚刚找到了JLayer功能。我想用它作为等待锁。如果我激活图层,则底层组件将接收所有事件(键和鼠标)。如何捕获图层中的事件并阻止委派?

提前谢谢

dbausnnd

2 个答案:

答案 0 :(得分:1)

我从http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html“动画繁忙的指示器”中获取示例并覆盖了一些方法:processMouseEvent和processMouseEvent有条件的e.consume(),当然还有

 jlayer.setLayerEventMask(
                    AWTEvent.MOUSE_EVENT_MASK |
                            AWTEvent.MOUSE_MOTION_EVENT_MASK
            );


class WaitLayerUI extends LayerUI<JPanel> implements ActionListener
{
        private boolean mIsRunning;
        private boolean mIsFadingOut;
        private Timer mTimer;

        private int mAngle;
        private int mFadeCount;
        private int mFadeLimit = 15;

        @Override
        public void installUI(JComponent c) {
            super.installUI(c);
            JLayer jlayer = (JLayer)c;
            jlayer.setLayerEventMask(
                    AWTEvent.MOUSE_EVENT_MASK |
                            AWTEvent.MOUSE_MOTION_EVENT_MASK
            );
        }

        @Override
        public void uninstallUI(JComponent c) {
            JLayer jlayer = (JLayer)c;
            jlayer.setLayerEventMask(0);
            super.uninstallUI(c);
        }

        @Override
        protected void processMouseEvent(MouseEvent e, JLayer l) {
            System.out.println("mouse event");
            if (mIsRunning)
            {
                e.consume();
            }
        }

        @Override
        protected void processMouseMotionEvent(MouseEvent e, JLayer l) {
            System.out.println("mouse motion event");
            if (mIsRunning)
            {
                e.consume();
            }
        }

        @Override
        public void paint(Graphics g, JComponent c)
        {
            Graphics2D g2 = (Graphics2D)g.create();

            try
            {
                int w = c.getWidth();
                int h = c.getHeight();

                // Paint the view.
                super.paint(g, c);

                if (!mIsRunning)
                {
                    return;
                }


                float fade = (float)mFadeCount / (float)mFadeLimit;

                // Gray it out.
                Composite urComposite = g2.getComposite();
                g2.setComposite(AlphaComposite.getInstance(
                        AlphaComposite.SRC_OVER, .5f * fade));
                g2.fillRect(0, 0, w, h);
                g2.setComposite(urComposite);

                // Paint the wait indicator.
                int s = Math.min(w, h) / 10;
                int cx = w / 2;
                int cy = h / 2;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setStroke(
                        new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g2.setPaint(Color.white);
                g2.rotate(Math.PI * mAngle / 180, cx, cy);
                for (int i = 0; i < 12; i++)
                {
                    float scale = (11.0f - (float)i) / 11.0f;
                    g2.drawLine(cx + s, cy, cx + s * 2, cy);
                    g2.rotate(-Math.PI / 6, cx, cy);
                    g2.setComposite(AlphaComposite.getInstance(
                            AlphaComposite.SRC_OVER, scale * fade));
                }
            }
            catch (Exception ex) {
                mIsRunning = false;
            }
            finally
            {
                g2.dispose();
            }
        }

        public void actionPerformed(ActionEvent e)
        {
            if (mIsRunning)
            {
                firePropertyChange("tick", 0, 1);
                mAngle += 3;
                if (mAngle >= 360)
                {
                    mAngle = 0;
                }
                if (mIsFadingOut)
                {
                    if (--mFadeCount == 0)
                    {
                        mIsRunning = false;
                        mTimer.stop();
                    }
                }
                else if (mFadeCount < mFadeLimit)
                {
                    mFadeCount++;
                }
            }
        }

        public void start()
        {
            if (mIsRunning)
            {
                return;
            }

            // Run a thread for animation.
            mIsRunning = true;
            mIsFadingOut = false;
            mFadeCount = 0;
            int fps = 24;
            int tick = 1000 / fps;
            mTimer = new Timer(tick, this);
            mTimer.start();
        }

        public void stop()
        {
            mIsFadingOut = true;
        }

        @Override
        public void applyPropertyChange(PropertyChangeEvent pce, JLayer l)
        {
            if ("tick".equals(pce.getPropertyName()))
            {
                l.repaint();
            }
        }
}

答案 1 :(得分:0)

Swing将搜索父/子层次结构,以查找侦听MouseEventKeyEvent的组件。

因此,您需要在底层添加MouseListenerKeyListener

例如,这是Disable Glass Pane中使用的方法。