Java Swing中的MouseMotionListener,将其与组件内部的组件一起使用 [英] MouseMotionListener in Java Swing, using it with components inside components etc

查看:113
本文介绍了Java Swing中的MouseMotionListener,将其与组件内部的组件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swing中的Touch User界面。虽然我知道这不是最佳的,但我的截止日期很短,没有时间使用触摸屏特定的GUI包(如果有的话)。

I am working on a Touch User interface in Swing. While I know this isn't optimal, I am on a short deadline and don't have time to Touch-screen specific GUI packages (if there are any).

我希望我的用户能够在屏幕上滑动他们的手指,并且我制作的特殊JScrollPane视图随之移动。
代码非常简单 -

I want my users to be able to 'swipe' their finger across the screen, and the view of a special JScrollPane I made moves with it. The code is very simple -

    public class PanScrollPane extends JScrollPane implements MouseMotionListener{  
public PanScrollPane() {
    super();        
    this.addMouseMotionListener(this);      
}
@Override
public void mouseDragged(MouseEvent arg0) {
    System.out.println("Mouse Dragged!");       
}
@Override
public void mouseMoved(MouseEvent arg0) {
    System.out.println("Mouse Moved!");     
}

我遇到的问题是JScrollPane是所有种类的容器JComponents。当我第一次开始研究这个时,我发现MouseMovedEvent和MouseDraggedEvent会向上传播'GUI树',直到他们遇到一个带有专门针对该事件的侦听器的Component。现在看来,我添加到panScrollPane的任何组件都会阻止任何这些MouseMotion事件,让我无法平移。

The problem I'm having is that the JScrollPane is a container for all sorts of JComponents. When I first started working on this, I figured the MouseMovedEvent and MouseDraggedEvent would propagate up the 'GUI tree', untill they encountered a Component with a listener specifically for that event. Now it seems that any component I add to the panScrollPane blocks any of these MouseMotion events, leaving me unable to pan.

    panScrollPane.add(new JButton("This thing blocks any mouse motion events"));

我想用手传播MouseEvent(将侦听器添加到每个组件,然后让它们发送事件他们的父母)会工作。然而,这是一项非常耗时的工作,因为我宁愿把时间花在其他事情上,所以我想知道你们中是否有人知道这个问题的任何解决办法。

I figured propagating the MouseEvent by hand (adding listeners to every single component and then having them send the event to their parent) would work. This, however, is a very time-intensive undertaking and as I would rather spend my time working on other things, I was wondering if any of you know any work-around for this problem.

感谢阅读,希望感谢您的回答! :)

Thanks for reading, and hopefully thanks for answering! :)

编辑:让我的意图更清晰。我只希望panPanel捕获mousemotion事件,任何其他事件(如MouseClick,MouseRelease)应该正常处理

edit: To make my intentions clearer. I only want the mousemotion events to be caught by the panPanel, any other event (like MouseClick, MouseRelease) should be processed normally

推荐答案

如何使用 GlassPane ?我认为它的意思是准确地解决这些类型的情况。

How about using a GlassPane? I think its meant to address exactly these types of situations.

这篇关于Java Swing中的MouseMotionListener,将其与组件内部的组件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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