Swing如何设置字体的颜色Color.red或new Color(int r, int g, int b​)

Swing如何设置字体的颜色

设置字体的颜色其实很简单的

有两种方法

第一:

//定义了 一个JLable组件

public static JLabel center_label_login_prompt = new JLabel("");        

//在其他类您可以这样设置文字和颜色

common.center_label_login_prompt.setText("登录失败,用户名或密码错误了!");
common.center_label_login_prompt.setForeground(Color.red);
第二:

根据,第一,我们查看了源码,发现他是这样来定义颜色的


public final static Color white     = new Color(255, 255, 255);
public final static Color WHITE = white;
public final static Color lightGray = new Color(192, 192, 192);
public final static Color LIGHT_GRAY = lightGray;
public final static Color gray      = new Color(128, 128, 128);
public final static Color GRAY = gray;
public final static Color darkGray  = new Color(64, 64, 64);
public final static Color DARK_GRAY = darkGray;
public final static Color black     = new Color(0, 0, 0);
public final static Color BLACK = black;
public final static Color red       = new Color(255, 0, 0);
public final static Color RED = red;
public final static Color pink      = new Color(255, 175, 175);
public final static Color PINK = pink;
public final static Color orange    = new Color(255, 200, 0);
public final static Color ORANGE = orange;
public final static Color yellow    = new Color(255, 255, 0);
public final static Color YELLOW = yellow;
public final static Color green     = new Color(0, 255, 0);
public final static Color GREEN = green;
public final static Color magenta   = new Color(255, 0, 255);
public final static Color MAGENTA = magenta;
public final static Color cyan      = new Color(0, 255, 255);
public final static Color CYAN = cyan;
public final static Color blue      = new Color(0, 0, 255);
public final static Color BLUE = blue;
是的,您看到了,他的静态变量是一个 Color类型的


所以第二种设置颜色的方法就是

common.center_label_login_prompt.setForeground(new Color(int r, int g, int b));



[caption id="attachment_1402" align="alignnone" width="476"]Swing改变字体颜色 Swing改变字体颜色[/caption]

爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情Blog Img