java label 边框_Java中JLabel圆角的最简单代码[复制]
时间: 2023-12-08 21:04:10 浏览: 151
Java 圆角边框实例
4星 · 用户满意度95%
下面是Java中使用JLabel实现圆角边框的最简单代码:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RoundedBorderJLabel extends JLabel {
private int radius;
private Color color;
public RoundedBorderJLabel(String text, int radius, Color color) {
super(text);
this.radius = radius;
this.color = color;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(color);
g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, radius, radius);
super.paintComponent(g);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rounded Border JLabel");
JPanel panel = new JPanel();
panel.add(new RoundedBorderJLabel("Hello, World!", 20, Color.RED));
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
```
在这个示例中,我们创建了一个名为RoundedBorderJLabel的自定义JLabel,它具有圆角边框。构造函数接受文本、半径和颜色参数,并在paintComponent方法中绘制了一个带有圆角边框的矩形。在main方法中,我们将自定义的JLabel添加到一个JPanel中,并将其添加到JFrame中进行显示。
阅读全文