将lable设置成圆角的方法

lable设置成圆角的方法

第一步先添加框架:QuartzCore,frame

label.layer.cornerRadius = 10;

label.layer.masksToBounds = YES;

 

利用控件尺寸和圆角半径创建圆,半径为50的圆

Label.frame = CGRectMake(0,0,100,100);

label.layer.cornerRadius = 50;

label.layer.masksToBounds = YES;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将 JLabel 改为圆角,可以通过设置其 Border 来实现。以下是一个示例代码: ``` import javax.swing.*; import java.awt.*; public class RoundedLabel extends JLabel { private int radius; private Color backgroundColor; private Color foregroundColor; public RoundedLabel(String text, int radius, Color backgroundColor, Color foregroundColor) { super(text); this.radius = radius; this.backgroundColor = backgroundColor; this.foregroundColor = foregroundColor; } @Override protected void paintComponent(Graphics g) { int width = getWidth(); int height = getHeight(); Graphics2D graphics = (Graphics2D) g; graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setColor(backgroundColor); graphics.fillRoundRect(0, 0, width, height, radius, radius); graphics.setColor(foregroundColor); super.paintComponent(graphics); } @Override public Dimension getPreferredSize() { Dimension preferredSize = super.getPreferredSize(); int width = preferredSize.width + radius; int height = preferredSize.height + radius; return new Dimension(width, height); } public static void main(String[] args) { JFrame frame = new JFrame(); RoundedLabel label = new RoundedLabel("Hello, World!", 10, Color.BLUE, Color.WHITE); label.setHorizontalAlignment(SwingConstants.CENTER); frame.add(label); frame.setSize(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } ``` 在上面的代码中,我们创建了一个继承自 JLabel 的 RoundedLabel 类,它具有圆角边框和背景色。我们重写了 paintComponent 和 getPreferredSize 方法来绘制圆角边框和设置首选大小。在 main 方法中,我们创建了一个 JFrame 并添加了一个 RoundedLabel 实例。 你可以根据需要调整 radius、backgroundColor 和 foregroundColor 参数来自定义圆角标签的外观。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值