Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个空布局的 JPanel。在这个面板上,我绘制了一些自定义 JComponents 作为标记、红色小圆和线条。
当线不是水平或垂直时,JComponent 的“敏感区域”是一个矩形,其本身具有斜边。这个过多的区域通常会“窃取”MouseEvents 到标记。
如果用户没有点击实线,我怎样才能让 MouseEvents “传递”给被覆盖的对象?
正确的方法是由moonwave99 建议的。
1) 创建并维护一系列行。
2) 使用 Graphics2D 基元绘制数组中覆盖 JPanel 的paintComponent 方法的每一行。
3) 向JPanel 添加一个MouseListener,在行的数组中带有一个研究函数,以知道选择了哪一行。