Posted on 2010-02-09 12:06
zht 阅读(3738)
评论(3) 编辑 收藏 所属分类:
Swing
对比图:
效果图:
昨天是相当的郁闷,写程序发泄下,搞了一个qq风格的JTextField,如上图所示
关键知识点
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
zht_dream@hotmail.com