对比图
效果图
关键知识点
1:
圆角效果
(1)通过setClip设置剪切区域,只绘制圆角区域
RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, this.getWidth(), this.getHeight(), 20, 20);
g.setClip(rect);
注意要在super之前设置
(2)重载paintBorder方法绘制圆角边框
RoundRectangle2D.Double rect = new RoundRectangle2D.Double(1, 1, this.getWidth() - 2, this.getHeight() - 2, 20, 20);
g2d.setColor(new Color(0, 100, 100));
g2d.draw(rect);
2:
查询图标
(1)重载getInsets方法设置间隙
(2)重载paintComponent绘制图片和三角箭头
3:
鼠标及事件触发
(1)光标:添加addMouseMotionListener事件,在mouseMove中设置光标
if (getIconBounds().contains(e.getPoint())) {
SearchTextField.this.setCursor(Cursor.getDefaultCursor());
} else {
SearchTextField.this.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}
(2)鼠标点击search图标事件:添加addMouseListener事件,在mouseClick中弹出菜单或做其它处理
if (getIconBounds().contains(e.getPoint())) {
JPopupMenu menu = new ZHTPopupMenu("menu");
for (int i = 1; i < 6; i++) {
JCheckBoxMenuItem item = new JCheckBoxMenuItem("item" + i);
menu.add(item);
}
menu.show(SearchTextField.this, getWidth() - 5, getHeight() - 2);
}
by
zhangtao