如何在带有嵌套组件的 Java Swing 中有效使用 MouseMotionListener?
Java Swing 中的 MouseMotionListener:克服组件拦截
在 Swing 中开发触摸用户界面时,当嵌套组件阻塞时,可能会遇到如何有效使用 MouseMotionListener 接口的挑战事件传播。
MouseMovedEvent 和 MouseDraggedEvent 旨在向上传播 GUI 层次结构,但它们可能会被容器内的组件阻止。例如,当向 JScrollPane 添加 JButton 时,事件可能不再触发 JScrollPane 的 MouseMotionListener。
为了解决此问题,我们提出了一种临时方法,该方法利用 JScrollPane 的内置方法操作,通常用于键绑定。通过调整 N 变量以与您的实现保持一致,此解决方案可以实现事件传播,而无需大量手动事件转发。
这是一个工作示例:
<code class="java">import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.Action; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JViewport; import javax.swing.Timer; public class ScrollAction extends JFrame { private static final int TILE = 64; private static final int DELTA = 16; public ScrollAction() { setupAndDisplay(); } private void setupAndDisplay() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.lightGray); int w = this.getWidth() / TILE + 1; int h = this.getHeight() / TILE + 1; for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { if ((row + col) % 2 == 0) { g.fillRect(col * TILE, row * TILE, TILE, TILE); } } } } }; panel.setOpaque(false); panel.setFocusable(true); panel.setPreferredSize(new Dimension(50 * TILE, 50 * TILE)); final JScrollPane scrollPane = new JScrollPane(panel); final JViewport viewport = scrollPane.getViewport(); viewport.addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { handleMouseMovement(scrollPane, e); } }); add(scrollPane); } private static final class ScrollTimer implements ActionListener { private static int N = 10; private static int DELAY = 100; private String cmd; private Timer timer; private Action action; private JScrollPane scrollPane; private int count; public ScrollTimer(JScrollPane scrollPane, String action) { this.cmd = action; this.timer = new Timer(DELAY, this); this.action = scrollPane.getActionMap().get(action); this.scrollPane = scrollPane; } @Override public void actionPerformed(ActionEvent e) { if (count++ < N) { action.actionPerformed(new ActionEvent(scrollPane, 0, cmd)); } else { timer.stop(); } } public void start() { count = 0; timer.start(); } public void stop() { timer.stop(); count = 0; } } private void handleMouseMovement(JScrollPane scrollPane, MouseEvent e) { final ScrollTimer left = new ScrollTimer(scrollPane, "scrollLeft"); final ScrollTimer right = new ScrollTimer(scrollPane, "scrollRight"); final ScrollTimer up = new ScrollTimer(scrollPane, "scrollUp"); final ScrollTimer down = new ScrollTimer(scrollPane, "scrollDown"); left.stop(); if (e.getX() < DELTA) { left.start(); } right.stop(); if (e.getX() > viewport.getWidth() - DELTA) { right.start(); } up.stop(); if (e.getY() < DELTA) { up.start(); } down.stop(); if (e.getY() > viewport.getHeight() - DELTA) { down.start(); } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ScrollAction().setVisible(true); pack(); } }); } }</code>
以上是如何在带有嵌套组件的 Java Swing 中有效使用 MouseMotionListener?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...
