矩形以外的其他形状的按钮 [英] Buttons in shapes other than rectangles

查看:71
本文介绍了矩形以外的其他形状的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建非矩形(或圆角矩形)形状的按钮.例如,您能否在地图上以状态的形式创建按钮?单击形状中的任意位置会导致播放MouseEvent吗?

Is it possible to create buttons in shapes other than rectangles (or rounded rectangles). Could you, for instance, create a button in the shape of a state on a map? Would clicking anywhere in the shape cause a MouseEvent to be broadcast?

推荐答案

您可以通过扩展JButton并调用 setContentAreaFilled(false); 来创建此类按钮.在构造函数中.然后,您可以在覆盖的 paintComponent()方法中绘制任何内容.另外,您还需要重写 JComponent contains()方法以提供所需的鼠标命中测试.在此方法中,您可以调用多边形的(用于绘制按钮的多边形) contains()方法.

You can create such buttons by extending JButton and calling setContentAreaFilled(false); in the constructor. Then you can paint whatever you like in the overridden paintComponent() method. Also you need to override the contains() method of JComponent to provide desired mouse hit-test. In this method you can call polygon's (the one used for drawing the button) contains() method.

这篇关于矩形以外的其他形状的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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