简体   繁体   English

Jlabel额外的可点击空间

[英]Jlabel Extra Clickable Space

I create buttons using jlabels, so I can make a image into sort of a button. 我使用jlabels创建按钮,因此可以将图像变成某种按钮。 The only problem is, is that jlabels are square, therfore if i click somewhere within the square where the picture is not contained, it still runs the jlabel.MouseClickEvent. 唯一的问题是,jlabels是正方形的,因此如果我在不包含图片的正方形内的某个位置单击,它仍然会运行jlabel.MouseClickEvent。 Is there any fix for this, or another component that i could use? 是否对此有任何修复,或者我可以使用其他组件?

Ex. 例如 If i click this on the corner where the circle is not showing, but the square is still there, then the event fires. 如果我在未显示圆圈的角落处单击按钮,但正方形仍然存在,则事件触发。

Any fixes/different components to use? 是否有要使用的修复程序/不同组件? Thanks! 谢谢!

If you are just using simple Shapes for the images then you might be able to use the Shape Component found in Playing With Shapes . 如果仅对图像使用简单的Shapes,则可以使用“ Playing With Shapes”中Shape Component

The ShapeComponent will only respond to mouse events within the bounds of the Shape. ShapeComponent将仅响应Shape范围内的鼠标事件。

Otherwise the solution is to override the contains(...) method of your JLabel to check if the mouse point is in the bounds of your image, or in your case if the pixel at that location is not transparent. 否则,解决方案是重写JLabel的contains(...)方法,以检查鼠标指针是否在图像的边界内,或者在这种情况下,检查该位置处的像素是否不透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM