java中JLabel的setFont()方法
时间: 2024-09-15 20:15:37 浏览: 118
`JLabel`是Java Swing库中的一个组件,它用于显示文本。`setFont()`是一个方法,用于设置标签所显示文本的字体。你可以通过这个方法改变文本的样式,如大小、粗细、斜体、颜色等,让文本看起来更具个性化。
例如,以下代码展示了如何使用`setFont()`方法:
```java
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.Font;
public class JLabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JLabel Font Example");
frame.setSize(400, 200);
JLabel label = new JLabel("Hello, World!");
// 设置字体为Arial,大小为16点,粗体
Font font = new Font("Arial", Font.BOLD, 16);
label.setFont(font);
frame.getContentPane().add(label);
frame.setVisible(true);
}
}
```
在这个例子中,`setFont()`方法接受一个`Font`对象作为参数,该对象描述了新的字体属性。如果你想要自定义更复杂的字体设置,可以创建一个新的`Font`实例。
阅读全文
相关推荐



















