展开全部
设置一个圆角矩形的Border即可。
panel.setBorder(BorderFactory.createLineBorder(Color.RED, 10, true));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
面板是jpanel。你要的是在jpanel上面画一个圆角矩形图形吗?
追问
我要把JPANEL绘制成圆角矩形
追答
示例代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class SubtleSquareBorder implements Border
{
protected int m_w = 6;
protected int m_h = 6;
protected Color m_topColor = Color.white;
protected Color m_bottomColor = Color.gray;
protected boolean roundc = false; // Do we want rounded corners on the border?
public SubtleSquareBorder(boolean round_corners)
{
roundc = round_corners;
}
public Insets getBorderInsets(Component c)
{
return new Insets(m_h, m_w, m_h, m_w);
}
public boolean isBorderOpaque()
{
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
{
w = w - 3;
h = h - 3;
x ++;
y ++;
// Rounded corners
if(roundc)
{
g.setColor(m_topColor);
g.drawLine(x, y + 2, x, y + h - 2);
g.drawLine(x + 2, y, x + w - 2, y);
g.drawLine(x, y + 2, x + 2, y); // Top left diagonal
g.drawLine(x, y + h - 2, x + 2, y + h); // Bottom left diagonal
g.setColor(m_bottomColor);
g.drawLine(x + w, y + 2, x + w, y + h - 2);
g.drawLine(x + 2, y + h, x + w -2, y + h);
g.drawLine(x + w - 2, y, x + w, y + 2); // Top right diagonal
g.drawLine(x + w, y + h - 2, x + w -2, y + h); // Bottom right diagonal
}
// Square corners
else
{
g.setColor(m_topColor);
g.drawLine(x, y, x, y + h);
g.drawLine(x, y, x + w, y);
g.setColor(m_bottomColor);
g.drawLine(x + w, y, x + w, y + h);
g.drawLine(x, y + h, x + w, y + h);
}
}
}
调用方式:
JPanel panel = new JPanel();
panel.setBorder(new SubtleSquareBorder(true));
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关键知识点
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询