java swing 怎么绘制一个圆角矩形的面板?
1个回答
展开全部
关键知识点
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);
}
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);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询