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



嗨,我刚刚找到了JLayer函数。我想把它用作等待锁。如果我激活层,底层组件接收所有事件(键和鼠标)。如何在层中捕获事件并防止委托?

提前谢谢你

dbausnnd

我以http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html "Animating a Busy Indicator"为例,并覆盖了一些方法:processMouseEvent和processMouseEvent与有条件的e.p obe(),当然还有

 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();
            }
        }
}

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

所以你需要在底层添加MouseListenerKeyListener

例如,这是在禁用玻璃窗格中使用的方法。

相关内容

  • 没有找到相关文章

最新更新