swing 将修改label字体格式
时间: 2023-11-09 09:06:43 浏览: 60
您可以使用以下代码来修改 JLabel 的字体格式:
```
// 获取 JLabel 实例
JLabel label = new JLabel("Hello World");
// 创建新的字体
Font font = new Font("Arial", Font.BOLD, 16);
// 将字体设置到 JLabel 上
label.setFont(font);
```
上述代码中,首先获取了一个 JLabel 的实例,然后创建了一个新的字体对象,最后将这个字体对象设置到 JLabel 上。您可以根据需要修改字体的名称、字体样式和字体大小。
相关问题
swing 添加文字label
若要在 Swing 中添加文字标签,可以使用 JLabel 组件。以下是一个简单的示例代码:
```java
import javax.swing.*;
public class LabelExample {
public static void main(String[] args) {
// 创建 JFrame 窗口
JFrame frame = new JFrame("Label Example");
// 创建 JLabel 组件
JLabel label = new JLabel("Hello, World!");
// 将 JLabel 添加到 JFrame 中
frame.getContentPane().add(label);
// 设置 JFrame 大小
frame.setSize(300, 200);
// 设置 JFrame 可见
frame.setVisible(true);
}
}
```
在上面的代码中,我们创建了一个 JFrame 窗口,并在其中添加了一个 JLabel 组件,该组件包含文本 "Hello, World!"。最后设置了 JFrame 的大小并使其可见。
你可以根据自己的需求修改 JLabel 的文本、字体、颜色和位置等属性。
swing 将lable改为圆角
要将 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 参数来自定义圆角标签的外观。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)