将子组件添加到JButton [英] Adding a child component to a JButton
问题描述
是否可以将子组件放置在JButton
内,并使它对鼠标事件的子集透明,从而:
Is it possible to place a child component inside a JButton
and make it transparent to a subset of mouse events so that:
- 子组件收到
MouseMotionEvent
s(因此它可以通过修改显示的图像进行响应) - 单击仍然会压下子组件"
JButton
"
- The child component receives
MouseMotionEvent
s (so it can respond by modifying a displayed image) - Clicking still depresses the
JButton
"behind" the child component
如果将子组件添加到按钮上,但未进行其他任何更改,则在子组件占据的区域中单击不会激活按钮.
If you add the child component to the button but make no other changes, clicking in the area occupied by the child does not activate the button.
我知道可以通过创建一个扩展JButton
的新类来实现,但是我更喜欢使用已经编写的子组件.
I know this can be achieved by creating a new class that extends JButton
but I would prefer to use a child component which has already been written.
注意:这纯粹是出于装饰性原因.子组件仅更改其自身的外观.它不会执行任何其他操作来响应点击.通常只有一个Action
由按钮触发.
Note: this is purely for cosmetic reasons. The child component only changes its own appearance. It does not perform any other actions in response to clicks. There is just one Action
, triggered by the button in the normal way.
推荐答案
是的,虽然可以,但是可能有更好的方法来更改鼠标悬停时JButton的外观.您可以扩展ButtonUI来做到这一点.但是,如果要在JButton上放置组件,则应将其他鼠标事件(例如,单击)传递给基础JButton.
Yes, it is possible but probably there are better ways to change the appearance of a JButton on mouse over. You can extend a ButtonUI to do that. However, if you want to drop a component over a JButton, you should pass other mouse events (e.g clicks) to the underlying JButton.
这篇关于将子组件添加到JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!