java swing font 设置颜色
时间: 2024-05-07 09:20:08 浏览: 19
在Java Swing中,您可以使用setFont()方法设置字体,使用setForeground()方法设置前景颜色,也就是字体颜色。
以下是设置字体和颜色的示例代码:
```
import javax.swing.*;
import java.awt.*;
public class FontAndColorExample extends JFrame {
public FontAndColorExample() {
JLabel label = new JLabel("Hello, World!");
label.setFont(new Font("Serif", Font.BOLD, 24));
label.setForeground(Color.RED);
getContentPane().add(label, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new FontAndColorExample();
}
}
```
在上面的示例代码中,我们创建了一个JLabel,并使用setFont()方法设置字体为Serif,加粗,字号为24。使用setForeground()方法将字体颜色设置为红色。
您可以根据需要更改字体和颜色。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)