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

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

问题描述

我正在 Swing 中开发触摸用户界面.虽然我知道这不是最佳选择,但我的截止日期很短,没有时间触摸屏特定的 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 是各种 JComponent 的容器.当我第一次开始研究这个时,我认为 MouseMovedEvent 和 MouseDraggedEvent 会向上传播GUI 树",直到他们遇到一个带有专门用于该事件的侦听器的组件.现在看来,我添加到 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

推荐答案

如何使用 玻璃窗?我认为它的目的是解决这些类型的情况.

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

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

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