Java:重叠JComponents的鼠标点击检测 [英] Java: mouse hit detection with overlapping JComponents

查看:141
本文介绍了Java:重叠JComponents的鼠标点击检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有空布局的JPanel.在此面板上,我绘制了一些自定义JComponent作为标记,小的红色圆形和线条.

I have a JPanel with null layout. On this panel I draw some custom JComponents as markers, little red rounds, and lines.

当线条不是水平或垂直时,JComponent的敏感区域"是一个矩形,其直线本身为斜边.这个过多的区域通常会将鼠标事件"窃取"到标记中.

When the line is not horizontal or vertical, the JComponent's "sensitive area" is a rectangle which has as hypotenuse the line itself. This excessive area often "steals" MouseEvents to the markers.

如果用户没有单击实线,如何使MouseEvents传递"到覆盖的对象?

How can I make MouseEvents "pass" to the covered objects, if the user is not clicking the real line?

推荐答案

正确的方法是moonwave99建议的方法.

The correct approach is that suggested by moonwave99.

1)创建并维护一系列行.

1) Create and mantain an array of lines.

2)使用Graphics2D原语绘制覆盖JPanel的paintComponent方法的数组中的每一行.

2) Draw every line in the array overriding the paintComponent method of JPanel using Graphics2D primitives.

3)在行的数组中添加带有研究功能的MouseListener到JPanel,以了解选择了哪一行.

3) Add a MouseListener to the JPanel, with a research function in the line's array, to know which line is selected.

这篇关于Java:重叠JComponents的鼠标点击检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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