在多个不同的组件中使用事件 [英] Use event in multiple different components

查看:34
本文介绍了在多个不同的组件中使用事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JscrollPane,其中包含我在其上绘制的 JPanel.我想使用 MouseWheelEvent 来:

I have a JscrollPane which holds a JPanel on which I draw. I would like to use the MouseWheelEvent to:

  • 缩放(使用 CTRL+Wheel)面板内容
  • 并滚动滚动窗格(仅使用滚轮)

每个动作本身都有效,但我无法使两者都有效.一旦我将 MouseWheelListener 添加到 JPanel,事件就不会到达 JScrollPanels 监听器.

Each action by itself works, but I dont't manage to make both work. As soon as I add a MouseWheelListener to the JPanel, the event does not arrive at the JScrollPanels listner.

如果在 JPanel 中未使用 MouseWheelEvent,如何将其转发给 JScrollPanes MouseWheelListener?

How can I get the MouseWheelEvent to be forwarded the JScrollPanes MouseWheelListener if it has not been used in the JPanel?

感谢您的提示:还有办法让两个听众都听吗?因为滚动窗格中的滚动实际上是默认实现的.那么有没有一种不用任何额外实现就可以使用它的方法?

Thanks for the hint: Is there also a way to make both listeners listen? Because the scrolling in the scrollpane is actually default implemented. So is there a way to use it without any additional implementation?

谢谢&问候,马克

Thanks & Regards, Marc

推荐答案

让您的自定义侦听器将事件(它不想自己处理)分派给发件人的父级,最终,scrollPane 将接收它们:

Make your custom listener dispatch the events (which it doesn't want to handle itself) to the sender's parent, eventually, the scrollPane will pick them up:

    final MouseWheelListener wheel = new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (shouldHandleHere(e)) {
                LOG.info("do-my-own-stuff");
            } else {
                LOG.info("dispatch-to-parent");
                e.getComponent().getParent().dispatchEvent(e);
            } 
        }

        public boolean shouldHandleHere(MouseWheelEvent e) {
            return (e.getModifiersEx() & MouseWheelEvent.CTRL_DOWN_MASK) != 0;
        }
    };

这篇关于在多个不同的组件中使用事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆