Adding a child component to a JButton

北战南征 提交于 2020-01-05 13:24:11

问题


Is it possible to place a child component inside a JButton and make it transparent to a subset of mouse events so that:

  • The child component receives MouseMotionEvents (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.

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.

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.


回答1:


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.




回答2:


JXLayer is just the thing you need. Check out https://jxlayer.dev.java.net/ The project site has several good articles about JXLayer's usage for many different use cases.



来源:https://stackoverflow.com/questions/1281612/adding-a-child-component-to-a-jbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!